0

i am using code this blog to have a draggable list. This tutorial is using a custom DragNDropAdapter that takes the content as an ArrayList.

In my listActivity i query a table with returned column name.It has 11 values inserted. i tried to convert it to ArrayList from String[] with many ways such as :

 String[] from = new String[]{DbManager.KEY_NAME};
 ArrayList<String> content = new ArrayList<String>();

 for (int i=-1,l=from.length; ++i<l;) {
            content.add(from[i]);
            //Log.i("ArrayList", from[i]);
        }

or

 while(!mShopCatCursor.isAfterLast()){
         content.add(mShopCatCursor.getString(0));
     }

what i get is a list with just the name of the column, name. do you have any ideas

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Nik Tsekouras
  • 289
  • 2
  • 15

2 Answers2

1

You can use following method this method will get data from db and then return you an ArrayList of String for this data. In your case this array list will contain names.

private ArrayList<String> getArrayList() {

    ArrayList<String> namesList = null;

    Cursor cursor = null;
    try {
        String query = "";//your query here
        cursor = db.rawQuery(query,null);
        if (cursor != null && cursor.moveToFirst()) {
            namesList = new ArrayList<String>();
            do {
                namesList.add(cursor.getString(0));
            } while (cursor.moveToNext());
        }
    } catch (Exception e) {
        e.printStackTrace();
        namesList = null;
    } finally {
        if (cursor != null && !cursor.isClosed()) {
            cursor.deactivate();
            cursor.close();
            cursor = null;
        }
        close();
    }
    return namesList;
}


/**
 * Closes the database
 */
private void close() {
    try {
        if (db != null && db.isOpen()) {
            DBHelper.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Muhammad Nabeel Arif
  • 19,140
  • 8
  • 51
  • 70
0
String[] from = new String[]{DbManager.KEY_NAME};

Because your string array has only one value which is KEY_NAME.

What you need to do is,

Get values from Cursor using loop and populate it String[] above.

Cursor userCur = adaptor.getYourData();
            if (userCur != null) {
                String[] strArr = new String[userCur.getCount()];
                startManagingCursor(userCur);
                if (userCur.moveToFirst()) {
                    int count = 0;
                    do {
                        String userName = userCur.getString(1);
                        strArr[count] = userName.trim();
                        count++;
                    } while (userCur.moveToNext());
                }

ArrayList<String> content = new ArrayList<String>();  
               for (int i=-1,l=from.length; ++i<l;) {    
                content.add(from[i]);              
      //Log.i("ArrayList", from[i]);           
     }                  
}

Note: I haven't validated this in IDE, there may be syntax errors.

kosa
  • 65,990
  • 13
  • 130
  • 167
  • thanks that worked and is only reasonable. i was confused from many tutorials that used the above syntax String[] from = new String[]{DbManager.KEY_NAME}; and import the from array into ListAdapters. i have a question though regarding your answer. Why should i use if(userCur != null) at the first line and not something like this: cursor.moveToFirst() and then the while loop. And second question why do i need the trim function inside the loop. thanks very much anyway – Nik Tsekouras Jan 29 '12 at 12:27
  • It depends on how your getYourData() method implemented. Some implementation it may be null, if cursor is null, cursor.moveToFirst() will throw NullPointerException. So, it is always better to do null check. – kosa Jan 29 '12 at 12:39