【问题标题】:android.database.CursorIndexOutOfBounds: Exception Index -1 requested, with a size of 6android.database.CursorIndexOutOfBounds:请求异常索引-1,大小为6
【发布时间】:2015-01-29 05:58:43
【问题描述】:

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

【问题讨论】:

  • 然后确保您的光标正确关闭..
  • 向我们展示完整的 logcat

标签: java android simplecursoradapter android-cursoradapter


【解决方案1】:
 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;
}

【讨论】:

  • 对于大小使用 cursor.getCount();
  • 但我得到 mRowColumnIndex = -1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-12
  • 1970-01-01
  • 2017-12-24
相关资源
最近更新 更多