Android :EditText inside ListView always update first item in the listview

532 views Asked by At

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;

    }
}
2

There are 2 answers

3
NehaK On

You have to create custom class for listener:-

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();

            }
        }

    }
}

And to use this add this line :-

    editText.setTag(position);
    editText.addTextChangedListener(new CustomTextWatcher(editText));
0
Renato Probst On

Change

 View view = convertView;
    if (view == null) {
        view = lInflater.inflate(R.layout.list_style_task
                , parent, false);
    }

to

 View view = convertView;
        view = lInflater.inflate(R.layout.list_style_task
                , parent, false);

I know this is not a good approach, but your view is beeing cached, thats why its showing the same values on first and second item.