0

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.

kobbycoder
  • 682
  • 2
  • 10
  • 33

2 Answers2

1

add this to your adapter class

public void myRemove(int position){
  Listc.remove(position);
  myAdapter.super.notifyDataSetChanged();
}

and use this rather to see if it will make a difference

Elltz
  • 10,730
  • 4
  • 31
  • 59
0

I think in your adapter you should have an array variable like this.

class yourAdapter extends BaseAdapter  {
    private ArrayList<Object> your_list_variable; 
    ....
}

All you need to do is update this list, and then call

adapter.notifyDataSetChanged(); 

Try it.

// update here for an example, you could check my code.

public class MainActivity extends ActionBarActivity {

private ListView myList;
private myAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    mAdapter = new myAdapter(this, android.R.layout.simple_list_item_1);
    myList = (ListView) findViewById(R.id.myList);

    for(int i = 0; i < 100; i++) {
        mAdapter.add("item : " + (i+1));
    }
    myList.setAdapter(mAdapter);

    myList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int index, long id) {

            System.out.println("index : " + index);
            System.out.println("item : " + mAdapter.getItem(index));
            mAdapter.remove(mAdapter.getItem(index));
            mAdapter.notifyDataSetChanged();
            return false;
        }
    });

}


class myAdapter extends ArrayAdapter<String> {

    private List<String> listData = new ArrayList<>();


    public myAdapter(Context context, int resource) {
        super(context, resource);
    }

    @Override
    public void add(String object) {
        super.add(object);

        listData.add(object);
    }

    @Override
    public void remove(String object) {
        super.remove(object);
        listData.remove(object);
    }

    @Override
    public String getItem(int position) {
        return super.getItem(position);
    }
}

Hope this help you.

Chauyan
  • 157
  • 8
  • i already added it in the `setOnItemLongClickListener`, or what do you mean? you mean in the class? and what do you mean with update this list? – kobbycoder Apr 27 '15 at 00:23
  • I mean, you could directly call remove function of your list, like this ArrayList.remove and then use notifyDataSetChanged. – Chauyan Apr 27 '15 at 00:25
  • what type of adapter are you using? Is it ArrayAdapter? Kindly have a look at: http://stackoverflow.com/questions/18444671/remove-item-in-arrayadapterstring-in-listview – Sam Apr 27 '15 at 00:36
  • ill will take a look at that post but if you need more info about my code just tell me – kobbycoder Apr 27 '15 at 00:37