SimpleCursorAdapter returns empty data - android

When I am trying to put data from Database to ListView via SimpleCursorAdapter, the rows of the ListView are shown but they are empty.
Like:
[___________]
[___________]
[___________]
Instead of:
[TEXT]
[TEXT]
[TEXT]
My code:
db = provider.getReadableDatabase();
String[] columns = {"_id", "Title"};
Cursor cursor = db.query(NotificationsProvider.TABLE_NAME, columns, null, null, null, null, null);
Log.d(TAG, "COUNT: "+cursor.getCount());
String[] clm = {"Title"};
int[] to = {android.R.id.list};
cursorAdapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
cursor, clm, to, 0);
setListAdapter(cursorAdapter);
Thanks!

You are simply using the wrong id, try:
int[] to = {android.R.id.text1};
(You can check out simple_list_item_1.xml yourself to verify the appropriate id.)

Related

Fetch specific data from android database sqlite

I'm wondering if it is possible to fetch one specific type of data from an android database, based on sqlite.
Let's say I have a table with rows "Category" and "Title" and I want to get a String array containing the "Title" of those categories matching a given one.
For example:
Title Category
A spam
B important
C spam
And given "spam", I want to get a String array like
S = {A,C}
Is there a way to do this?
Please note that I'm very new to databases.
Thanks in advance.
EDIT:
I'm actually trying with a query
mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE,
KEY_BODY, KEY_CATEGORY}, KEY_CATEGORY + "=" + category, null, null, null, KEY_CATEGORY);
But it returns a Cursor and I need a SimpleCursorAdapter like here for formatting
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.notes_row, notesCursor, from, to);
mList.setAdapter(notes);
where from and to are:
String[] from = new String[] { NotesDbAdapter.KEY_TITLE };
int[] to = new int[] { R.id.text1 };
FINAL EDIT:
It finally worked, with this code provided by #ELITE
Cursor cursor = mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, KEY_BODY, KEY_CATEGORY}, KEY_CATEGORY + "=?", new String[]{category}, null, null, KEY_CATEGORY);
ArrayList elements = new ArrayList();
while(cursor.moveToNext()) {
elements.add(cursor.getString(cursor.getColumnIndex(KEY_TITLE)));
}
// use elements variable,
// here you'll get ["A", "C"]
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.notes_row, cursor, from, to);
mList.setAdapter(notes);
Iterator over the cursor and store the result in ArrayList or Vector and then use it.
Cursor cursor = mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE, KEY_BODY, KEY_CATEGORY}, KEY_CATEGORY + "=?", new String[]{category}, null, null, KEY_CATEGORY);
ArrayList elements = new ArrayList();
while(cursor.moveToNext()) {
elements.add(cursor.getString(cursor.getColumnIndex(KEY_TITLE)));
}
cursor.close();
// use elements variable,
// here you'll get ["A", "C"]
SimpleCursorAdapter notes =
new SimpleCursorAdapter(this, R.layout.notes_row, elements, from, to);
mList.setAdapter(notes);
Hope it'll work.
For more details refer this question
Use the query() of SQLiteDatabase class like this:
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query("<name of the table>", new String[]{}, "Category=?", new String[]{"spam"}, null, null, null);

Can I add custom list layout by SimpleCursorAdapter?

I am new with android..I am learning work with database files..I have a simplecurseadapter like this
Cursor c = db.query(TABLE_NAME,columns,null, null, null, null, COLUMN_ID);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list_item, c,
new String[] {COLUMN_ID,COLUMN_NAME,COLUMN_order}, new int[] {R.id.list_item_text_id,R.id.list_item_text_main,R.id.list_item_text_sub}, 0);
ListView list = (ListView) findViewById(R.id.list_poet_name);
list.setAdapter(adapter);
I want to know Is there any way to make a custom listview?my column_order has just to value 0,1 and I want rows with 0 value shows in R.id.list_item_text_main and if its value is 1 it shows in R.id.list_item_text_sub Can I define an if statement in SimpleCursorAdapter?I yes How?If no..Whats the best way to do it?
Make your query like this
Cursor c = db.query(TABLE_NAME,columns,COLUMN_order + "=? OR " + COLUMN_order + "=?", new String[]{"0", "1"}, null, null, COLUMN_ID);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list_item, c,
new String[] {COLUMN_ID,COLUMN_NAME,COLUMN_order}, new int[] {R.id.list_item_text_id,R.id.list_item_text_main,R.id.list_item_text_sub}, 0);
ListView list = (ListView) findViewById(R.id.list_poet_name);
list.setAdapter(adapter);
I think I find my answer...
http://enjoyandroid.wordpress.com/2012/03/12/customizing-simple-cursor-adapter/
I am working on it..Maybe I am wrong

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");

Android: Select Query SQLite for ListView

I'm using this method to display a table and its content from SQLite on a ListView:
listView.setAdapter(new MySimpleCursorAdapter(this, R.layout.listitems,
managedQuery(Uri.withAppendedPath(Provider.CONTENT_URI,
Database.Project.NAME), new String[] { BaseColumns._ID,
Database.Project.C_PROJECTTITLE,
Database.Project.C_SMALLIMAGE, Database.Project.C_PROJECTDESCRIPTION, Database.Project.C_ORGANIZATIONTITLE, Database.Project.C_DONATIONAMOUNT, Database.Project.C_KEYWORD, Database.Project.C_PRICE, Database.Project.C_SHORTCODE}, null, null, null),
new String[] { Database.Project.C_PROJECTTITLE,
Database.Project.C_SMALLIMAGE, Database.Project.C_PROJECTDESCRIPTION, Database.Project.C_ORGANIZATIONTITLE, Database.Project.C_DONATIONAMOUNT}, new int[] {
R.id.txt_title, R.id.image, R.id.txt_list_desc, R.id.txt_org, R.id.btn_amount}));
Now I'm wondering how am I able to do this:
For example I want to display only Database.Project.C_PROJECTTITLE which contains the word 'Health' in its column.
So only the PROJECTS whose PROJECTTITLE contain the word 'Health' will show up on the list.
It doesn't have to be EQUAL 'Health', so the PROJECTTITLE with the name for example 'Health/Bio' also appears on the list. How am I able to perform that?
You query should be something like below:
db.query(TABLE_NAME, new String[] {"_id", "yourColumn"},"yourColumn like " + "'%Health%'", null, null, null, null);
If you let the search term be %health% then it should select all rows that contain health.

Android array list from database

I wanna create an array of cities that are stored in the database
Cities Table
CREATE TABLE cities (_id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT);
Querying for City List
// City List
public Cursor cityList() throws SQLException {
return db.query(TABLE_CITIES, new String[] {ID, KEY_NAME}, null, null, null, null, null, null);
}
Trying to Get the content into the Array
Cursor cities = db.cityList();
startManagingCursor(cities);
String[] city_list = new String[] { DBAdapter.KEY_NAME };
Spinner cityList = (Spinner)this.findViewById(R.id.citySpiner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, city_list);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
cityList.setAdapter(adapter);
Am not able to populate the Spinner.. with the database content.
Try SimpleCursorAdapter
String[] from = new String[] { DBAdapter.KEY_NAME };
int[] to = new int[] {android.R.id.text1};
SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(context, android.R.layout.simple_spinner_dropdown_item , cursor, from, to);
cityList.setAdapter(cursorAdapter);
Edit:
from means the column(s) from the Cursor which will be used to display as text array in Spinner.
to means the id(s) of the view which will hold the value of that column.
It is very interesting that the view at index N will hold the text from column at N.

Categories

Resources