Android AdapterView 不支持 removeView(View)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11428168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
removeView(View) is not supported in AdapterView
提问by Gabrielle
I want to delete a certain row from a listView when an ImageView is clicked. My listview looks like this :
我想在单击 ImageView 时从 listView 中删除某一行。我的列表视图如下所示:
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
我得到错误: ERROR AndroidRuntime java.lang.UnsupportedOperationException: removeView(View) is not supported in AdapterView
Where is my mystake? Any idea?
我的赌注在哪里?任何的想法?
回答by AMerle
You don't have to remove the View
but remove items in your photos list.
您不必删除View
照片列表中的项目,但要删除项目。
photos.remove(yourPhoto);
notifyDataSetChanged();
Moreover, you should use ViewHolders
, there is a lot of tuts in Google.
Hope this will help you.
此外,您应该使用ViewHolders
,Google 中有很多 tuts。希望这会帮助你。
回答by macio.Jun
In onItemClick(AdapterView parent, View view, int position, long id) use
在 onItemClick(AdapterView parent, View view, int position, long id) 中使用
parent.removeViewInLayout(view);
parent.removeViewInLayout(view);
instead of
代替
parent.removeViewAt(position);
回答by Yogesh Somani
You should try it on the UI thread so that it works instantaneously:
您应该在 UI 线程上尝试它,以便它立即工作:
listView.remove(position);
runOnUiThread(new Runnable() {
public void run() {
adapter.notifyDataSetChanged();
}
});