How can I send cursor's values to an string array[]

As I say at the title, I want to save my cursor's values to an string array. I will use this array in a ArrayAdapter and call setLineAdapter(ArrayAdapter).. I have these codes but LogCat says at arr[i] = crr.getString(i) line have a problem... Can somebody help me?

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

Upvotes: 1

Views: 1385

Answers (2)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132982

Change your code as using do-while and use ArrayList instead of Array populating dynamic values from 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());
}

Upvotes: 1

Philip Sheard
Philip Sheard

Reputation: 5815

You have to allocate the array, thus:

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

Upvotes: 0

Related Questions