removeView(View) is not supported in AdapterView

25,323

Solution 1

You don't have to remove the View but remove items in your photos list.

photos.remove(yourPhoto);
notifyDataSetChanged();

Moreover, you should use ViewHolders, there is a lot of tuts in Google. Hope this will help you.

Solution 2

In onItemClick(AdapterView parent, View view, int position, long id) use

parent.removeViewInLayout(view);

instead of

parent.removeViewAt(position);
Share:
25,323
Gabrielle
Author by

Gabrielle

Updated on July 09, 2022

Comments

  • Gabrielle
    Gabrielle almost 2 years

    I want to delete a certain row from a listView when an ImageView is clicked. My listview looks like this : enter image description here

    I want that when the last image is clicked to delete that row. Here is my adapter :

    public class UserItemAdapter extends ArrayAdapter<Photos.Record> {
            private ArrayList<Photos.Record> photos;
    
            public UserItemAdapter(Context context, int textViewResourceId, ArrayList<Photos.Record> photos) {
                super(context, textViewResourceId, photos);
                this.photos = photos;
            }
    
            @Override
            public View getView(final int position, View convertView, ViewGroup parent) {
                View v = convertView;
    
    
                if (v == null) {
                    LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.photorowlist, null);
                                    v.setClickable(true);
                                    v.setFocusable(true);
                }
    
                Photos.Record user = photos.get(position);
                if (user != null) {
                    TextView photo_name = (TextView) v.findViewById(R.id.photoname);
    
                    if (photo_name != null) {
                        photo_name.setText(user.photo_name);
                    }
    
                }
                            v.setOnClickListener(new OnClickListener() {
    
                            @Override
                            public void onClick(View view) {
                                //Toast.makeText(view.getContext(), "Clicked", Toast.LENGTH_SHORT).show();
    
                                ImageView delete_photo = (ImageView) view.findViewById(R.id.deletephoto);
                                delete_photo.setOnClickListener(new OnClickListener(){  
                                @Override  
                                public void onClick(View v) {  
                                    Toast.makeText(Photos.this, "Delete Button Clicked", Toast.LENGTH_SHORT).show();  
                                    listView.removeView(v);
                                    myadapter.notifyDataSetChanged();
    
    
                  }});
                             }
    
                            });
    
    
                return v;
            }
        }
    
        public class Record {
            public String photo_name;
    
            public Record(String photo_name) {
                this.photo_name = photo_name;
            }
        }
    

    I tried to delete the row using this :

    listView.removeView(v);
    myadapter.notifyDataSetChanged();
    

    and I get the error : ERROR AndroidRuntime java.lang.UnsupportedOperationException: removeView(View) is not supported in AdapterView

    Where is my mystake? Any idea?

  • AMerle
    AMerle almost 12 years
    No, but I think you're doing the wrong way. You should not write your ClickListener in your adapter but in your activity (or fragment). Then you set an OnItemClickListener for your view and delete your photo here. I'm not sure that I am clear, but you should have a look on Cyril Mottier's blog
  • Marco Zanetti
    Marco Zanetti almost 8 years
    I tried such solution (removing data from the adapter arraylist and then refreshing) but didn't work. Opened another issue here: stackoverflow.com/questions/38250987/…