我有一个ListView,其中每一行都有一个TextView
,一个EditText
视图和两个按钮。这两个按钮用于增加和减少EditText
的值。这些按钮工作正常,但是当我编辑第一行的EditText
的值时,它会更改所有行的EditTexts
的值,然后当我按下按钮时,它会增加所有行的值。这是我的监听器,正在引起问题
viewHolder.supplyQuantity.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
if(viewHolder.supplyQuantity.getText().toString()!="") {
childVaccinationArrayList.get(position).setQuantity(Integer.parseInt(viewHolder.supplyQuantity.getText().toString()));
notifyDataSetChanged();
Log.d("list", textMap.toString());
}
}
});
适配器代码
public class LHWSupplyListAdapter extends ArrayAdapter<SpinnerData> {
List<SpinnerData> childVaccinationArrayList;
Context context;
public LHWSupplyListAdapter(List<SpinnerData> arrayList, Context context) {
super(context, R.layout.item_supplies_to_given_to_lhw, arrayList);
this.childVaccinationArrayList = arrayList;
this.context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
SpinnerData suppliesModel = getItem(position);
final LHWSupplyListAdapter.ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new LHWSupplyListAdapter.ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.item_supplies_to_given_to_lhw, parent, false);
viewHolder.supplyName = convertView.findViewById(R.id.SupplyName);
viewHolder.supplyQuantity = convertView.findViewById(R.id.Quantity);
viewHolder.btnAdd = convertView.findViewById(R.id.AddQuantity);
viewHolder.btnRemove = convertView.findViewById(R.id.RemoveQuantity);
convertView.setTag(viewHolder);
} else {
viewHolder = (LHWSupplyListAdapter.ViewHolder) convertView.getTag();
}
viewHolder.supplyName.setText(childVaccinationArrayList.get(position).getItem());
viewHolder.supplyQuantity.setText(String.valueOf(childVaccinationArrayList.get(position).getQuantity()));
viewHolder.btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
childVaccinationArrayList.get(position).setQuantity(Integer.parseInt(viewHolder.supplyQuantity.getText().toString())+1);
notifyDataSetChanged();
}
});
viewHolder.btnRemove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (childVaccinationArrayList.get(position).getQuantity() > 0 && Integer.parseInt(viewHolder.supplyQuantity.getText().toString()) > 0) {
childVaccinationArrayList.get(position).setQuantity(Integer.parseInt(viewHolder.supplyQuantity.getText().toString()) - 1);
notifyDataSetChanged();
}
}
});
viewHolder.supplyQuantity.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
if(viewHolder.supplyQuantity.getText().toString()!="") {
childVaccinationArrayList.get(position).setQuantity(Integer.parseInt(viewHolder.supplyQuantity.getText().toString()));
notifyDataSetChanged();
}
}
});
return convertView;
}
private static class ViewHolder {
TextView supplyName;
EditText supplyQuantity;
ImageView btnAdd, btnRemove;
}
}
答案 0 :(得分:0)
您的适配器中有监听器吗?
您可以在这里输入适配器代码吗?
答案 1 :(得分:0)
您应更改以下代码
您需要在按钮的点击侦听器中复制以下代码
viewHolder.supplyQuantity.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
}
@Override
public void afterTextChanged(Editable arg0) {
if(viewHolder.supplyQuantity.getText().toString()!="") {
childVaccinationArrayList.get(position).setQuantity(Integer.parseInt(viewHolder.supplyQuantity.getText().toString()));
notifyDataSetChanged();
}
}
});