0

I am trying to get all items from the table in sqlite and need to display it in List View.

Here is my code for getting Items

public List<MenuData> getMenuItem(){
    SQLiteDatabase db;
    Cursor cursor=null;
    List<MenuData> menuList = new ArrayList<MenuData>();
    db=getReadableDatabase();
    String query ="SELECT * from "+TABLE_NAME_MENU;
    try{
    cursor = db.rawQuery (query, null );
    }
    catch (NullPointerException e){
        Log.e("Error","Null Pointer Exception");
    }

   if ( cursor.moveToFirst()) {
        do {
            MenuData menuData= new MenuData();
            menuData.setKEY_ITEM_NAME(cursor.getString(cursor.getColumnIndex(KEY_ITEM_NAME)));
           menuData.setKEY_ITEM_CATEGORY(cursor.getString(cursor.getColumnIndex(KEY_ITEM_CATEGORY)));
            menuData.setKEY_ITEM_CONTENTS(cursor.getString(cursor.getColumnIndex(KEY_ITEM_CONTENTS)));
            menuData.setKEY_ITEM_TYPE(cursor.getString(cursor.getColumnIndex(KEY_ITEM_TYPE)));
            menuData.setKEY_PRICE(cursor.getString(cursor.getColumnIndex(KEY_PRICE)));
            menuList.add(menuData);
        } while (cursor.moveToNext());
    }
   return menuList;

}

My problem is I got the result menuList of last row of the table and it has same number of rows in table. So the list view has all the same items.

Top Cat
  • 2,473
  • 3
  • 22
  • 34

4 Answers4

6

Move the MenuData menuData= new MenuData() inside the do-while loop so you're creating new objects instead of updating the same object over and over again.

Also, change

if (cursor!=null) {
    cursor.moveToFirst();

to

if (cursor.moveToFirst()) {

so your code doesn't crash in case there are no result rows. Checking for cursor != null is not really necessary.

laalto
  • 150,114
  • 66
  • 286
  • 303
0

plz correct your code with this structure

if(cursor.getCount() != 0 && cursor != null)
{
 do
  {
    your values.. 
  }while(cursor.moveToNext())
} 

all data is store MenuData then Menudata values are attached to Listview..

Amit
  • 51
  • 9
  • All data are attached to a MenuData object and each MenuData object is added to the ArrayList<> to pass to the List View. – Top Cat Mar 11 '14 at 10:04
0

you have to change your last part of code to:

if(cursor != null && cursor.getCount() > 0){
    while(cursor.moveToNext(){
       // your code
     }
}

... and are you sure that problem in this code? Check your datalist. May be your problem in your adapter.

Vitaliy
  • 69
  • 4
0

Maybe you dont't close the cursor. my suggestion of code:

public List<MenuData> getMenuItem(){
    List<MenuData> menuList = new ArrayList<MenuData>();
    SQLiteDatabase db = getReadableDatabase();
    Cursor cursor = db.rawQuery ("SELECT * from " + TABLE_NAME_MENU, null);
    try { 

        if ( cursor.moveToFirst()) {
            do {
                MenuData menuData = new MenuData();
                menuData.setKEY_ITEM_NAME(cursor.getString(cursor.getColumnIndex(KEY_ITEM_NAME)));
                menuData.setKEY_ITEM_CATEGORY(cursor.getString(cursor.getColumnIndex(KEY_ITEM_CATEGORY)));
                menuData.setKEY_ITEM_CONTENTS(cursor.getString(cursor.getColumnIndex(KEY_ITEM_CONTENTS)));
                menuData.setKEY_ITEM_TYPE(cursor.getString(cursor.getColumnIndex(KEY_ITEM_TYPE)));
                menuData.setKEY_PRICE(cursor.getString(cursor.getColumnIndex(KEY_PRICE)));
                menuList.add(menuData);
            } while (cursor.moveToNext());
        }

    } finally {
        // close the cursor
        if(cursor != null) {
            cursor.close(); 
        }
    } catch(Exception e) {
        // handle exception
    }
    return menuList;
}
Kenumir
  • 664
  • 8
  • 24
  • this too dont work. I got list containing the last row elements in the table. – Top Cat Mar 12 '14 at 09:24
  • Maybe problem is somewhere else, try add some Log.i where data is read from cursor, mayby something is wrong when you display items of menu – Kenumir Mar 12 '14 at 09:50
  • my list got all same elements as i checked. – Top Cat Mar 12 '14 at 09:53
  • i write sample app, works fine whit this code: https://drive.google.com/file/d/0BwoxRKemDO1EZXBqUFQ3cjNwdTQ/edit?usp=sharing – Kenumir Mar 12 '14 at 10:35