11

I'm having some troubles while trying to remove an item from the list view on long click. Below is the code:

public class MListViewActivity extends ListActivity {

private ListView lv;
private String[] some_data = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    some_data = getResources().getStringArray(R.array.mdata);

    // Bind resources Array to ListAdapter
    ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this,
            R.layout.list_item, R.id.label, some_data);
    this.setListAdapter(myAdapter);

    lv = getListView();
    lv.setDividerHeight(3);

    lv.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view,
                int arg2, long arg3) {

            // Can't manage to remove an item here

            return false;
        }
    });
}

Any help is appreciated

2

4 Answers 4

15

You shouldn't use Arrays, you should use ArrayList to remove and add items to a Listview.

Once the Array size is declared you can modify the data in particular index but cannot remove the items or add to it.

So Take an ArrayList and just when you long click on the ListView Item, just call remove method of Arraylist and notify the data set changed.

Example:

ArrayList<String> al = new ArrayList<String>();

inside your longclick write the below code to remove item.

al.remove(arg2);//where arg2 is position of item you click
myAdapter.notifyDataSetChanged();
12

try

lv.setOnItemLongClickListener(new OnItemLongClickListener() {

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view,
            int position, long arg3) {

              myAdapter.remove(some_data[position]);
              myAdapter.notifyDataSetChanged();

        return false;
    }

});
2
  • throws an UnsupportedOperationException
    – Droidman
    Jan 15, 2013 at 15:27
  • exactly was I was looking for. If you are using a ListView or any subclass of it, use the method above to get the held item from the list
    – Clocker
    Mar 27, 2015 at 2:35
0

I had issues using this method. and i solved it using this.

            listStat.remove(listStat.get(arg2));
            lvStat.requestLayout();
            adapterStat.notifyDataSetChanged();

I think this will help to others.

3
  • I'm having a similar issue. What is lvStat here in your code ?
    – codrp
    Jun 18, 2017 at 18:06
  • @pc listview reference Jun 18, 2017 at 18:08
  • I have VideoView, ImageView and TextView in my ListView. Its working for ImageView and TextView but not for VideoView
    – codrp
    Jun 18, 2017 at 18:14
0
itemAdapter = new ArrayAdapter<String>(
        view.getContext(),
        androidx.appcompat.R.layout.support_simple_spinner_dropdown_item,
        itemViewModel.getItems().getValue().stream().map(TaskCard::getTitle).collect(Collectors.toList())
);
ListView listview = view.findViewById(R.id.item_listview);
listview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int i, long l) {
        itemAdapter.remove(itemAdapter.getItem(i));
        return true;
    }
});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.