1

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();
1
  • Maybe like String arr[]=new String[cr.getCount()]; Commented Jan 5, 2013 at 16:20

2 Answers 2

1

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());
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanx... Everything is OK now =)
Now, how can I do a onListItemListener, which opens a TableView, when I clicked the items? I cannot do it with if position is x, start intent I suppose... Because I dont know how many items will be there... Listener should open the TableView and also should send a integer(index) to TableView to say which item has chosen. And TableView should get the data from DataBase, and show it; using the index which is sent by Listener? But, I have no idea how can I do it... (or am I wrong?)
@ArdaOğulÜçpınar : to get current task done you will need to set setOnItemClickListener for ListView and do your work. try it if you have any issue then make a new question for new issue .Thanks
0

You have to allocate the array, thus:

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

1 Comment

You may also want to replace cr.getString(i) to get the String from the proper column, cr.getString(cr.getColumnIndex(THE_INDEX)) :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.