I have a listView which I am populating using custom adapter.Now the issue is that I have an editText inside ListView and on the textchange event of that editText I am trying to update model and display new values but which ever listitem editText I change it always gets reflected in first listItem I dont why. My getview code is like this:
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;
}
My model is like this:
/**
* 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;
}
}
You have to create custom class for listener:-
And to use this add this line :-