列表视图中的复选框状态 onItemLongClick
Checkbox state in listview onItemLongClick
我有一个列表视图,当您 select 一行时,它的复选框变为选中/未选中状态。但是,我有一个显示对话框的 onItemLongClick
。
问题是当我长按列表视图中的一行时,它会变成选中状态,我不希望这种情况发生,我只需要它来显示一个对话框。这让我感到困惑,因为当我使用 onItemLongClick
.
时也会调用 onItemClick
Here's the code for 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
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;
}
});
感谢任何帮助!
创建一个布尔值 isLongClick
并将其设置为 false
。
onItemLongClick()
,将isLongClick
设为true
。
在您的对话框中,当单击任何按钮或关闭对话框时,再次将 isLongClick
设置为 false
。
最后,将所有代码包装在 onItemClick()
中:
if (!isLongClick) {
// onItemClick() code
}
The source of this solution
我有一个列表视图,当您 select 一行时,它的复选框变为选中/未选中状态。但是,我有一个显示对话框的 onItemLongClick
。
问题是当我长按列表视图中的一行时,它会变成选中状态,我不希望这种情况发生,我只需要它来显示一个对话框。这让我感到困惑,因为当我使用 onItemLongClick
.
onItemClick
Here's the code for
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
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;
}
});
感谢任何帮助!
创建一个布尔值 isLongClick
并将其设置为 false
。
onItemLongClick()
,将isLongClick
设为true
。
在您的对话框中,当单击任何按钮或关闭对话框时,再次将 isLongClick
设置为 false
。
最后,将所有代码包装在 onItemClick()
中:
if (!isLongClick) {
// onItemClick() code
}
The source of this solution