我想在單擊ImageView時從listView中刪除某一行。我的列表視圖看起來像這樣: AdapterView不支持removeView(View)
我希望當最後一個圖像被點擊刪除該行。這裏是我的適配器:
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
哪裏是我的mystake?任何想法?
不,但我認爲你做錯了。您不應將您的ClickListener寫入適配器,而是寫入您的活動(或片段)中。然後,您爲您的視圖設置一個OnItemClickListener並在此刪除您的照片。我不確定我是否清楚,但你應該看看[Cyril Mottier的博客](http://android.cyrilmottier.com/?p=525) – AMerle 2012-07-11 08:28:52
我試過這樣的解決方案(從適配器數組列表中刪除數據然後刷新),但沒有奏效。在這裏打開另一個問題:http://stackoverflow.com/questions/38250987/removing-an-item-from-the-dropdown-list-of-a-baseadapter – 2016-07-08 08:30:28