android.database.CursorIndexOutOfBounds:请求异常索引-1,大小为6

时间:2015-01-29 05:58:43

标签: java android simplecursoradapter android-cursoradapter

MarineModel.java

 public Cursor MedicineList()
{
    SQLiteDatabase db = database.getReadableDatabase();
    String countQuery ="SELECT * FROM " + TABLE_Marine ;
    Cursor cursor = db.rawQuery(countQuery, null);
    Log.e("***********med***", cursor.toString());
    cursor.moveToFirst();
     return cursor;
}

AndroidListViewCursorAdaptorActivity.java

 public class AndroidListViewCursorAdaptorActivity extends Activity {
private MarineModel dbHelper;
private SimpleCursorAdapter dataAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.medicinelist);
    dbHelper = new MarineModel(this);
     displaymyListView();

}

private void displaymyListView() {

    Cursor cursor = dbHelper.MedicineList();
     String[] columns = new String[]{
                MarineModel.B_Name,
                MarineModel.AvailableQuant
        };

        int[] to = new int[]{
                R.id.bname,
                R.id.quantity
        };


       cursor.moveToFirst();
        dataAdapter = new SimpleCursorAdapter(
                getBaseContext(), R.layout.mainlistview,
                cursor,
                columns,
                to, 0);
        ListView listView = (ListView) findViewById(android.R.id.list);
        listView.setAdapter(dataAdapter);
   }
 }

但是我收到了错误

android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 6.

我调试了它,它显示了返回的所有列名。表中总共有10列,我需要显示2.模型部分或CursorAdapter的问题也是如此。请帮助:)

1 个答案:

答案 0 :(得分:0)

 public Cursor MedicineList()
{
    SQLiteDatabase db = database.getReadableDatabase();
    String countQuery ="SELECT * FROM " + TABLE_Marine ;
    Cursor cursor = db.rawQuery(countQuery, null);
cursor.moveToFirst();
    Log.e("***********med***", cursor.toString());
     return cursor;
}