I need to get data from DB & displayed it as list with pagination.
i.e. 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 .
Related
I am calling same activity for 5, 6 different movies to book ticket. I have used button for seats. I set button disable after clicking and store its id in sql database. Now when I come again to book another ticket for same movie, same show
String[] projection= new String[]{
movieEntry.COLUMN_NAME_SEAT
};
//where clause
String selection=movieEntry.COLUMN_NAME_MN + " =?"+" AND "+movieEntry.COLUMN_NAME_MD +" =?" +" AND "+movieEntry.COLUMN_NAME_MT +" =?" ;
// int i=0;
String colindex=null;
// this query is select seatno from tablename
// where movie_name='movie that user selected' and movie_time='user seklected currently'
//and movie_date='';
//it is working correctly its fetching all records
// i am trying to add all this fetched seatno to the seatsArraylist so that
//we can get it from this arrayList and disable that seats for the user for that particular movie and date and time
// but problem is that it is adding only one records to arraylist
//can you plz go through this code
String[] selectionArgs ={ Movietitle, Datem, Mtiming} ;
try{
Cursor cursor = rdb.query(
movieEntry.TABLE_NAME,
projection,
selection,
selectionArgs,
null,
null,
null
);
if(cursor!= null ) {
int i=0;
while(cursor.moveToFirst())
{
colindex= cursor.getString(i);
seatsArrayList.add(colindex);
i++;
tv1.setText(seatsArrayList.get(2));
}
tv.setText(colindex);
}
/* for(int j=0;j<=seatsArrayList.size();j++)
{
if(seatsArrayList.get(j)=="Seat4")
{
theBtn[3].setEnabled(false);
}
}*/
You are not supposed to iterate in your cursor like that you have just made your cursor to move to the first part (0th). To iterate in a cursor its easy with a for loop like this:
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
// all operations should be here
}
And again even inside the loop the method cursor.getString(int) the int passed is not the index of a cursor but it is a column index. In your table there may be multiple columns the first one is (0th) the second column is (1st). So you should pass column indexes.
Android Studio 0.4.6
Hello,
I have the following function that should get the rows of the database in ASC order based on the column name. However it just gets the rows in the order that they are listed in the database.
So it should be in alphabetical order for names in the database.
I think my cursor query is correct, as in the debugger the cursor value is:
SQLiteQuery: SELECT _id, name, phone, email FROM friends ORDER BY name ASC
Which is what I want. I then set my cursor to the first row and loop over over them until I get to the last row.
However, it doesn't display in the alphabetical for name.
private void loadDB() {
Cursor cursor = db.query(FriendContract.TABLE,
new String[] {FriendContract.Column.ID, FriendContract.Column.NAME, FriendContract.Column.PHONE, FriendContract.Column.EMAIL},
null, null, null, null, FriendContract.Column.NAME + " ASC");
/* Check if database is empty */
if(cursor.getCount() == 0) {
/* There are no rows to load - so just return */
Log.d(TAG, "loadDB() cursor.getCount() == 0. There are no rows, just just refresh listview");
adapter.notifyDataSetChanged();
return;
}
/* Clear all items from array list -
we are going to fill this with the content of the database */
friendsList.clear();
Friend friend;
cursor.moveToFirst();
while(!cursor.isAfterLast()) {
friend = new Friend();
friend.setId(cursor.getLong(0));
friend.setName(cursor.getString(1));
friend.setPhone(cursor.getString(2));
friend.setEmail(cursor.getString(3));
/* Add this to the list of friends */
friendsList.add(friend);
cursor.moveToNext();
}
/* Clean up */
cursor.close();
/* Refresh the listview with the loaded friends */
adapter.notifyDataSetChanged();
}
The code my above question is working as expected.
I am trying to get the last 3 entries made to the database. Im using the following code but im only getting the last entry not the last 3.
String query = "Select * from " + TABLE_DETAILS + " DESC limit 3;";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
if (cursor.moveToFirst()) {
cursor.moveToFirst();
System.out.println(Integer.parseInt(cursor.getString(2)));
cursor.close();
} else {
}
db.close();
Thanks
You'll need to loop with the cursor to get all result rows, e.g. instead of
if (cursor.moveToFirst()) {
...
loop like this
while (cursor.moveToNext()) {
System.out.println(cursor.getString(2));
}
cursor.close();
To change the ordering, add ORDER BY <expression> to your query, for example
SELECT ... ORDER BY _id DESC LIMIT 3
I have a table called food. I am selecting the "category" of the food item and I want to show it in a List View. This is the code I tried.
Cursor c = db.query(DATABASE_TABLE,
new String[] { "category" }, null, null, null, null, null);
ArrayList<ArrayList<String>> results = new ArrayList<ArrayList<String>>();
if(c.moveToFirst())
{
do
{ ArrayList<String> recipe = new ArrayList<String>();
recipe.add(c.getString(1));
recipe.add(c.getString(2));
recipe.add(c.getString(3));
recipe.add(c.getString(4));
results.add(recipe);
}while(c.moveToNext());
if(c != null && !c.isClosed())
c.close();
}
Try Now,
Cursor c = db.query(DATABASE_TABLE,
new String[] { "category" }, null, null, null, null, null);
ArrayList<String> results = new ArrayList<String>();
if(c.moveToFirst())
{
do
{
results.add(c.getString(0)); // instead of 0 Index of Category column in your case
}while(c.moveToNext());
if(c != null && !c.isClosed())
c.close();
}
From your database query You are selecting only category column so you have only one column result in cursor and its start with 0 index. So c.getString(1) to c.getString(4) is meaning less. If your select all data from table then only you get all columns..
I see your problem, you are only returning one column (category), yet you are trying to access several different ones.
You should be returning at least five (since you are trying to access up to 4 and the cursor columns start at 0).
If you are trying to pull a list of items with a certain category you need to change your query. Somethign like this :
String query = "SELECT * FROM " + DATABASE_TABLE + " WHERE category = " + category;
return mDb.rawQuery(query, null);
That will select all items that have a category matching whatever is contained in the variable category, and return all the coilumns in the row.
My SQLite query returning only one record, However, the table has multiple rows
cursor=mydb.rawQuery("Select category from items;", null);
I have even tried GROUP BY but still wont work.
I am new to SQLite, would appreciate any help. Thanks.
First of all your string query must not be terminated so instead of passing it as:
"Select category from items;"
you should try passing it as:
"Select category from items"
as mentioned on this page.
Also, are you looping over the cursor? Here is an example of how to get data out of a cursor with a while loop:
ArrayList<String> results = new ArrayList<String>()
while (cursor.moveNext()) {
results.add(cursor.getString(0)); // 0 is the first column
}
First, search:
Cursor cs = myDataBase.rawQuery("Your query", null);
if (cs.moveToFirst()) {
String a = cs.getString(cs.getColumnIndex("your_column_name"));
cs.close();
return a;
}
cs.close();
return "";
Get the information from cursor:
if (cs.moveToFirst()) {
ArrayList<String> results = new ArrayList<String>()
do {
results.add(cs.getString(cs.getColumnIndex("your_column_name")));
} while (cs.moveNext());
}
Without if, I took error in my project. But this worked for me. By the way, your query doesn't look good. If you give some information about your database, we can help much more.
Use this to select all items from the table:
Cursor cursor = db.rawQuery("SELECT * FROM Tablename ,
null);
this can help you
public ArrayList<mydata> getallContents() {
ArrayList<mydata> lst = new ArrayList<mydata>();
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery("select * from " + GAMETABLE, null);
if (c.moveToFirst()) {
do {
lst.add(new mydata(c.getString(1),
c.getString(3),c.getString(4),c.getString(5),c.getString(6)));
} while (c.moveToNext());
}
db.close();
return lst;
}
you don't need raw query method. i think that the android way is better in this case:
db.query(items, category, null, null, null, null, null);
than use the cursor how already is written in the other comment.