0

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

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;
            }

数据库.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;

有人知道我的编码错误在哪里吗?

4

1 回答 1

4

你忘了打电话moveToFirst()cursordatabase.rawQuery

cur=helper.getProductDirect(....);
if(cur.moveToFirst()){
   /*
   String name=helper.getProductNameT(cur);    
   ... rest code goes here ...
   */
}
于 2012-12-04T15:37:51.273 回答