【问题标题】:Getting the position of a checkbox in a listview获取列表视图中复选框的位置
【发布时间】:2013-02-17 06:06:25
【问题描述】:

我有一个 lisview,其中每一行有三个文本框和一个复选框,我使用以下代码 sn-p 来获取选中复选框的确切位置,它对我很有效,但现在我想不要把它放在listView.setOnItemClickListener 因为我想通过一个按钮而不是通过单击列表视图的项目或行来删除选中的项目,但是我不知道将这些代码放在哪里来实现我的目标,我会提前感谢您的关注和建议。

listView.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {

        CheckBox cbx = (CheckBox)view.findViewById(R.id.ChkOrder);

            int firstPosition = listView.getFirstVisiblePosition();
            for(int i=firstPosition;i<listView.getCount();i++){
            View v=listView.getChildAt(i);
            cbx = (CheckBox)v.findViewById(R.id.ChkOrder);
            if(cbx.isChecked()){

                 Toast.makeText(getApplicationContext(), 
                                 "Checked position " + goods.get(i), 
                                  Toast.LENGTH_SHORT).show();
                 checkedItemPosition=i;


           }
        }

     }

});

【问题讨论】:

    标签: android listview checkbox onitemclicklistener


    【解决方案1】:

    将其放入您的首选按钮并将视图更改为您在代码中声明的listview 即可解决此问题:

    Button Delete=(Button)findViewById(R.id.DeleteButton);
           DeleteGoods.setOnClickListener(new OnClickListener() {
    
                public void onClick(View v) {
    
                    /** Getting the position of the currently selected item**/
                    //////////////////////////////////
                     CheckBox cbx = (CheckBox)listView.findViewById(R.id.ChkOrder);
    
                        int firstPosition = listView.getFirstVisiblePosition();
                        for(int i=firstPosition;i<listView.getCount();i++){
                        View v1=listView.getChildAt(i);
                        cbx = (CheckBox)v1.findViewById(R.id.ChkOrder);
                        if(cbx.isChecked()){
    
                            // Toast.makeText(getApplicationContext(), 
                                   //          "Checked position " + goods.get(i), 
                                    //          Toast.LENGTH_SHORT).show();
                             checkedItemPosition=i;
                        }
                        }
    

    【讨论】:

      猜你喜欢
      • 2014-09-03
      • 2012-05-03
      • 1970-01-01
      • 2013-02-24
      • 1970-01-01
      • 2017-01-03
      • 2017-04-18
      • 2014-01-09
      • 2017-07-01
      相关资源
      最近更新 更多