Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android: how to delete a row from a ListView with a delete button in the row

I know there are lots of threads with more or less same topic but none of them covers my situation:

I am populating my ListView with a custom CursorAdapter by using my custom rows. I have two states for the ListView: The first state consists of only texts in the rows; the second state has a delete button in every row. The switch between two states is a button at the bottom of the view.

What I try to achieve is: In first state, when I click on the row, the screen should switch to another view. In second state, when I click on delete button the selected row should be deleted from database, the ListView should be repopulated, and nothing should happen if I press on any other part of the row other than delete button.

I know how to delete an item and repopulate the ListView afterwards and I know how to switch to another view when I click on the row in first state; but I could not combine the two. I am using following method to get the id and position of the row:

protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
     //Code goes in here either for deleting or switching into another view
}

I tried to use some if statements to get the condition for delete button clicked or for row clicked but it did not work.

The first question is obvious: How can I make it work?

The second question is: 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? I tried to set .setClickable, .setPressed, .setSelected attributes of the row to false but it did not help.

Thank you for your responses in advance.

like image 464
Engin Yapici Avatar asked Oct 20 '11 05:10

Engin Yapici


1 Answers

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());  
like image 126
Reno Avatar answered Sep 28 '22 18:09

Reno