2012-12-04 147 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