11

单击 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: removeView(View) is not supported in AdapterView

我的赌注在哪里?任何的想法?

4

3 回答 3

25

您不必删除View但删除照片列表中的项目。

photos.remove(yourPhoto);
notifyDataSetChanged();

此外,你应该使用ViewHolders,谷歌有很多 tuts。希望这会帮助你。

于 2012-07-11T08:09:25.503 回答
11

在 onItemClick(AdapterView parent, View view, int position, long id) 使用

parent.removeViewInLayout(view);

代替

parent.removeViewAt(position);
于 2013-12-09T14:45:19.040 回答
0

您应该在 UI 线程上尝试它,以便它立即工作:

     listView.remove(position);
        runOnUiThread(new Runnable() {
            public void run() {
                adapter.notifyDataSetChanged();
            }
        });
于 2012-07-11T08:43:28.507 回答