Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

removeView(View) is not supported in AdapterView

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?

like image 889
Gabrielle Avatar asked Jul 11 '12 08:07

Gabrielle


2 Answers

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.

like image 149
AMerle Avatar answered Sep 23 '22 20:09

AMerle


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

parent.removeViewInLayout(view);

instead of

parent.removeViewAt(position);
like image 31
macio.Jun Avatar answered Sep 21 '22 20:09

macio.Jun