Reputation: 99
This problem has encountered to me and lots of bodies here but yet i haven't seen an efficient answer. i have a listview and a delete button for each item of the list. when i click on the button, the item is deleted from the database and also removed from the list but the listview doesn't refresh the items.
i have called the method notifyDataSetChanged()
but has no result.(when the item is deleted the title of the next item comes up and it seems there is two item with one title, sorted one after another one).
myListView.invalidateViews()
and myListView.invalidate()
didn't work too.
whats the exact reason and whats the absolute solution?
Here is my code:
here are the whole codes:
public class AdapterNote extends ArrayAdapter {
public AdapterNote(ArrayList<StructNote> notes) {
super(G.context, R.layout.adapter_note, notes);
}
private class ViewHolder {
TextView txtTitle;
ImageView imgDelete;
public ViewHolder(View view) {
txtTitle = (TextView) view.findViewById(R.id.txtTitle);
imgDelete = (ImageView) view.findViewById(R.id.imgDelete);
}
public void fill(final ArrayAdapter<StructNote> adapter, final StructNote item, final int position) {
txtTitle.setText(item.title + "");
imgDelete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
G.notes.remove(item);
adapter.remove(item);
deleteDataFromDatabase(item.id);
G.adapter.notifyDataSetChanged();
}
});
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
StructNote item = getItem(position);
if (convertView == null) {
convertView = G.inflater.inflate(R.layout.adapter_note, parent, false);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.fill(this, item, position);
return convertView;
}
}
Upvotes: 0
Views: 3637
Reputation: 674
Late to the discussion...
When I did it from a class where I use the adapter, the listview just won't refreshed. Even worse, at one point, sometimes it's refreshed, sometimes it's not. And I have called adapter.notifyDataSetChanged();
.
Finally I got it work by making a method to delete item directly inside the adapter, like this:
CustomAdapter extends BaseAdapter {
public void deleteItem(AdapterItem ai) {
dataList.remove(ai); /*dataList = data source used by adapter*/
notifyDataSetChanged();
}
}
Upvotes: 0
Reputation: 147
int pos = nameList.indexOf(object);
nameList.remove(pos);
listAdapter.notifyDataSetChanged();
Upvotes: 0
Reputation: 163
Just remove it from your array-list and set adapter to your list view again. do this in OnItemClickListener.
private OnItemClickListener listPairedClickItem = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
arrlist.remove(2);
lv.setAdapter(adapter);
}
};
Upvotes: 0
Reputation: 4391
The problem you are facing concerning the 'two items with same title part' has to do with loss of synchronization between the listener and the current position. Try something like this, if your custom item is of type:
Object item
Put it in the view-holder alongside your text-views. After inflating the view, do this:
holder.item = getItem(position);
And then, instead of item.xx do it:
holder.item.xx
Upvotes: 0
Reputation: 23596
Its a simple thing that you need to remove it from the adapter by calling yourAdapterObject.remove(POSITION);
Here, yourAdapterObject
is the object of the adapter and POSITION
is the position you want to remove from listview.
After that you need to refresh the adapter with the remaining data and information. So for that you need to call as below:
yourAdapterObject.notifyDataStateChanged();
In Addition here is the Animation demo given by the developer site for showing animation. But they also shows, how to remove item from list and add it into list. All works fine with nice animation effect.
I guess it will surly help you.
Enjoy Coding... :)
Upvotes: 0
Reputation: 4649
Remove the item from the list or ArrayList belongs to the adapter according to the index of the list and then try to call notifyDataSetChanged()
Upvotes: 0