簡體   English   中英

獲取復選框在列表視圖中的位置

[英]Getting the position of a checkbox in a listview

我有一個lisview,其中每行有三個文本框和一個復選框,我使用以下代碼段獲取了已選中復選框的確切位置,它對我來說很好用,但是現在我不想將其放在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;


           }
        }

     }

});

通過將其放在您的首選按鈕中,並將視圖更改為您在代碼中聲明的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;
                    }
                    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM