android: how to delete a row from a ListView with a delete button in the row
In second state when any part of the row is pressed other than delete button screen switches to the other view. How can I prevent this?
Simply have a listener for the deleteButton
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = null;
LayoutInflater inflater = getLayoutInflater();
row = inflater.inflate(R.layout.one_result_details_row, parent, false);
// inflate other items here :
Button deleteButton = (Button) row.findViewById(R.id.Details_Button01);
deleteButton.setTag(position);
deleteButton.setOnClickListener(
new Button.OnClickListener() {
@Override
public void onClick(View v) {
Integer index = (Integer) view.getTag();
items.remove(index.intValue());
notifyDataSetChanged();
}
}
);
In the getView() method you tag the ListItem to postion
deleteButton.setTag(position);
Convert the getTag()
Object to an Integer
In the OnClickListener()
you then delete the item
items.remove(index.intValue());