0

这是我使用该字符串的代码

String item = item1.getText().toString();
        item = item.toLowerCase();
        String date = getDate();
        Datahelper edited = new Datahelper(this);
        edited.open();
            String returnedprice = edited.getprice(item,date);
        String returneddetail = edited.getdetail(item,date);
        edited.close();
            price.setText(returnedprice);
        details.setText(returneddetail);

这是我在 sqlite 中的方法

public String getprice(String item ,String date) {
        // TODO Auto-generated method stub
         String[] columns = new String[]{KEY_ROWID, KEY_CATEGORY,KEY_DATE,KEY_PRICE,KEY_DETAILS};

         String whereClause = KEY_CATEGORY  + " = ? and " + KEY_DATE + " = ?";
         String[] whereArgs = {item,date};

         Cursor c = ourDatabase.query("DATABASE_TABLE", columns, whereClause, whereArgs, null, null, null);
            if(c!=null){
                String price = c.getString( c.getColumnIndex(KEY_PRICE));
            return price;
            }

        return null;

    }

    public String getdetail(String item, String date) {
        // TODO Auto-generated method stub

         String[] columns = new String[]{KEY_ROWID, KEY_CATEGORY,KEY_DATE,KEY_PRICE,KEY_DETAILS};

         String whereClause = KEY_CATEGORY  + " = ? and " + KEY_DATE + " = ?";
         String[] whereArgs = {item, date};

         Cursor c = ourDatabase.query("DATABASE_TABLE", columns, whereClause, whereArgs, null, null, null);
            if(c!=null){
                    String detail = c.getString( c.getColumnIndex(KEY_DETAILS));
                return detail;
                }

        return null;
    }

使用此代码时,我的应用程序崩溃了,如果有人需要,我不知道代码中有什么问题,我可以发布整个代码,请帮助我

4

2 回答 2

0

正确的代码是 -

public String getprice(String item ,String date) {
    // TODO Auto-generated method stub
     String[] columns = new String[]{KEY_ROWID, KEY_CATEGORY,KEY_DATE,KEY_PRICE,KEY_DETAILS};

     String whereClause = KEY_CATEGORY  + " = ? and " + KEY_DATE + " = ?";
     String[] whereArgs = {item,date};

     Cursor c = ourDatabase.query("DATABASE_TABLE", columns, whereClause, whereArgs, null, null, null);
        if(c!=null){
           c.moveToFirst();
            String price = c.getString( c.getColumnIndex(KEY_PRICE));
        return price;
        }

    return null;

}

public String getdetail(String item, String date) {
    // TODO Auto-generated method stub

     String[] columns = new String[]{KEY_ROWID, KEY_CATEGORY,KEY_DATE,KEY_PRICE,KEY_DETAILS};

     String whereClause = KEY_CATEGORY  + " = ? and " + KEY_DATE + " = ?";
     String[] whereArgs = {item, date};

     Cursor c = ourDatabase.query("DATABASE_TABLE", columns, whereClause, whereArgs, null, null, null);
        if(c!=null){
   c.moveToFirst();
    String detail = c.getString( c.getColumnIndex(KEY_DETAILS));
            return detail;
            }

    return null;
}
于 2013-03-10T21:26:07.900 回答
0

列索引与返回的集合没有任何共同之处:

String price = c.getString( c.getColumnIndex(KEY_PRICE));

因为c您只有 5 列(0 到 4)(String[] columns = new String[]{KEY_ROWID, KEY_CATEGORY,KEY_DATE,KEY_PRICE,KEY_DETAILS};)并c.getColumnIndex(KEY_PRICE)返回表中可能高于 4 的实际索引。

代替

String price = c.getString( c.getColumnIndex(KEY_PRICE));

尝试

String price = c.getString(3);
于 2013-03-10T19:34:31.423 回答