How to Delete row using context menu? - android

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!

Related

Getting individual text values from items in listView

I have a custom list view over here that displays data from a sqlite database with the help of a simple cursor adapter.
I would like to know how to display a toast showing the title of each entry (e.g. School) when I hold down each entry (using a longClickListener). Thanks.
The method for the simple cursor adapter which is called during onResume:
private void populateListViewFromDatabase() {
DBAdapter db = new DBAdapter(this);
db.open();
Cursor c = db.retrieveAllEntriesCursor();
String[] from = {DBAdapter.ENTRY_TITLE, DBAdapter.ENTRY_DESTINATION};
int[] to = {R.id.tvAlarmTitle, R.id.tvAlarmDestination};
SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(this,R.layout.custom_alarm_destination_row,c,from,to);
db.close();
lvDestinations.setAdapter(myCursorAdapter);
}

Update ListView with cursor and SimpleCursorAdapter

My cursor is fetch data from sqlite.
msgsdbadapter = new MSGSDBAdapter(this);
msgsdbadapter.open();
cons_cursor = msgsdbadapter.fetchConversations();
startManagingCursor(cons_cursor);
After that I create a SimpleCursorAdapter and assign it to ListView. The ListView now can display some records well.
String[] from = new String[] { MSGSDBAdapter.KEY_FROM, MSGSDBAdapter.KEY_MSG, MSGSDBAdapter.KEY_DATE};
int[] to = new int[] { R.id.lblFrom, R.id.lblMsgExcerpt, R.id.lblDate };
cons_cursor_adapter = new SimpleCursorAdapter(context, R.layout.conversation_item, cons_cursor, from, to);
lvConversations.setAdapter(cons_cursor_adapter);
Next, I insert new row into table and notify dataset changed, but the ListView is not update
msgsdbadapter.createMsg(msg);
cons_cursor_adapter.notifyDataSetChanged();
And when I should close the db connection?
You need to either requery() the existing Cursor, or run the query again to get a fresh Cursor and swap the new Cursor into your CursorAdapter.
The best solution if your using the ListFragment is to put this in inside this sample:
public SimpleCursorAdapter myNote;
public class NOteLIST_MAIN extends ListFragment {
#SuppressWarnings("deprecation")
#Override
public void onResume() {
super.onResume();
myNote.getCursor().requery();//Don't forget to put these on onResume }
private void DataLoader(){private void DataFiller(){
myNote = new SimpleCursorAdapter(getActivity(), R.layout.notes_row_line, notesCursor, from, to,0);
setListAdapter(myNote);}
}

SimpleCursorAdapter not reloading data&view on insert in ContentProvider

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.

How do I refresh my ListView after the database changes?

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.

ListView Click event

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.

Categories

Resources