Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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());  

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...