简体   繁体   English

listView onItemLongClick中的复选框状态

[英]Checkbox state in listview onItemLongClick

I have a listview in which when you select a row, it's checkbox becomes checked / unchecked. 我有一个列表视图,当您选择一行时,它的复选框被选中/取消选中。 However, I have a onItemLongClick that displays a dialog. 但是,我有一个onItemLongClick ,它显示一个对话框。

Problem is when I long-click a row in the listview, it becomes checked and I don't want that to happen, I just need it to display a dialog. 问题是当我长时间单击列表视图中的一行时,该行被选中并且我不希望发生这种情况,我只需要它显示一个对话框即可。 This is confusing me because the onItemClick is also called when I use onItemLongClick . 这是困惑我,因为onItemClick当我使用也被称为onItemLongClick

Here's the code for onItemClick : 这是onItemClick的代码:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @RequiresApi(api = Build.VERSION_CODES.N)
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            CheckBox checkBox = (CheckBox)view.findViewById(R.id.checkmark);
            TextView tv3 = (TextView)view.findViewById(R.id.tx_amount);
            String shitts = listView.getItemAtPosition(position).toString();
            HashMap<String, String> data = new HashMap<>();
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            try {
                checkBox.setChecked(!checkBox.isChecked());
                String[] a = shitts.split(", ");
                String[] sep = a[0].split("=");
                String betamount = sep[1];
                String[] sepx = a[2].split("=");
                String betnumber = sepx[1];
                String showbetnumber = betnumber.replaceAll("[;/:*?\"<>|&{}']","");

                if(checkBox.isChecked()){
                    hash.put(showbetnumber,tv3.getText().toString());
                }else {
                    tv3.setText(betamount);
                    checked.removeAll(Collections.singletonList(position));
                    hash.remove(showbetnumber,tv3.getText().toString());
                }
            }catch (Exception e){
            }
        }
    });

and here's the code for onItemLongClick 这是onItemLongClick的代码

listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            TextView txAmt = view.findViewById(R.id.tx_amount);
            AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
            alert.setTitle("Enter Amount:");
            final EditText input = new EditText(MainActivity.this);
            input.setInputType(InputType.TYPE_CLASS_NUMBER);
            input.setRawInputType(Configuration.KEYBOARD_12KEY);
            alert.setView(input);
            alert.setPositiveButton("enter", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    String x = input.getText().toString();
                    txAmt.setText(x);
                }
            });
            alert.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                    //Put actions for CANCEL button here, or leave in blank
                }
            });
            alert.show();
            return false;
        }
    });

Any help is appreciated! 任何帮助表示赞赏!

Create a boolean isLongClick and set it to false . 创建一个布尔isLongClick并将其设置为false

onItemLongClick() , set isLongClick to true . onItemLongClick() ,将isLongClick设置为true

On your dialog, set isLongClick to false again when any button is clicked or if the dialog was dismissed. 在对话框上,单击任何按钮或isLongClick对话框时,再次将isLongClick设置为false

Finally, wrap all your code inside onItemClick() in: 最后,将所有代码包装在onItemClick()中:

if (!isLongClick) { 
    // onItemClick() code
}

The source of this solution 该解决方案的来源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM