TextView with Database - android

I'd like to put some data in a TextView. My Code so far:
myDataBase = myDbHelper.getReadableDatabase();
Cursor cursor = myDataBase.rawQuery(KLASSEN_SELECT_RAW, null);
startManagingCursor(cursor);
SimpleCursorAdapter sca =
new SimpleCursorAdapter(this, R.layout.barinfo_layout, cursor, new String[]
{"name"}, new int[] {R.id.barinfo_barinfo});
barinfo.setText((CharSequence) sca);
Eclipse doesn't show errors but the programm chrashes when I execute it.
What do I have to change?

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

SimpleCursorAdapter returns empty data

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

GridView with images from DataBase

I have images in my DB and I put it to my GridView by this code:
public void setNotes()
{
String[] columns = {NotesDbAdapt.KEY_ID, NotesDbAdapt.KEY_IMG, NotesDbAdapt.KEY_NAME, NotesDbAdapt.KEY_DATE, NotesDbAdapt.KEY_TIME};
String table = NotesDbAdapt.NOTES_TABLE;
Cursor c = MainNote.mDB.getHandle().query(table, columns, null, null, null, null, null);
startManagingCursor(c);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.file_dialog_row,
c,
new String[] {NotesDbAdapt.KEY_IMG, NotesDbAdapt.KEY_NAME, NotesDbAdapt.KEY_DATE, NotesDbAdapt.KEY_TIME},
new int[] {R.id.img, R.id.txt, R.id.date, R.id.time});
adapter.setViewBinder(new NotesBinder());
gridview.setAdapter(adapter);
}
All ok, but but the scrolling is slow, jerky. Seems that information takes from DB every time. How to fix it?
This method was deprecated in API level 11. Use the new CursorLoader class with LoaderManager instead. It can perform the cursor query on a background thread so that it does not block the application's UI.

Sorting a Cursor in Android to be used in SimpleCursorAdapter

I'm doing a project for my Android class. The app is very ugly and not very useful, but it's to demonstrate that we can implement Content Providers. The problem I'm having is sorting the Cursor. Here is my code:
setContentView(R.layout.main);
String[] projection = new String[] {Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER};
Cursor mCursor = this.getContentResolver().query(Phone.CONTENT_URI, projection , null, null, "Phone.CONTACT_ID ASC");
startManagingCursor(mCursor);
ListAdapter adapter = new SimpleCursorAdapter(
this, // Context.
R.layout.rows,
mCursor,
new String[] {Phone.CONTACT_ID, Phone.DISPLAY_NAME, Phone.NUMBER},
new int[] {R.id.text1, R.id.text2, R.id.text3});
setListAdapter(adapter);
Instead of doing this:
..., "Phone.CONTACT_ID ASC");
try this
..., Phone.CONTACT_ID + " ASC");

Categories

Resources