How to add an item to an already populated SimpleCursorAdapter? - android

I'm using CursorLoader to fetch categories from a content provider. The data is being populated in SimpleCursorAdapter and displayed in a dropdown menu (spinner).
The thing is, I need to add an additional item to the list that will be displayed first.
How can I do that?
Here is the code:
CursorLoader cl = new CursorLoader(Main2Activity.this,
ContentProvider.createUri(Category.class, null), null, null, null,
"name ASC");
Cursor cursor = cl.loadInBackground();
final SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(Main2Activity.this,
android.R.layout.simple_spinner_item,
cursor,
new String[]{"name"},
new int[]{android.R.id.text1},
0);
cursor.close();

Related

Update ListView

I received cursor from Database and set andapter to ListView. Then I insert new row to Database, but my ListView didn't update.
Cursor c = getContentResolver().query(GEN_URI, null, null, null, null);
String[] from = { DB.column_name };
int[] to = { R.id.textViewItem };
myAdapter = new SimpleCursorAdapter(this, R.layout.shop_list_item, c, from, to, SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER );
ShopList.setAdapter(myAdapter);
ContentValues newC = new ContentValues();
newC.put(DB.column_name, "Селёдочка");
Uri uri = getContentResolver().insert(SHOP_LIST_URI, newC);
getContentResolver().query(GEN_URI, null, null, null, null);
myAdapter.notifyDataSetChanged();
If I set flag SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER - ListView don't update.
If I set flag SimpleCursorAdapter.FLAG_AUTO_REQUERY- ListView update.
What do?
You can tryed myadapter.notifyDataSetChanged();
You must notify the adapter that data has changed and tell it to refresh.
adapter.notifyDataSetChanged();
When using the flag autorequery, the cursor automatically requeris and hence, your listview is getting updated, But as far as I know, this FLAG_AUTO_REQUEY is now deprecated.
UPDATE
After some googling, I found out that notifyDataSetChanged has nothing to do with SimpleCursorAdapters.
So the workaround is requers(). But as I stated it is deprecated and the new method is by changing the Cursor.
adapter.changeCursor(cursor) and send it to your adapter.
Here is the google's official documentation about requery().

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

Android Binding SQLite to GridView in Eclipse

I have made the following code to retrive data from SQLite database.
public Cursor fetchAllScores() {
return database.query(DATABASE_TABLE, new String[] { KEY_ROWID,
KEY_PLAYDATE, KEY_NUMVALA, KEY_NUMVALB }, null, null, null,
null, null);
}
Then I call this function in my main.java file using the following
cursor = dbHelper.fetchAllScores();
startManagingCursor(cursor);
After having cursor I manage to populate myGridView with some data using following code
GridView myGV = (GridView)findViewById(R.id.gridView1);
String[] cols = new String[] { scoreDbAdapter.KEY_PLAYDATE, scoreDbAdapter.KEY_NUMVALA, scoreDbAdapter.KEY_NUMVALB};
int[] views = new int[] { android.R.id.text1, android.R.id.text2, android.R.id.text2 };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, cursor, cols, views);
Log.w("NumRows",adapter.getCount() + "");
myGV.setAdapter(adapter);
Now the problem is only first row is populated and first two columns, I want data like in forst row (2011-10-27 , 5 , 6 ) and second row like (2011-10-26 , 3 , 2 ) but i m getting is only forst row like (2011-10-27, 2011-10-26 ).
Can this be fixed in GridView?
For your question, take a look here, Binding to Data with AdapterView.
Note: Below decription is just example for how to bind cursor to an adapter.
Filling the Layout with Data
Inserting data into the layout is typically done by binding the AdapterView class to an Adapter, which retrieves data from an external source (perhaps a list that the code supplies or query results from the device's database).
The following code sample does the following:
1.Creates a Spinner with an existing View and binds it to a new ArrayAdapter that reads an array of colors from the local resources.
2.Creates another Spinner object from a View and binds it to a new SimpleCursorAdapter that will read people's names from the device contacts (see Contacts.People).
// Get a Spinner and bind it to an ArrayAdapter that
// references a String array.
Spinner s1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter adapter = ArrayAdapter.createFromResource( this, R.array.colors,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter);
// Load a Spinner and bind it to a data query.
private static String[] PROJECTION = new String[] { People._ID, People.NAME};
Spinner s2 = (Spinner) findViewById(R.id.spinner2);
Cursor cur = managedQuery(People.CONTENT_URI, PROJECTION, null, null);
SimpleCursorAdapter adapter2 = new SimpleCursorAdapter(this,android.R.layout.simple_spinner_item,cur,new String[] {People.NAME}, new int[] {android.R.id.text1});
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s2.setAdapter(adapter2);
Note that it is necessary to have the People._ID column in projection used with CursorAdapter or else you will get an exception.
If, during the course of your application's life, you change the underlying data that is read by your Adapter, you should call notifyDataSetChanged(). This will notify the attached View that the data has been changed and it should refresh itself.
For more info look at this,
Custom CursorAdapters.
Thanks.
Make a new layout which contains textViews. Find the layout as shown in first line of the code below and attach your data to the listView. ListView loads the layout containing textView.
private void populateGrid()
{
ListView lv = (ListView)findViewById(R.id.listView1);
Cursor c = dbHelper.fetchAllScores();
startManagingCursor(cursor);
String[] cols = new String[] { TodoDbAdapter.KEY_PLAYDATE, TodoDbAdapter.KEY_NUMVALA, TodoDbAdapter.KEY_NUMVALB};
int[] views = new int[] { R.id.txt_date, R.id.txt_no, R.id.txt_yes};
// Now create an array adapter and set it to display using our row
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.listviewtemp, c, cols, views);
Log.w("NumRows",adapter.getCount() + "");
lv.setAdapter(adapter);
}
R.layout.listviewtemp is the listView template layout (separate xml), lv is the listview found in the mainlayout.xml.
public Cursor fetchAllScores() {
return database.query(DATABASE_TABLE, new String[] { KEY_ROWID,
KEY_PLAYDATE, KEY_NUMVALA, KEY_NUMVALB }, null, null, null,
null, null);
}

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.

Categories

Resources