Skip to content
Advertisement

data from sqlite into ArrayList

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

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement