2012-12-04 103 views
0

我想从数据库中检索名称。但是,我总是得到错误有:无法从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要求, ,字符串名中的大小为1 = helper.getProductNameT(cur);

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

+0

Selvin回报是他的回答是正确的。不要忘了在调用cur.close()时关闭游标。 – dymmeh

回答

4

你忘了呼吁cursormoveToFirst()这是从database.rawQuery

cur=helper.getProductDirect(....); 
if(cur.moveToFirst()){ 
    /* 
    String name=helper.getProductNameT(cur);  
    ... rest code goes here ... 
    */ 
} 
+0

thx帅哥.....问题解决了... – johnk