1

正如我在标题中所说,我想将光标的值保存到字符串数组中。我将在 ArrayAdapter 中使用这个数组并调用 setLineAdapter(ArrayAdapter).. 我有这些代码,但 LogCat 说在arr[i] = crr.getString(i)行有问题......有人可以帮我吗?

DBAdapter db;
String arr[];

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    db = new DBAdapter(this);
    db.open();

    ArrayAdapter<String> AA = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, arr);

    try {
          Cursor crr = db.getRecord(4);

          crr.moveToFirst();

          for (int i = 0; i <= cr.getCount(); i++){ 
            arr[i] = cr.getString(i);
            crr.moveToNext();
    }}
    catch (IOException e) {e.printStackTrace();}

    setListAdapter(AA);
    db.close();
4

2 回答 2

1

将代码更改为使用 do-while 并使用ArrayList而不是 Array 从 cursor 填充动态值:

 ArrayList<String> arrcurval=new ArrayList<String>();
if (crr.moveToFirst()) {
   do {
       arrcurval.add(crr.getString(0)); //<< pass column index here instead of i

     } while (crr.moveToNext());
}
于 2013-01-05T16:22:46.197 回答
0

您必须分配数组,因此:

      Cursor crr = db.getRecord(4);
      int n = crr.count();
      arr = new int[n];
于 2013-01-05T16:19:13.210 回答