2

I have Fragment...

    public class MyFragment extends Fragment {...

which calls adapter...

    MyArrayAdapter adapter = new MyArrayAdapter...
    my.setAdapter(adapter);

adapter has image for delete item. I can listen image click but cannot remove item from listview which is in fragment.

public class MyArrayAdapter extends ArrayAdapter<String> {...

public View getView(final int position, View convertView, ViewGroup parent) {...

    ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
    imageView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
        // ???????????
        ...}
Arnes
  • 403
  • 1
  • 5
  • 20

4 Answers4

2

Because your click is inside an OnClickListener inner class you must specify MyArrayAdapter.this to access the functions of the adapter from inside the click listener. If you just use this you will be referring to the OnClickListener and will only have access to methods on that class.

Like so:

public View getView(final int position, View convertView, ViewGroup parent) {

    ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
    imageView.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            MyArrayAdapter.this.remove(getItem(position));
            MyArrayAdapter.this.notifyDataSetChanged();
        }
}
Bobbake4
  • 24,509
  • 9
  • 59
  • 94
  • this one crashes the app, also :( – Arnes Feb 27 '15 at 20:44
  • 02-27 21:25:31.781: E/AndroidRuntime(11332): FATAL EXCEPTION: main 02-27 21:25:31.781: E/AndroidRuntime(11332): java.lang.UnsupportedOperationException 02-27 21:25:31.781: E/AndroidRuntime(11332): at java.util.AbstractList.remove(AbstractList.java:638) 02-27 21:25:31.781: E/AndroidRuntime(11332): at java.util.AbstractList$SimpleListIterator.remove(AbstractList.java:75) 02-27 21:25:31.781: E/AndroidRuntime(11332): at java.util.AbstractCollection.remove(AbstractCollection.java:229) 02-27 21:25:31.781: E/AndroidRuntime(11332): at android.widget.ArrayAdapter.remove(ArrayAdapter.java:244) – Arnes Feb 27 '15 at 21:27
  • It's probably because you initialize your adapter with an array ie: String[] instead of a list ie: ArrayList. Without seeing the entire MyArrayAdapter constructor it's hard to tell. – Bobbake4 Feb 28 '15 at 02:44
0

Has been a long time since I did Java, but as far I can remember you can use this:

this.remove(position);
this.notifyDataSetChanged();
  • This is the current object, you are calling a static that might or might not be initialized yet, probably not. – laurens Feb 28 '15 at 18:21
0

You could write a method inside your adapter:

private boolean removeItemByPosition(int position){
    try{
        items.remove(position);
        this.notifyDataSetChanged();
        return true;
    } catch (Exception e){
        e.printStackTrace();
        return false;
    }   
}

In some cases, id the notifyDataSetChanged() method doesn't work, try with re-setting the ListView Adapter, you shuould be able to do this inside the Fragment.

azurh
  • 410
  • 4
  • 12
-1

simply call convertView.setVisibility(View.GONE);

OWADVL
  • 10,704
  • 7
  • 55
  • 67