无法从SQLite数据库中检索数据

时间:2012-12-04 15:28:52

标签: android sqlite

我想从数据库中检索名称。但我总是得到错误:

String name=helper.getProductNameT(cur);

public View getView(int position, View convertView, ViewGroup parent) {
                View tmpView = super.getView(position, convertView, parent);
                Log.i(CN, "getView:" + position);

                final CheckBox cBox = (CheckBox) tmpView.findViewById(R.id.checkD);

                Item tag = (Item) cBox.getTag();

                cBox.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        Cursor cur = null;
                        String name2;
                        Item tag = (Item) v.getTag();
                        if (tag == null)
                            Log.i(CN, "checkbox clicked no tag");

                        else

                                //Cursor c=helper.getProductDirect(String.valueOf(tag.id));

                                Log.i(CN, "checkbox clicked tag=" + tag.id);
                                helper.open();

                                cur=helper.getProductDirect(String.valueOf(tag.id));
                                String name=helper.getProductNameT(cur);

                                try{



                                }catch(Exception e){
                                    Log.e("error", e.toString());
                                }
                                //helper.insertProduct3(list,2, "name");



                        if (cBox.isChecked()) {
                            Log.i(CN, " Checked!");
                            // do some operations here




                            helper.insertProduct3(list,2, "go");
                        } else {
                            Log.i(CN, "NOT Checked!");
                            // do some operations here
                            //helper.updateStatusUnCheck(tag.id);



                        }
                        // notifyDataSetChanged();

                        Intent start = new Intent(AddProductDirect.this,
                                productClass.class);
                    //  start.putExtra(ID_EXTRA, String.valueOf(list));
                        //startActivity(start);
                    }

                });

                return tmpView;
            }

Database.java

public Cursor getProductDirect(String id) {
                String[] arg={id};

                // TODO Auto-generated method stub

                    return (database.rawQuery("SELECT " + SQLiteHelper.product_id
                            + " as _id," + SQLiteHelper.productBar + ","
                            + SQLiteHelper.productName + " ," + SQLiteHelper.productDesp
                            + "," + SQLiteHelper.productQtty + ","
                            + SQLiteHelper.productPrice + ","
                            + SQLiteHelper.productTotalPrice + ","
                            + SQLiteHelper.product_FId + "," + SQLiteHelper.product_Image
                            + "," + SQLiteHelper.product_ShoppingF + ","
                            + SQLiteHelper.product_Status + " FROM "
                            + SQLiteHelper.productTable + " WHERE "+SQLiteHelper.product_id+"=?",arg));



            }
    public String getProductNameT(Cursor c) {
            // TODO Auto-generated method stub
            return ((c.getString(2)));
        }

不断返回错误

  

android.database.CursorIndexOutOfBoundsException:索引-1请求,   String name = helper.getProductNameT(cur);

中的大小为1

任何人都知道编码中的错误在哪里?

1 个答案:

答案 0 :(得分:4)

您忘记致电从moveToFirst()

返回的cursor database.rawQuery
cur=helper.getProductDirect(....);
if(cur.moveToFirst()){
   /*
   String name=helper.getProductNameT(cur);    
   ... rest code goes here ...
   */
}