How to use limit and offset to page through the data - android

I am new to SQLite and I don't know how to use limit and offset to select a limit number of data from the database, I mean I know the query phrase, but how to use it in a cursor so I can get those data into a listview?
Currently I am using the code below to query data from the database and show them in a listview but it seems I query too much data for one query and the SQLite fail to grow, so I want to split the query into some smaller ones and do it in one time,someone suggested me to try limit and offset,but I googled it there are really not much about it on the Internet.
Would somebody kindly provide me the guide to this? an example or a tutoral, anything will do,thx
channellist = (ListView) findViewById(R.id.Channel);
mDB = new ChannelDB(this);
String[] columns = {mDB.KEY_ID, mDB.KEY_POSTER, mDB.KEY_CHANNEL, mDB.KEY_PATH, mDB.KEY_DBLINK};
String table = mDB.channelS_TABLE;
c = mDB.getHandle().query(table, columns, null, null, null, null, null);
startManagingCursor(c);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.channelview,
c,
new String[] {mDB.KEY_POSTER, mDB.KEY_CHANNEL, mDB.KEY_DBLINK},
new int[] {R.id.poster, R.id.channel, R.id.douban});
adapter.setViewBinder(new ChannelViewBinder(this));
channellist.setAdapter(adapter);

pass the last argument with number as string like you need fetch 10 then you can do like this way
c = mDB.getHandle().query(table, columns, null, null, null, null, null,"10");
for more reference see How to use the LIMIT argument in an SQLite Query with Android

Related

how to retrieve the sum of column values in simplecursoradapter in sqlite database

Am using db query to sum the column values by using group by clause. I can get the total amount but i couldn't display the values in simplecursoradapter. Please help me to display the total values. I have posted my code below.
String[] columns = new String[] { KEY_ID, KEY_CATEGORY,"sum("+KEY_AMOUNT+")as" + KEY_AMOUNT,
KEY_COMMENT };
Cursor cursor = db.query(TABLENAME2, columns, null, null, null, null,
null);
return cursor;
To display the values in Listview using Simplecursoradapter
String[] from = new String[] { Dailydialog.KEY_CATEGORY,
Dailydialog.KEY_AMOUNT,Dailydialog.KEY_COMMENT};
int[] to = new int[] { R.id.id2, R.id.id3,R.id.id4};
cursorAdapter1 = new SimpleCursorAdapter(this, R.layout.dailydialogall, cursor1,
from, to);
listview_dialog.setAdapter(cursorAdapter1);
Total values are not listed in Listview.
Instead of "sum("+KEY_AMOUNT+")as" + KEY_AMOUNT use " sum("+KEY_AMOUNT+") as " + KEY_AMOUNT. Notice the spaces? Its always a good idea to print your query in log. Another thing is that you need to specify group by clause as well for sum function otherwise only one row will be returned with total sum of amount of all the rows.

Android Simplecursoradapter not using the cursor i'm passiing

cursor = getContentResolver().query(GroceryItemProvider.ITEM_URI, projection, id+"="+GroceryItemTable.LIST_ID, null, null);
adapter= new SimpleCursorAdapter(this, R.layout.list_row, cursor, from, to , 0);
adapter.swapCursor(cursor);
This creates a list with the entire table rather than the cursor I pass with custom values, is that the default behaviour??
Do you mean you are getting all columns of data or all rows of data? If you are getting all columns, then please mention the value of variable projection. If you are getting all rows, then you can limit rows by providing the last argument to getContentResolver().query() method like:-
cursor = getContentResolver().query(GroceryItemProvider.ITEM_URI, projection, id+"="+GroceryItemTable.LIST_ID, null, "id limit 10");

ListView Data from SimpleCursorAdapter result of a query

Since a few month I am engaged with Android development and now I have a problem I dont get the right answer.
I have a ListView with data filled from a SimpleCursorAdapter. The query which should provides the results has a where statement, but it returns all records of the table.
final Cursor c = mStorage.loadList();
startManagingCursor(c);
c.moveToFirst();
final ListCursorAdapter adapter =
new ListCursorAdapterAusgabe(this, R.layout.listview,
c, FROM, TO);
adapter.setViewBinder(new ListViewBinderAusgabe());
setListAdapter(adapter);
The query:
int mode = 0;
public Cursor loadList() {
return mDb.getReadableDatabase().query(TABLE.NAME, TABLE.ALL_COLUMNS,
"mode=?", new String [] {String.valueOf(mode)}, null, null, null, null);
}
In read in this forum that there must be an _id column: In the above mentioned table there is one column with "_id". I also tried to enter "mode" in single quotations and double quotations but it didn't work. Has anybody an idea?
Thank you in advance,
Hadja
Have you tried
return mDb.getReadableDatabase().query(TABLE.NAME, TABLE.ALL_COLUMNS,
"mode=" + String.valueOf(mode), null, null, null, null, null);
?

Android: Displaying contact names in a Spinner

I'm trying to simply get contacts from the Device phone book and display them on a Spinner, the code:
// Form an array specifying which columns to return.
String[] PROJECTION = new String[] {
People._ID, People.NAME
};
// Get the base URI for the People table in the Contacts content provider.
Uri contacts = People.CONTENT_URI;
Spinner contactsSpinner = (Spinner) findViewById(R.id.recipient_names);
// Make the query.
Cursor contactsCursor = managedQuery(contacts,
PROJECTION, // Which columns to return
null, // Which rows to return (all rows)
null, // Selection arguments (none)
// Put the results in ascending order by name
People.NAME + " ASC");
Log.e("EE", String.valueOf(contactsCursor.getCount()));
SimpleCursorAdapter sca = new SimpleCursorAdapter(
this, android.R.layout.simple_spinner_item,
contactsCursor, new String[] {People.NAME}, new int[] {android.R.id.text1}
);
Log.e("EE", String.valueOf(sca.getCount()));
sca.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
contactsSpinner.setAdapter(sca);
Compiles and runs fine, just that... The logs is showing 1 for both Log.e() call, which I think imply that contacts were actually retrieved successfully, however the Spinner is empty... Could anyone tell me I am doing wrong?
Turns out that it's not supported on sdk > 2.0

Query the android Mediastore for Artist names of Music files

I'm torn about how to implement this because Content Provider URI querys do not support the simple SQL "DISTINCT" query method to return a cursor to the Artists of the songs in the mediastore, removing any duplicate entries.
I can query and get a cursor to all the artists, I'm just torn as to how to remove the dupes, or simply not show them.
I've tried using matrixcursor to construct a new cursor with the dupe entries removed, but it's slow (build an array of artists, if in that array don't copy to the new matrixcursor, etc.)
Can anyone recommend a better solution, or point me in the proper direction??
I've also thought about pre-loading the information i need into an array of objects - I'm just concerned with memory overhead in my application.
Thank You for any help you may provide.
The easiest way to get a list of all the artist (and albums is the same method) is to use the MediaStore.Audio.Artist for the quest. For Example something like this would get and show all the artist:
String[] proj = {MediaStore.Audio.Artists._ID, MediaStore.Audio.Artists.ARTIST, MediaStore.Audio.Artists.NUMBER_OF_ALBUMS, MediaStore.Audio.Artists.NUMBER_OF_TRACKS };
musiccursor = managedQuery(MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI, proj, null, null, MediaStore.Audio.Artists.ARTIST + " ASC");
String[] from= new String[]{ MediaStore.Audio.Artists.ARTIST, MediaStore.Audio.Artists.NUMBER_OF_ALBUMS, MediaStore.Audio.Artists.NUMBER_OF_TRACKS };
int[] to = new int[] { R.id.songname, R.id.rowlength, R.id.rowartist };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.musicrow, musiccursor, from, to);
SongsView.setAdapter(adapter);
Where SongsView would be your list to display them in. Everything else is using a simple Cursor adapter
Hope this helps
You might want to try creating stateful CursorWrapper, overriding the appropriate methods. Simply ensure every call to next() iterates through the cursor until it finds an artist name you consider appropriately unique, optionally storing seen artists in an ArrayList instance variable.
ArrayList<SongBeen> genresList=new ArrayList<SongBeen>();
String value=search+ "%";
String[] inValue=new String[] {value};
ContentResolver musicResolver = activity.getContentResolver();
/*Uri musicInUri = android.provider.MediaStore.Audio.Genres.INTERNAL_CONTENT_URI;
Cursor mInternalCursor = musicResolver.query(musicInUri, null, null, null, null);
*/
Uri musicExUri = android.provider.MediaStore.Audio.Genres.EXTERNAL_CONTENT_URI;
Cursor mExternalCursor = musicResolver.query(musicExUri, null, MediaStore.Audio.Genres.NAME+ " like ?",
inValue, "LOWER(" + MediaStore.Audio.Genres.NAME + ") ASC");
Cursor[] cursors = {mExternalCursor};
final MergeCursor mMergeCursor = new MergeCursor(cursors);
if (mMergeCursor.moveToFirst()) {
do {
SongBeen been=new SongBeen();
long thisId= mMergeCursor.getLong(mMergeCursor.getColumnIndexOrThrow(MediaStore.Audio.Genres._ID));
String thisTrack = mMergeCursor.getString(mMergeCursor.getColumnIndexOrThrow(MediaStore.Audio.Genres.NAME));
been.setId(thisId);
been.setTrack(thisTrack);
genresList.add(been);
} while (mMergeCursor.moveToNext());
}
mMergeCursor.close();
return genresList;

Categories

Resources