机器人:如何删除从一个ListView行与行中的删除按钮 [英] android: how to delete a row from a ListView with a delete button in the row

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

问题描述

我知道有大量的线程或多或少相同的话题,但他们没有涉及我的情况:

我用我的自定义行填充我的ListView用自定义CursorAdapter的。我有两个状态为ListView:第一个国家由行中的唯一的案文;第二个国家有每一行中删除按钮。两种状态之间的转换是在视图底部的按钮。

我尝试实现的是:在第一种状态,当我点击的行,画面应切换到其他视图。在第二状态,当我点击删除按钮选定行应该从数据库中删除,ListView控件应重新填充,并没有什么应该发生,如果我$该行不是删除按钮以外的其他任何部件P $ PSS。

我知道如何删除一个项目,并重新填充的ListView后来,我知道如何切换到当我点击第一状态行上另一种看法;但我不能将二者结合起来。我使用下面的方法来获取该行的ID和位置:

 保护无效onListItemClick(ListView的L,视图V,INT位置,长的id){
        super.onListItemClick(L,V,位置ID);
     // code去这里无论是删除或切换到另一个视图
}
 

我试图用一些if语句来获取删除按钮的状态点击或单击行,但没有奏效。

第一个问题是显而易见的:我怎样才能使它发挥作用

第二个问题是:在第二状态时,任何部分的行为pressed不是删除按钮,屏幕将切换到其他视图等。我怎样才能prevent呢?我试图设置.setClickable,.SET pressed,.setSelected行属性设置为false,但它并没有帮助。

感谢您的答复提前。

解决方案
  

在第二状态时,任何部分的行为pressed不是删除按钮,屏幕将切换到其他视图等。我怎样才能prevent呢?

只要有一个侦听deleteButton

  @覆盖
    公共查看getView(INT位置,查看convertView,父母的ViewGroup)
    {
        查看排= NULL;
        LayoutInflater充气= getLayoutInflater();

        行= inflater.inflate(R.layout.one_result_details_row,父母,假);

        //这里充气其他项目:
        按钮deleteButton =(按钮)row.findViewById(R.id.Details_Button01);
         deleteButton.setTag(位置);

        deleteButton.setOnClickListener(
            新Button.OnClickListener(){
                @覆盖
                公共无效的onClick(视图v){
                    整数指数=(整数)view.getTag();
                    items.remove(index.intValue());
                    notifyDataSetChanged();
                }
            }
        );
 

在getView()方法您标记的列表项到现在的位置。

  deleteButton.setTag(位置);
 

getTag()对象转换为整数

OnClickListener()然后删除该项目

  items.remove(index.intValue());
 

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.

解决方案

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

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

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