I have an Activity which goes through the normal procedures.
databaseHelper = new AppDatabaseHelper(this ); //openOrCreateDatabase(Constants.NAME, 1, null);
database = databaseHelper.getWritableDatabase();
// select columns[] from table order by sentdate desc
cursor = database.query(AppDatabaseHelper.MAIL_TABLE, columns, null, null, null, null, AppDatabaseHelper.C_MAIL_SENTDATE + " DESC");
// Does the displaying work //
cursorAdapter = new SimpleCursorAdapter(this, R.layout.mail_element, cursor, columns, columnsMap);
lv.setAdapter( cursorAdapter );
My problem is when the list gets refreshed, the dataset observer does not fire, and executing this code from a timer to ensure it is working causes all elements in the listview to become greyed out, or 50ish% invisible.
Log.d(Constants.TAG, "MailActivity > List changed!");
CursorAdapter.notifyDataSetChanged();
cursor = database.query(AppDatabaseHelper.MAIL_TABLE, columns, null, null, null, null, AppDatabaseHelper.C_MAIL_SENTDATE + " DESC");;
cursorAdapter = new SimpleCursorAdapter(context, R.layout.mail_element, cursor, columns, columnsMap);
lv.setAdapter( cursorAdapter );
Does anyone have an idea why these elements are turning grey when the list is updated? When I exit the activity and re-open it, it shows up fine again.
Solved, I had to change the cursor, instead of re-doing the adapter!
Log.d(Constants.TAG, "MailActivity > List changed!");
cursorAdapter.notifyDataSetChanged();
cursor = database.query(AppDatabaseHelper.MAIL_TABLE, columns, null, null, null, null, AppDatabaseHelper.C_MAIL_SENTDATE + " DESC");;
cursorAdapter.changeCursor( cursor );
lv.setAdapter( cursorAdapter );
Related
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();
I have a database and I would like to use it as data source for MultiAutoCompleteTextView. Is it possible utilize SimpleCursorAdapter in MultiAutoCompleteTextView?
this is code
Cursor cursor = db.query(TABLE_NAME, COLUMN, null, null, null, null, null);
mAdapter = new SimpleCursorAdapter(context,android.R.layout.simple_list_item_1, cursor,new String[]{"name"},new int[]{android.R.id.text1},0);
multiText.setAdapter(mAdapter);
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");
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);
?
I have a SQLite database and I'm getting a ListView from the database through a SimpleCursorAdapter. This is the existing code:
Cursor c = mDatabase.query(
"database",
bla = new String[]{
"_id",
"title",
"message",
"time",
"date",
"done"
},
null, null, null, null, null);
startManagingCursor(c);
SimpleCursorAdapter listAdapter =
new SimpleCursorAdapter(this,
R.layout.list_item, c,
new String[]{"title", "message", "time", "date"},
new int[]{R.id.txtv_title, R.id.txtv_message, R.id.txtv_time, R.id.txtv_date}
);
setListAdapter(listAdapter);
This works as intended and I'm getting my ListView.
Now I want that if the "done" field in my database contains the string "false" (instead of "true" ...), this row doesn't get into the ListView.
How can I do this?
Thanks in advance!
This is more a SQL then Android-related. What you're looking for is a Where-clause:
String query = "SELECT _id, title, message, time, date, done "+
"FROM database "+
"WHERE done = 'true' "+
"ORDER BY date";
To send this Query to your SQLite Database, you can use the rawQuery()-method:
final Cursor c = db.rawQuery(query, null);