0

let's say if this is my code

Cursor mCursor = dbAdapter.getAllTitles(); //returns results from 3 columns, eg column1, column2, column3

is there anyway I can assign the values from column1 to 1 string array, column2 to another and so on? perhaps like this

String[] getColumn1 = mCursor.dataFrom("column1");
String[] getColumn2 = mCursor.dataFrom("column2");
String[] getColumn3 = mCursor.dataFrom("column3");

examples i see to achieve the same result as above involves iterating through the values and put them into the respective array. too much resource consuming, no? then it's better to make separate sql query for each columns, no?

2
  • 1
    What is your Schema? and Why do you need to assign column values to an array of Strings? Commented Jan 20, 2012 at 9:10
  • what do u mean by Schema? And let's just say I need to manage to assign column values to an array of Strings Commented Jan 20, 2012 at 9:36

5 Answers 5

5

I found the answer posted by Dyonisos is correct, except I would change this line in the for loop:

for(mCursor.moveToFirst(); mCursor.moveToNext(); mCursor.isAfterLast())

to this:

for(cursor.moveToFirst(); !cursor.isAfterLast();cursor.moveToNext() )

As the middle statement is the one that checks whether to continue the loop. This may answer Liam W's question in the accepted answer's comments.

Disclaimer: I am aware this should've been a comment, but I don't have enough reputation to make one and this information might be useful to others who come looking for this information in the future =)

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

Comments

4
ArrayList<String> columnArray1 = new ArrayList<String>();
ArrayList<String> columnArray2 = new ArrayList<String>();
ArrayList<String> columnArray3 = new ArrayList<String>();
for(mCursor.moveToFirst(); mCursor.isAfterLast(); mCursor.moveToNext()) {
    columnArray1.add(mCursor.getString(COLUM_INDEX1));
    columnArray2.add(mCursor.getString(COLUM_INDEX2));
    columnArray3.add(mCursor.getString(COLUM_INDEX3));
}

Afterwards you can convert the ArrayList into a String array:

String[] colStrArr1 = (String[]) columnArray1.toArray(new String[columnArray1.size()]);

4 Comments

this one converts it into ArrayList right.. how do I convert it to something that can be insert inside String[] getColumn for example?
You can convert your ArrayList to a String array. E.g.: String[] colStrArray1 = (String []) columnArray1.toArray(new String [columnArray1.size()]);
Hey Dyonisos, I'm trying to do the same thing, but columnArray1.add(mCursor.getString(COLUM_INDEX1)); COLUMN_INDEX is giving me error to change this into string. Can you please guide me through this.
Sorry to bump an old question & answer, but this works with one snag - the first one is always ignored. I don't know if it's an issue with the query selection, or this system...
0

Another way to do this without the use of ArrayList:

final int size = mCursor.getCount();
mArray1 = new String[size];
mArray2 = new String[size];
mArray3 = new String[size];

if (mCursor.moveToFirst) {
    int i = 0;
    do {
        mArray1[i] = mCursor.getString(COLUMN_INDEX_1);
        mArray2[i] = mCursor.getString(COLUMN_INDEX_2);
        mArray3[i] = mCursor.getString(COLUMN_INDEX_3);
        i++;
    } while (mCursor.moveToNext());
}

This will also work for arrays of primitives

Comments

0

Actually, it's easy to convert cursor into arrays. Use this method:

private void initializeArrays(Cursor c) {
    collumn1 = new String[c.getCount()];
    collumn2 = new String[c.getCount()];
    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
        Array.set(collumn1, c.getPosition(), c.getString(c.getColumnIndex(""collum1")));
        Array.set(collumn2, c.getPosition(), c.getString(c.getColumnIndex("collumn2")));
    }
}

Comments

0

As several other answers have said, I agree that dyonisos's answer is basically correct, but I think that a simpler way (and safer way) to implement the loop is a while loop like this:

while (mcursor.moveToNext()){
            try {
                columnArray1.add(cursor.getString(1));
                columnArray2.add(cursor.getString(2));
                columnArray3.add(cursor.getString(3));
                 ...
            }catch(Exception e) {
                Log.d("error",e.toString());
            }
        } 

and then one can convert the arrays as in the previously cited answer.

String[] colStrArr1 = (String[]) columnArray1.toArray(new String[columnArray1.size()]);

Comments

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.