I am trying to implement ListView with Delete functionality to delete item from the listview. I am successful to delete but failed to refresh the listview after deletetion o
I'm guessing that using
getActivity().recreate();
instead of restarting the activity via a new Intent is better because using a new Intent will only stop the current activity and not destroy it.
Anyway, it works.
Call that Activity once again Using Intent
Try calling refreshDrawableState to tell the list to redraw.
I did something like this in my adapter:
((Activity)cxt).finish();
Intent intent = new Intent(cxt, YourActivity.class);
cxt.startActivity(intent);
This ends the activity and then starts the same one again.
I have the solution:
If you want to delete a row from a list view clicking on the DELETE button of each of that row do this. Here you have an example of a custom adapter class with a name and a delete button. Every time you press the button the row is deleted
public class UserCustomAdapter extends ArrayAdapter<User>{
Context context;
int layoutResourceId;
ArrayList<User> data = new ArrayList<User>();
public UserCustomAdapter(Context context, int layoutResourceId,ArrayList<User> data) {
super(context, layoutResourceId, data);
this.layoutResourceId = layoutResourceId;
this.context = context;
this.data = data;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
UserHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new UserHolder();
holder.textName = (TextView) row.findViewById(R.id.textView1);
holder.btnDelete = (Button) row.findViewById(R.id.button2);
row.setTag(holder);
} else {
holder = (UserHolder) row.getTag();
}
User user = data.get(position);
holder.btnDelete.setTag(position);
holder.textName.setText(user.getName());
holder.btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String pos = v.getTag().toString();
int _posicion = Integer.parseInt(pos);
data.remove(_posicion);
notifyDataSetChanged();
}
});
return row;
}
static class UserHolder {
TextView textName;
Button btnDelete;
}
}
Does it remove it from your list adapter?
If not that would be the reason the notifyDataSetChanged()
won't do you much good.
Actually looking at your code again i can only find that you're removing it from your database and not the adapter itself.
edit (to answer comment):
Well that's hard to do without your ListView_CustomAdapter
class.
The problem is, in this adapter there's a data set (the one you put in the constructor (listitemDisplay
)) which needs to be updated as well. Only then the notifyDataSetChanged()
will work.