android:如何使用行中的删除按钮从 ListView 中删除一行 [英] android: how to delete a row from a ListView with a delete button in the row

查看:69
本文介绍了android:如何使用行中的删除按钮从 ListView 中删除一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有很多主题或多或少相同的主题,但没有一个涵盖我的情况:

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

我正在使用自定义行使用自定义 CursorAdapter 填充我的 ListView.ListView 有两个状态:第一个状态仅包含行中的文本;第二个状态在每一行都有一个删除按钮.两种状态之间的切换是视图底部的一个按钮.

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.

我试图实现的是:在第一种状态下,当我点击行时,屏幕应该切换到另一个视图.在第二种状态下,当我单击删除按钮时,应从数据库中删除所选行,应重新填充 ListView,如果我按下除删除按钮以外的行的任何其他部分,则不会发生任何事情.

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.

我知道如何删除一个项目并在之后重新填充 ListView,并且我知道当我在第一个状态下单击行时如何切换到另一个视图;但我无法将两者结合起来.我使用以下方法来获取行的 id 和位置:

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
}

我尝试使用一些 if 语句来获取单击删除按钮或单击行的条件,但没有奏效.

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?

第二个问题是:在第二个状态下,当除删除按钮之外的行的任何部分被按下时,屏幕会切换到另一个视图.我怎样才能防止这种情况?我试图将行的 .setClickable、.setPressed、.setSelected 属性设置为 false,但没有帮助.

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.

提前感谢您的回复.

推荐答案

在第二种状态下,当行的任何部分被按下时,除删除按钮屏幕外,屏幕会切换到另一个视图.我怎样才能防止这种情况?

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?

只需为 deleteButton 设置一个监听器

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

在 getView() 方法中,您将 ListItem 标记为位置

In the getView() method you tag the ListItem to postion

deleteButton.setTag(position);

getTag() 对象转换为整数

Convert the getTag() Object to an Integer

OnClickListener() 中,然后删除项目

In the OnClickListener() you then delete the item

 items.remove(index.intValue());  

这篇关于android:如何使用行中的删除按钮从 ListView 中删除一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆