I am trying to manage a simple list in an Android application. The contents of the list are maintained in a SQLite database. When the user selects and holds on a specific row, a context menu appears with a "Delete" option. When they select "Delete", the row is deleted from the database, but the view does not refresh. When I back out of the application and get back in, the appropriate row has been removed. So, I know the row is deleted, it's just the ListView that doesn't refresh.
Here are the relevant bits of code...
In the onCreate method...
SQLiteDatabase db = tasks.getReadableDatabase();
Cursor cursor = db.query(TABLE_NAME,
new String[] { _ID, TITLE, DETAILS, },
null, null, null, null, TITLE + " DESC");
startManagingCursor(cursor);
ListView lv = (ListView) findViewById(R.id.list);
lv.setAdapter(new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,
cursor,
new String[] {TITLE},
new int[] { android.R.id.text1}
));
In the onContextItemSelected method...
switch(item.getItemId()) {
case 0:
SQLiteDatabase db = tasks.getWritableDatabase();
ListView lv = (ListView) findViewById(R.id.list);
SimpleCursorAdapter adapter = (SimpleCursorAdapter) lv.getAdapter();
db.delete(TABLE_NAME, "_ID=?", new String[] {adapter.getCursor().getString(0)});
adapter.notifyDataSetChanged(); // Not working, clears screen and doesn't reload
return true;
}
return super.onContextItemSelected(item);
What am I missing?
Thanks!
Get rid of the notifyDataSetChanged() and call requery() on your Cursor. Here is a sample project demonstrating this.
Related
I'm creating a method to read all the information in the database and view it through a spinner here is the code i tried for this function
public Spinner loadArtist(){
SQLiteDatabase DB = getReadableDatabase();
String[] projection = {
ArtistMaster.Artist.ARTIST_NAME};
Cursor cursor = DB.query(
ArtistMaster.Artist.TABLE_ARTIST,
projection,
null,
null,
null,
null,
null);
Spinner itemIds = new ArrayList<>();
while(cursor.moveToNext()) {
long itemId = cursor.getLong(
cursor.getColumnIndex(ArtistMaster.Artist.ARTIST_NAME));
itemIds.setAdapter(itemId);
}
cursor.close();
return itemIds;
}
but it gives me an error in this line Spinner itemIds = new ArrayList<>();
Should i declare it as a list instead of spinner
itemIds should be defined as an ArrayList<Long>. A Spinner is a UI element and an ArrayList is a data structure. You will most likely need to map the data to your UI using an adapter of some sort, eg. an ArrayAdapter:
Spinner spinner = ... // findViewById, new Spinner() etc.
ArrayList<Long> itemIds = new ArrayList<>();
//... fill array with artist IDs
spinner.setAdapter(
new ArrayAdapter(
this, // Context, Activity etc.,
android.R.layout.simple_list_item_1, // Spinner TextView item resource ID
itemIds // Data set.
));
By default, ArrayAdapter will call Object#toString() on each data object in the collection.
I'd suggest that it would be easier if you used a Cursor Adpater as they are designed to be used with a Cursor and there is no need to generate arrays.
SimpleCursorAdapter being that, a simple but still pretty flexible adapter for use with Cursors.
The only issue is that a Cursor Adapter requires a column name specifically _id (BaseColumns._ID resolves to this (as used below)).
First have the following member variables (obviously names can be what you wish)
:-
Cursor mCursor;
SimpleCursorAdapter mAdapter;
Spinner spinner;
SQLiteDatabase db;
In the onCreate Method of the activity have
:-
spinner = this.findViewById(R.id.?????); //
db = ???????? (as per your existing code)
manageSpinner();
Have a method
:-
private void manageSpinner() {
mCursor = db.query(
ArtistMaster.Artist.ARTIST_NAME,
new String[]{"*","rowid AS " + BaseColumns._ID}, //<<<<<<<< adds _ID column (unless the table is a WITHOUT ROWID table, pretty unlikely)
null,null,null,null,null
);
if (mAdapter == null) {
mAdapter = new SimpleCursorAdapter(
this,
android.R.layout.simple_list_item_1,
mCursor,
new String[]{"the_column"}, // column(s) from which to extract data
new int[]{android.R.id.text1}, // layout views into which the data is placed
0
);
spinner.setAdapter(mAdapter);
// You want want to do something when an Item in the spinner is clicked (this does nothing as it is)
spinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//........... do your stuff here
// notes
// id will be the id of the row
// cursor will be positioned, so you can access data from the cursor if needed
}
});
} else {
mAdapter.swapCursor(mCursor);
}
}
Override the activity's onResume (refresh the spinner when returning to activity as underlying data may have changed) and onDestroy (to close the Cursor) methods using
:-
#Override
protected void onDestroy() {
super.onDestroy();
mCursor.close();
}
#Override
protected void onResume() {
super.onResume();
manageSpinner();
}
I'm sorry, I've tried looking at a bunch of other answers, but I can't get my particular implementation to work.
I have a ListView populated by an extended SimpleCursorAdapter. In the onCreate() method I set it up this way:
list = (ListView) findViewById(R.id.duty_history_list);
list.setOnItemClickListener(this);
list.setOnItemLongClickListener(this);
masterCursor = db.rawQuery("SELECT * FROM WorkLog", null);
list.setAdapter(new NotSoSimpleCursorAdapter(this,
R.layout.log_list_item,
masterCursor,
columns,
views,
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER));
When an item is long clicked, the corresponding row is deleted from the db. Here is my attempt to update the view. With this code, nothing happens, I'm not sure why. IT is called at the very end of handleItemLongClick(), after the db is updated.
public void refreshView() {
CursorAdapter cA = (CursorAdapter) list.getAdapter();
masterCursor = db.rawQuery("SELECT * FROM WorkLog", null);
cA.changeCursor(masterCursor);
}
How can I delete a list item and get an updated list using onContextItemSelected?
How can I delete a row using context menu?
Code:
case R.id.Delete_Note:
Database_Notepad db=new Database_Notepad(Create_Task.this);
db.DeleteNote();
break;
My Database:
public void DeleteNote(int rowId) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(Table_Notepad, Col_ID + " = ?", new String[] { String.valueOf(rowId) });
db.close();
}
How can I update my listview after the deletion of an item?
This is how I'm setting data to listview:
Database_Notepad db = new Database_Notepad(Create_Task.this); Cursor c = db.Get_All_Note();
String[] from = new String[] {Database_Notepad.Col_File_Name,Database_Notepad.Col_Due_Date};
int[] to = new int[] {R.id.titlename, R.id.duedatetext};
SimpleCursorAdapter madapter = new SimpleCursorAdapter(Create_Task.this, R.layout.file_row, c, from, to);
this.setListAdapter(madapter);
getListView().setOnItemClickListener(this);
registerForContextMenu(getListView());
db.close();
As per your comment you success to delete the row item, but you failed to update your listview,
So whenever some changes made in listview data you should notify your adater by applying notifyDataSetChanged() like this adapter.notifyDataSetChanged() ,it will reflect in your listview if some items get added or deleted.
hope it works!
this code is working fine - i'm loading a bunch of rows into the SimpleCursorAdapter and the ListView displays them. nice.
SimpleCursorAdapter adapter;
Cursor cursor;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView lv = (ListView)findViewById(R.id.main_lv_projects);
lv.setOnItemClickListener(this);
String[] columns = new String[] { CompassDataProvider.ORG_NAME };
int[] names = new int[] { R.id.organisation_row_name};
cursor = this.managedQuery(CompassDataProvider.CONTENT_URI_ORGANISATIONS, null, null, null, null);
adapter = new SimpleCursorAdapter(this, R.layout.organisation_row, cursor, columns, names);
lv.setAdapter(adapter);
startManagingCursor(cursor);
}
But when i'm inserting a new row via
long rowID = db.getWritableDatabase().insert(CompassDBHelper.ORGS_TABLE_NAME, "", values);
getContext().getContentResolver().notifyChange(CONTENT_URI_ORGANISATIONS, null);
return Uri.withAppendedPath(CONTENT_URI_ORGANISATIONS, ""+rowID);
the list view is not updateing itself - i thought the SimpleCursorAdapter is notified an can then reload its view - not?
the new row is created - i checked on this
when i'm using
cursor.requery();
adapter.notifyDataSetChanged();
in my UI threat the new data gets loaded correctly... whats the observer/listener pattern here that i did not get? :)
thanks,
Martin
The cursor is being passed into the SimpleCursorAdapter by value and not reference. So, calling cursor.requery has no effect. To get this to work the way you want, you're really going to need to implement a listAdapter and process your cursor in there.
I have a List of Items which I retrieved from my Sqlite DB...
I want to set a Click event for each item. How I can customize this event based on the Item clicked????
Be descriptive... I am a beginner.
This is the method that I used to fill data in my List:
private void fillData() {
db = new DBAdapter(this);
db.open();
ArrayList db_results = new ArrayList();
//All Category
//Cursor cursor = db.getAllTitles();
//Single Category
Cursor cursor = db.getTitle(1);
if (cursor.moveToFirst())
{
do {
db_results.add(cursor.getString(4));
} while (cursor.moveToNext());
}
cursor.close();
this.list.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, db_results));
}
Call setOnItemClickListener() on the ListView. The AdapterView.OnItemClickListener listener you provide will be given the position (0-based index) and ID (if you were using a CursorAdapter, as you should be, rather than converting the Cursor into an ArrayList), so you will know which item was clicked upon.