3

I try to convert my Cursor data to a arraylist<String[]>. But at the end all the data in the arraylist is overwrited with the last row. What do i do wrong?

Cursor c = myDbHelper.getLvl1Cata();
String[] data = new String[3];
c.moveToFirst();
while(!c.isAfterLast()) {
    data[0] = Integer.toString(c.getInt(0));
    data[1] = c.getString(1);
    data[2] = Integer.toString(c.getInt(2));
    Log.e("cc", data[1]);
    catalogueData.add(data);
    c.moveToNext();
}   

3 Answers 3

12

Try this

Cursor c = myDbHelper.getLvl1Cata();
String[] data;
if (c != null) {
    while(c.moveToNext()) {
        data = new String[3]; // Note this addition
        data[0] = Integer.toString(c.getInt(0));
        data[1] = c.getString(1);
        data[2] = Integer.toString(c.getInt(2));
        Log.e("cc", data[1]);
        catalogueData.add(data);
    }
    c.close();
}

data is an array of strings. In the original code, you added the same array to your catalogueData structure several times. You changed the value of the array's contents each time, but it was still the same array object. So you ended up with catalogueData holding several references to a single array, and that array can only have one value for data[0]: the last thing you set it to.

This answer fixes that by using a new and different array for each row in the cursor.

Sign up to request clarification or add additional context in comments.

5 Comments

Hey Waren, I was also doing the same thing, I'm getting error at catalogueData.add(data);, what exactly is that? Please can you guide me through this.
@Anupam you need to tell me what the error is, otherwise I can't help.
@WarrenFaith Is there any way to read the cursor and store in the array list in one go. If i don't want to run the loop ?
@ShivamAgrawal No. The cursor is just a pointer to data and you need to move it by yourself. So looping is the only chance.
@Anupam catalogueData.add(data); is adding data to arraylist catalogueData. catalogueData variable should be List or ArrayList or something similiar to it.
1

Try this:

if(mycursor!=null){    
            do{
    TextView name = (TextView)view.findViewById(R.id.contact_name);               
    name.setText(cursor.getString(cursor.getColumnIndex
                (Displayname)));
    mycursor.moveToNext();
            }while (mycursor.isLast());
        }

Comments

0

Put String[] data = new String[3]; into the while loop. You're overwriting the array object with each iteration.

4 Comments

creating a new object in a loop is bad practice in android! See my answer where declaration and initialization is done in two steps.
@WarrenFaith: Can you clarify what you mean about creating a new object in a loop? Your answer also creates a new object in a loop, although you do separate declaration of the variable from creating the object.
@LarsH: His answer would perform the exact same as mine. It doesn't matter where the declaration is put. If you want to save a collection of Arrays, you have to create a new set of Arrays at each iteration or else the previous one will just overwrite the last set.
Otherwise if you were just throwing the array away at each iteration then yes, that would be true. You'd want to create the object outside the loop.

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.