简体   繁体   中英

listview itemclick set text to textview issue

In my listview, when I long press row 1 it should change the amount of a textview in row 1. However I have a problem, when I try to change the amount of the specific row (example: row 1), the 5th row of the listview also changes. Also, when the listview is recycled, the textview returns to its old value. Been trying to solve this for a day already but to no luck.

Any help is appreciated!

public View getView(int i, View convertView, ViewGroup viewGroup) {
        View mView = convertView;
        String betid = mData.get(i).get("id");
        final ViewHolder holder;
        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.checkBox = (CheckBox) mView.findViewById(R.id.checkmark);
            holder.tx_status = (TextView) mView.findViewById(R.id.tx_status);

            holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @RequiresApi(api = Build.VERSION_CODES.N)
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (buttonView.isChecked()) {
                        checked.add((Integer) holder.checkBox.getTag());
                        holder.tx_amount.setBackgroundColor(getResources().getColor(R.color.bluelim));
                        holder.tx_number.setBackgroundColor(getResources().getColor(R.color.bluelim));
                        holder.tx_status.setBackgroundColor(getResources().getColor(R.color.bluelim));
                    } else {
                        holder.tx_amount.setBackgroundColor(Color.WHITE);
                        holder.tx_number.setBackgroundColor(Color.WHITE);
                        holder.tx_status.setBackgroundColor(Color.WHITE);
                    }
                }
            });

            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 New 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 zzx = input.getText().toString();
                            txAmt.setText(zzx);
                            holder.tx_status.setText(zzx);
                            holder.tx_amount.setText(zzx);
                        }
                    });
                    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 true;
                }

            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("betamount");
            String status = mData.get(i).get("status");
            holder.tx_amount.setText(amountTarget);
            holder.tx_number.setText(betnumber);
            holder.tx_status.setText(status);
        }



        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_status;
        CheckBox checkBox;
    }
}

try this. please add this two methods if not added

@Override
public long getItemId(int position) {
    return position;
}

@Override
public int getItemViewType(int position) {
   return position;
}

Add notifyDataSetChange on positive button click listener.

 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 New 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 zzx = input.getText().toString();
                            txAmt.setText(zzx);
                            holder.tx_status.setText(zzx);
                            holder.tx_amount.setText(zzx);



adapter.notifyDataSetChanged();



                        }
                    });
                    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 true;
                }

you should learn the listview lifecycle. however if you want to do that you have to change the item data from the datasource which in this case , its mData . and after updateting the item call notifyItemchanged() on adapter to apply the item in listview. the reason for after recycling you see the old data is that the list gets data from mData and you just updated the View . so instead of geting the viewholder. get item by position in longclick and after you updated the item just call notifyItemChanged(position);

First you missed }); in your code before mView.setTag(holder); , so your code will have compile issues which I think you should have seen.

Second, you should not call listView.setOnItemLongClickListener in getView . Because this will repeat many times depends on how many items visible in your screen.

Finally, you should update the data source, ie: mData in your code, with the latest data, and then call adapter.notifyDatasetChanged() to update the UI.

So your code should be something like below:

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 New 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 zzx = input.getText().toString();
                            // Remove codes below
                            // txAmt.setText(zzx);
                            // holder.tx_status.setText(zzx);
                            // holder.tx_amount.setText(zzx);

                            mData.get(position).setXXX(zzx); // call the setter of your data model
                            adapter.notifyDatasetChanged(); // call this will update the listview with the latest data
                        }
                    });
                    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 true;
                }
                });


public View getView(int i, View convertView, ViewGroup viewGroup) {
        View mView = convertView;
        String betid = mData.get(i).get("id");
        final ViewHolder holder;
        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.checkBox = (CheckBox) mView.findViewById(R.id.checkmark);
            holder.tx_status = (TextView) mView.findViewById(R.id.tx_status);

            holder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @RequiresApi(api = Build.VERSION_CODES.N)
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (buttonView.isChecked()) {
                        checked.add((Integer) holder.checkBox.getTag());
                        holder.tx_amount.setBackgroundColor(getResources().getColor(R.color.bluelim));
                        holder.tx_number.setBackgroundColor(getResources().getColor(R.color.bluelim));
                        holder.tx_status.setBackgroundColor(getResources().getColor(R.color.bluelim));
                    } else {
                        holder.tx_amount.setBackgroundColor(Color.WHITE);
                        holder.tx_number.setBackgroundColor(Color.WHITE);
                        holder.tx_status.setBackgroundColor(Color.WHITE);
                    }
                }
            });

            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("betamount");
            String status = mData.get(i).get("status");
            holder.tx_amount.setText(amountTarget);
            holder.tx_number.setText(betnumber);
            holder.tx_status.setText(status);
        }



        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_status;
        CheckBox checkBox;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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