Android: transform each column of a cursor into a

2019-04-02 23:45发布

问题:

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?

回答1:

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


回答2:

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 =)



回答3:

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



回答4:

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


回答5:

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