且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

android:如何使用行中的删除按钮从 ListView 中删除一行

更新时间:2022-01-13 06:58:34

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

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