Weird Nerd Weird Nerd - 1 year ago 102
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

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

Intent editIntent = new Intent(this,NoteDisplay.class);
editIntent.putExtra(Constants.ROW_NAME,Constants.ROW_NUMBER);
startActivity(editIntent);

}


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.

@Override
public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId())
{

case R.id.deletebutton:

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

return true;

default:
return super.onOptionsItemSelected(item);

}
}


This is my custom Cursor adapter class,

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



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

if(getDeleteFlag==0)
{
ll = (LinearLayout)view.findViewById(R.id.listViewLayout);
setListViewHeight(ll,context);

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(R.id.content);
recordID = (TextView) view.findViewById(R.id.recordID);
dateET = (TextView) view.findViewById(R.id.date);
tv.setText(getText.trim());
recordID.setText(existsRecordID);
dateET.setText(dateFormatter.format(newdate));
}else{
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.

EDIT 1:

The above code in my
bindView()
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);
yAdapter.changeCursor(yCursor);

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download