gpt4 book ai didi

java - android.database.Cursor IndexOutOfBounds : Exception Index -1 requested, 大小为 6

转载 作者:太空宇宙 更新时间:2023-11-04 14:12:01 26 4
gpt4 key购买 nike

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 的问题也是如此。请帮助:)

最佳答案

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

关于java - android.database.Cursor IndexOutOfBounds : Exception Index -1 requested, 大小为 6,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28207888/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com