AdapterView中不支持android – removeView(View)

前端之家收集整理的这篇文章主要介绍了AdapterView中不支持android – removeView(View)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
当单击 ImageView时,我想从listView中删除某行.我的清单看起来像这样:

我想要的是当最后一个图像被点击删除该行.这是我的适配器

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;
        }
    }

我试图用这个删除行:

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

我收到错误:ERROR AndroidRuntime java.lang.UnsupportedOperationException:AdapterView不支持removeView(View)

我的秘密在哪里?任何想法?

解决方法

您不必删除“查看”,而是删除照片列表中的项目.
photos.remove(yourPhoto);
notifyDataSetChanged();

此外,您应该使用ViewHolders,Google中有很多提示.希望这将有助于您.

猜你在找的Android相关文章