我有一个listView,我使用自定义适配器进行填充。现在的问题是我在ListView内部有一个editText,并且在editText的textchange事件中我试图更新模型并显示新值但是哪个listitem editText I改变它总是反映在第一个listItem我不知道为什么。我的getview代码是这样的:
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.list_style_task
, parent, false);
}
p = getProduct(position);
tvAQuantity=((TextView) view.findViewById(R.id.tvAQuantity )) ;
((TextView) view.findViewById(R.id.tvMaterial )).setText(p.getMName() );
((TextView) view.findViewById(R.id.tvTask )).setText(p.getTName() );
tvBQuantity=((TextView) view.findViewById(R.id.tvBQuantity )) ;
tvBQuantity.setText(p.getBQ());
etQuantity=(EditText)view.findViewById(R.id.etTaskQuantity);
tvAQuantity.setText(p.getAQ());
etQuantity.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if(!s.equals(""))
{
if (Integer.parseInt(s.toString()) <= Integer.parseInt(p.getBQ().toString())) {
String str= s.toString();
p.setAQ(str);
notifyDataSetChanged();
} else {
p.setAQ(p.getBQ().toString());
notifyDataSetChanged();
}
}
}
});
// CheckBox cbBuy = (CheckBox) view.findViewById(R.id.checkbox);
//cbBuy.setOnCheckedChangeListener(myCheckChangList);
// cbBuy.setTag(position);
// cbBuy.setChecked(p.selected);
return view;
}
我的模型是这样的:
/**
* Created by Mubashir.gul on 29/05/2015.
*/
public class LItem {
private String MName = "";
private String MNo = "";
private String TName = "";
private String TNo = "";
private String BQ = "";
private String AQ="";
public LItem () {
MName = "";
MNo = "";
TName = "";
TNo = "";
BQ = "";
AQ="";
}
public LItem (String _MName, String _MNo,String _TName, String _TNo,String _BQ,String _AQuantity) {
MName = _MName;
MNo = _MNo;
TName = _TName;
TNo = _TNo;
BQ = _BQ;
AQ=_AQuantity;
}
@ Override
public String toString () {// Why should override toString ()? Because the adapter display data if the incoming adapter object is not a string of case, directly on the use of the object. ToString ()
// TODO Auto-generated method stub
return MName;
}
public String getMName () {
return MName;
}
public String getMNo () {
return MNo;
}
public String getTName () {
return TName;
}
public String getTNo () {
return TNo;
}
public String getBQ () {
return BQ;
}
public void setAQ(String Aq)
{
this.AQ=Aq;
}
public String getAQ()
{
return this.AQ;
}
}
答案 0 :(得分:0)
更改
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.list_style_task
, parent, false);
}
到
View view = convertView;
view = lInflater.inflate(R.layout.list_style_task
, parent, false);
我知道这不是一个好方法,但你的观点正在缓存,这就是为什么它在第一和第二项上显示相同的值。
答案 1 :(得分:0)
您必须为侦听器创建自定义类: -
private class CustomTextWatcher implements TextWatcher {
private EditText mEditText;
public CustomTextWatcher(EditText et) {
mEditText = et;
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
public void afterTextChanged(Editable s) {
int pos = (Integer)exList.getTag();
p = getProduct(pos);
if (!s.equals("")) {
if (Integer.parseInt(s.toString()) <= Integer.parseInt(p
.getBQ().toString())) {
String str = s.toString();
p.setAQ(str);
notifyDataSetChanged();
} else {
p.setAQ(p.getBQ().toString());
notifyDataSetChanged();
}
}
}
}
要使用此添加此行: -
editText.setTag(position);
editText.addTextChangedListener(new CustomTextWatcher(editText));