Weird Nerd Weird Nerd - 1 year ago 81
Android Question

Remove a view/value from the listview Android

I have some values in my database and populating the values in my custom Listview with a custom Cursor Adapter.So when the person clicks on the listView a new Activity is opened and when he selects the delete button the that value or View should disappear and the list should be refreshed.

I had referred to many answers on SO but all of them suggested to removing the value from the database(which is not what I want) and some were using ArrayList and suggested me to use it with custom Array Adapters.But I am using a Cursor adapter.

So my whole question is how do I remove a value or remove a view from the listview?

The Listview item onClick Listener opens a new Activity

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Constants.ROW_NUMBER=position;
TextView getRecordID = (TextView) view.findViewById(;
Constants.ROW_NUMBER = Integer.parseInt(getRecordID.getText().toString());

Intent editIntent = new Intent(this,NoteDisplay.class);


This is the new Activity and on the press of a Menu button the record will marked as deleted but won't be deleted from the database.

public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId())


DataBaseHelper deletedb = new DataBaseHelper(this);
int deletedata = deletedb.deleteAction(position,System.currentTimeMillis());
Intent dintent = new Intent(this,MainActivity.class);

return true;

return super.onOptionsItemSelected(item);


This is my custom Cursor adapter class,

public View newView(Context context, Cursor cursor, ViewGroup parent) {
return inflater.inflate(R.layout.customlistview, parent, false);

public void bindView(View view, Context context, Cursor cursor) {
//int getDeleteFlag is initialized above
getDeleteFlag = cursor.getInt(cursor.getColumnIndex(DELETE_FLAG));

ll = (LinearLayout)view.findViewById(;

if (cursor != null) {
getText = cursor.getString(cursor.getColumnIndex(NOTE_TITLE));
existsRecordID = cursor.getString(cursor.getColumnIndex(RECORD_ID));
datevalue = cursor.getLong(cursor.getColumnIndex(RECORD_DATE));
Date newdate = new Date(datevalue);
tv = (TextView) view.findViewById(;
recordID = (TextView) view.findViewById(;
dateET = (TextView) view.findViewById(;
Toast.makeText(context, "FALSE VALUE", Toast.LENGTH_SHORT).show();


I am confused whether to remove the view or the value at this point.

I have a row named "deleteflag" in my DB which stores 1=> Deleted
0=>Not deleted.

So only the values which are having 1 will be marked as deleted.


The above code in my
method does not work

Answer Source

as you said dont delete item from database just put deleteflag = 1 it is ok

but when you come back to your list or coming to your list you should do folowing

Cursor yCursor = db.rawQuery("SELECT  * FROM table where deleteflag!=1", null);

if you are using activity than you can do it in your onactivityresult or in onResume of activity

it will just refresh your adapter with latest cursor who is not deleted