简体   繁体   中英

how to retrieve particular records from SQLite in android

I need to get data from DB & displayed it as list with pagination.

ie If i retrieved 4 items..i need to display first 2 items in first page.

When i click next button.,remaining 2 items should be displayed which replaces old 2.

How could i restrict data from DB as 2 like that?

My code..

 db.open();

   // db.insertTitle("Money");
    //db.insertTitle("make");
    //db.insertTitle("make");
    //db.insertTitle("make");

    Cursor c = db.getAllTitles();
    if (c.moveToFirst())
    {
        do {          
              String firstName = c.getString(c.getColumnIndex("user"));
              results.add( firstName );
           } while (c.moveToNext());
    }
  setListAdapter(new ArrayAdapter<String>(DisplayAll.this, android.R.layout.simple_list_item_1,results));

    db.close();
}

My DBAdapter..

public Cursor getAllTitles() {

        return db.query(DATABASE_TABLE, new String[] 
                    {
                KEY_ROWID, 
                KEY_USER,
                }, 
                null, 
                null, 
                null, 
                null, 
                null);
    }

Please try this

public Cursor getAllRecords(int page,int totalRecord)
{
   return db.rawQuery("select * from your_table_name limit "+(page-1)*totalRecord+","+totalRecord, null);
}

Where limit = how many record you want at a time if you want 2 record then pass limit = 2 if 10 record then set limit = 10.. and page = first initial page variable with 1 and when second time you fetch next record increase your page variable by 1 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM