Top Cat
Top Cat

Reputation: 2483

Error with SQLite cursor

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.

Upvotes: 0

Views: 880

Answers (4)

Vitaliy
Vitaliy

Reputation: 69

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.

Upvotes: 0

Kenumir
Kenumir

Reputation: 671

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

Upvotes: 0

Amit
Amit

Reputation: 51

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..

Upvotes: 0

laalto
laalto

Reputation: 152787

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.

Upvotes: 6

Related Questions