I'm having issues with listview containing checkbox, when a checkbox is unchecked and when the view gets recycled, the checked checkbox becomes checked. Any help? I've seen lots of same topic but I can't seem to find an answer to my issue. Thank you.
Here's the code of getView.
public View getView(int i, View convertView, ViewGroup viewGroup) {
View mView = convertView;
String betid = mData.get(i).get("betid");
ViewHolder holder ;
ListView listview = findViewById(R.id.lvMain);
if (mView == null) {
Context context = viewGroup.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
mView = inflater.inflate(R.layout.row_layout, null,false);
holder = new ViewHolder();
holder.tx_number = (TextView) mView.findViewById(R.id.tx_number);
holder.tx_amount = (TextView) mView.findViewById(R.id.tx_amount);
holder.tx_counter = (TextView) mView.findViewById(R.id.tx_counter);
holder.checkBox = (CheckBox) mView.findViewById(R.id.checkmark);
holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
String[] b;
if (buttonView.isChecked()) {
EditText xxx = (EditText)findViewById(R.id.editText);
String amtX = xxx.getText().toString();
int toMultiply;
if(amtX.equals("")){
toMultiply = 1;
}else{
toMultiply = Integer.parseInt(amtX);
}
System.out.println(toMultiply);
String yy = editText.getText().toString().trim();
checked.add((Integer) holder.checkBox.getTag());
String item = listview.getItemAtPosition(i).toString();
String[] a = item.split(", ");
b = a[1].split("=");
String[] sep = a[0].split("=");
String betnumber = sep[1];
String betamount= b[1];
final String sorted = betnumber.chars().sorted().mapToObj(c -> Character.valueOf((char)c).toString()).collect(Collectors.joining());
if (doubleChecker(sorted)){
answer = (Integer.parseInt(betamount) * toMultiply / 3);
holder.tx_counter.setText(valueOf(answer));
}else{
if(yy.equals("")){
answer = (Integer.parseInt(betamount) * toMultiply / 6);
holder.tx_counter.setText(valueOf(answer));
}else{
answer = ((Integer.parseInt(betamount) * toMultiply - Integer.parseInt(yy)) / 6);
holder.tx_counter.setText(valueOf(answer));
}
}
holder.tx_counter.setBackgroundColor(getResources().getColor(R.color.bluelim));
holder.tx_amount.setBackgroundColor(getResources().getColor(R.color.bluelim));
holder.tx_number.setBackgroundColor(getResources().getColor(R.color.bluelim));
}
else {
holder.tx_counter.setBackgroundColor(Color.WHITE);
holder.tx_amount.setBackgroundColor(Color.WHITE);
holder.tx_number.setBackgroundColor(Color.WHITE);
holder.tx_counter.setText("0");
checked.remove((Integer) holder.checkBox.getTag());
}
}
});
holder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
holder.checkBox.setChecked(true);
}else{
holder.checkBox.setChecked(false);
}
}
});
mView.setTag(holder);
holder.checkBox.setTag(i);
} else {
holder = (ViewHolder) mView.getTag();
((ViewHolder)mView.getTag()).checkBox.setTag(i);
}
if (betid != null) {
String betnumber = mData.get(i).get("betnumber");
String amountTarget = mData.get(i).get("amountTarget");
holder.tx_amount.setText(amountTarget);
holder.tx_number.setText(betnumber);
holder.tx_counter.setText("0");
}
ViewHolder holde2r = (ViewHolder) mView.getTag();
for (int k = 0; k < checked.size(); k++) {
if (checked.get(k) == i) {
holde2r.checkBox.setChecked(true);
}
else if (checked.get(k) != i) {
holde2r.checkBox.setChecked(false);
}
}
return mView;
}
private class ViewHolder {
TextView tx_number;
TextView tx_amount;
TextView tx_counter;
CheckBox checkBox;
}
}
Sorry for the long code, I just think that it is needed to place here because i dont knw what causes the issue.
Aucun commentaire:
Enregistrer un commentaire