I have created a class adapter in a separate file but on my main file i am calling the remove method to delete an item or what ever item i select , but when i remove it, it doesnt update on my app.
CODE:
public static void populateChatListView(final Context context){
adapter = new myAdapter(context, R.layout.source_contact);
listv.setAdapter(adapter);
for (Contacts friend: friends){
adapter.add(friend);
}
listv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(context, "DELETED", Toast.LENGTH_SHORT).show();
//supposed to remove it
adapter.remove(adapter.getItem(0));
//supposed to update it(notify)
adapter.notifyDataSetChanged();
return true;
}
});
adapter.registerDataSetObserver(new DataSetObserver() {
@Override
public void onChanged() {
super.onChanged();
listv.setSelection(adapter.getCount() - 1);
}
});
}
It doesn't give me any error and it doesn't remove it. I would appreciate any help.
CLASS ADAPTER:
public class myAdapter extends ArrayAdapter<Contacts>{
private List<Contacts> Listc = new ArrayList<>();
TextView NameWindow, LastN;
ImageView contactProfile;
Context context;
public myAdapter(Context context, int resource) {
super(context, resource);
this.context = context;
}
@Override
public void add(Contacts object) {
Listc.add(object);
super.add(object);
}
@Override
public Contacts getItem(int position) {
return Listc.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.source_contact, parent, false);
}
NameWindow = (TextView) convertView.findViewById(R.id.contactN);
LastN = (TextView) convertView.findViewById(R.id.contactL);
contactProfile = (ImageView) convertView.findViewById(R.id.contactProfile);
String name, lastn;
Contacts provider = getItem(position);
name = provider.getName(); // name goes down
lastn = provider.getLastn(); // goes upper
NameWindow.setText(name);
LastN.setText(lastMessage);
return convertView;
}
...
EDITED.