I want to delete a certain row from a listView when an ImageView is clicked. My listview looks like this :
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?
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.
In onItemClick(AdapterView parent, View view, int position, long id) use
parent.removeViewInLayout(view);
instead of
parent.removeViewAt(position);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With