如何将光标的值发送到字符串数组[]

时间:2013-01-05 16:14:18

标签: android listview cursor android-arrayadapter

正如我在标题中所说,我想将光标的值保存到字符串数组中。我将在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();

2 个答案:

答案 0 :(得分:1)

将代码更改为使用do-while并使用ArrayList代替从光标填充动态值的Array:

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

答案 1 :(得分:0)

你必须分配数组,因此:

      Cursor crr = db.getRecord(4);
      int n = crr.count();
      arr = new int[n];