title: In addTextChangedListener inside the listview Using Custom List Adapter
body: <p>I am developing an appliction for invoice creation.for which user have to adding a list of items and set the quanity.the datas are added in a listview.for which i used an custom list adapter which extends a base adapter.for the first item if user select the qty as 15(for eg)for a item.qty as shows 15 until next item is added.if another one is added then the previously setted qty change to current one.I am using addTextChangedListener inside Custom List Adapter.when i change qty using addTextChangedListener all rows affected.I am using a Custom Adapter to fill a listview. Each row in a listview has 3 textviews(Custom Layout), and an edittext for entering the quantity.Another Problem is when scrolled the ListView the value repeats/change automatically in the edit text.why?
  <img src="https://i.stack.imgur.com/SjnqN.png" alt="enter image description here"></p>
  
  <p><img src="https://i.stack.imgur.com/zuqKK.png" alt="enter image description here"></p>
  
  <p>Here is my main class,</p>
  
  <pre><code>public class SalesTransactionFragment extends SherlockFragment {
      Button b1,b2,b3;
      TextView tvcusid,tvcusname,tvtotal;
      ListView lv;
      public static itemlistadapter itadapter;
      static ArrayList&lt;Items&gt;  item ;
      Items it;
  
      String cus_id,c_name;
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          // Get the view from fragmenttab3.xml
            View view = inflater.inflate(R.layout.salesform, container, false);
            return view;
  
      }
  
     @Override
  public void onActivityCreated(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      super.onActivityCreated(savedInstanceState);
      b1=(Button) getActivity().findViewById(R.id.sfbtncus);
      b2=(Button) getActivity().findViewById(R.id.sfbtnadd);
      b3=(Button) getActivity().findViewById(R.id.sfbtfinish);
      itadapter=null;
    it=new Items();
      tvcusid=(TextView) getActivity().findViewById(R.id.sftvcusid);
      tvcusname=(TextView) getActivity().findViewById(R.id.sftvcusname);
      tvtotal=(TextView) getActivity().findViewById(R.id.sftotal);
      lv=(ListView) getActivity().findViewById(R.id.sflvitem);
      lv.setTranscriptMode(lv.TRANSCRIPT_MODE_ALWAYS_SCROLL);
      item = new ArrayList&lt;Items&gt;();
  
      b1.setOnClickListener(new OnClickListener() {
  
          @Override
          public void onClick(View v) {
              Intent i = new Intent(getActivity(), CustomerSelect.class);
              startActivityForResult(i, 1);   
  //          Intent i = new Intent(SalesFormActivity.this, CustomerSelect.class);
  //          i.putExtra("cussel", "true");
  //          startActivityForResult(i, 0);   
          //  Toast.makeText(getActivity(), "CustomerSelect",Toast.LENGTH_LONG).show();
          }
      });
      b2.setOnClickListener(new OnClickListener() {
  
          @Override
          public void onClick(View v) {
  //          
              Intent i = new Intent(getActivity(), ItemsView.class);
              startActivityForResult(i, 0);           
  //          Toast.makeText(getActivity(), "AddItem",Toast.LENGTH_LONG).show();
  
          }
      });
      b3.setOnClickListener(new OnClickListener() {
  
          @Override
          public void onClick(View v) {
          //  Toast.makeText(getActivity(), "Finish",Toast.LENGTH_LONG).show();
  
               if(itadapter==null)
                  {
                          Toast.makeText(getActivity(), "Must select a item", Toast.LENGTH_LONG).show();
  
                  }
           else if(cus_id!=null&amp;&amp;itadapter!=null){
                  Intent i=new Intent(getActivity(), Invoice.class);
              i.putExtra("cus_id", cus_id);
  
                  i.putExtra("c_name", c_name);
  
                  startActivity(i);
  //                getActivity().finish();
  
              }
              else if (cus_id==null)
              {
                  Toast.makeText(getActivity(), "Must select a customer", Toast.LENGTH_LONG).show();
              }
  
  
  
  
          }
      });
  }
     @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      if(data!=null)
      switch (requestCode) {
  //      case 0:
  //          cus_id=data.getStringExtra("cus_id");
  //          c_name=data.getStringExtra("c_name");
  //          tvcusid.setText(cus_id);
  //          tvcusname.setText(c_name);
  //          
  //          break;
      case 0:
          it=new Items();
          it.setitemname(data.getStringExtra("name"));
          it.setprice(data.getDoubleExtra("price",0));
          it.setid(data.getIntExtra("item_id", 0));
          if(itadapter==null){
              item.add(it);
              itadapter=new itemlistadapter(item, getActivity());
              lv.setAdapter(itadapter);
              lv.setSelection(itadapter.getCount()-1);
  
          }else{
  
              itadapter.addItems(it);
          }
          if(itadapter!=null){
              tvtotal.setText(""+itadapter.gettotal());
          }
          break;
      case 1:
          cus_id=data.getStringExtra("cusid");
          c_name=data.getStringExtra("cusname");
          tvcusid.setText(cus_id);
          tvcusname.setText(c_name);
  
          break;
      default:
          break;
      }
  
  
     }
     @Override
  public void onResume() {
      super.onResume();
  }
  </code></pre>
  
  <p>Here is the adapter class,</p>
  
  <pre><code>public class itemlistadapter extends BaseAdapter implements Filterable {
  
      ArrayList&lt;Items&gt; item;
      ArrayList&lt;Items&gt; history;
      HashMap&lt;Integer, Double&gt; prices;
      Context con;
      double grantTotal = 0;
  
      public itemlistadapter(ArrayList&lt;Items&gt; item, Context con) {
          super();
          this.item = item;
          this.con = con;
          history = (ArrayList&lt;Items&gt;) item.clone();
          prices = new HashMap&lt;Integer, Double&gt;();
      }
  
      @Override
      public int getCount() {
          return item.size();
      }
  
      @Override
      public Object getItem(int position) {
          return item.get(position);
      }
  
      @Override
      public long getItemId(int position) {
          return position;
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          if (convertView == null) {
              LayoutInflater lvinflat = (LayoutInflater) con
                      .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              convertView = lvinflat.inflate(R.layout.singlesalelist, parent,
                      false);
          }
          final int pos = position;
          final TextView tvitem = (TextView) convertView
                  .findViewById(R.id.sinlvtvname);
          final TextView price = (TextView) convertView
                  .findViewById(R.id.sinlvtvprice);
  
          final EditText qty = (EditText) convertView
                  .findViewById(R.id.sinlvetqty);
          if (pos == item.size() - 1) {
              // Log.d("focusposition",
              // ""+pos+"qty.requestFocus()"+qty.requestFocus());
              qty.requestFocus();
          }
          final TextView tvtotal = (TextView) convertView
                  .findViewById(R.id.sinlvtvtotal);
          tvitem.setText(item.get(position).getitemname());
  
          price.setText("" + item.get(position).getprice());
  
          qty.setText("" + item.get(pos).getqty());
          Log.d("position", "" + pos + "qty" + item.get(position).getqty());
  
  //      if (qty.length() != 0) {
  //          int qnty = Integer.parseInt(qty.getText().toString());
  //      //  item.get(pos).setqty(qnty);
  //      //  Log.d("positionqty.length() != 0:", ""+pos+"qty"+item.get(position).getqty());
  //
  //          double unitp = Double.parseDouble(price.getText().toString());
  //          double total = (qnty * unitp);
  //          tvtotal.setText("" + total);
  //          prices.put(pos, total);
  //      } else {
  //
  //          tvtotal.setText("" + 0);
  //      }
          qty.addTextChangedListener(new TextWatcher() {
  
              @Override
              public void onTextChanged(CharSequence s, int start, int before,
                      int count) {
  
                  Log.d("On text changed", s+",Start:"+start+",count:"+count+",before:"+before);
                  if (s.length() != 0) {
                      int qnty = Integer.parseInt(s.toString());
                      item.get(pos).setqty(qnty);
                      Log.d("position in ChangedListener", ""+pos+"qty"+item.get(pos).getqty()+"qnty"+qnty);
  
                      double unitp = Double.parseDouble(price.getText()
                              .toString());
                      double total = (qnty * unitp);
                      tvtotal.setText("" + total);
                      prices.put(pos, total);
                  } else {
  
                      tvtotal.setText("" + 0);
                  }
              }
  
              @Override
              public void beforeTextChanged(CharSequence s, int start, int count,
                      int after) {
                  Log.d("Before text changed", s+",Start:"+start+",count:"+count+",after:"+after);
  
              }
  
              @Override
              public void afterTextChanged(Editable s) {
  
                  Log.d("After text changed", s+"");
              }
          });
  
          if (tvtotal.getText().equals("total")||tvtotal.getText().equals("Total"))
              tvtotal.setText(price.getText());
          try{
          prices.put(pos, Double.parseDouble(tvtotal.getText().toString()));
          }catch(NumberFormatException e){
              //TODO
          }
          return convertView;
      }
  
      public void addItems(Items i) {
          item.add(i);
          notifyDataSetChanged();
      }
  
      public ArrayList&lt;Items&gt; getItems() {
          return item;
      }
  
      public double gettotal() {
          grantTotal = 0;
          for (int i = 0; i &lt; prices.size(); i++) {
              grantTotal += prices.get(i);
          }
          return grantTotal;
      }
  
      Filter my_filter = new Filter() {
  
          @SuppressWarnings("unchecked")
          @Override
          protected void publishResults(CharSequence constraint,
                  FilterResults results) {
              item = (ArrayList&lt;Items&gt;) results.values;
              if (results.count &gt; 0) {
                  notifyDataSetChanged();
              } else {
                  notifyDataSetInvalidated();
              }
  
          }
  
          @Override
          protected FilterResults performFiltering(CharSequence constraint) {
              Log.d("insitefilterresults", "msg");
              FilterResults filter_results = new FilterResults();
              ArrayList&lt;Items&gt; arr = new ArrayList&lt;Items&gt;();
              if (constraint != null &amp;&amp; history != null) {
                  for (Items s : history) {
                      if (s.getitemname().startsWith(constraint.toString()))
                          arr.add(s);
                  }
                  filter_results.values = arr;
                  filter_results.count = arr.size();
              }
              return filter_results;
          }
      };
  
      @Override
      public Filter getFilter() {
          Log.d("insitegetFilte", "msg");
  
          // TODO Auto-generated method stub
          return my_filter;
      }
  
  }
  </code></pre>
  
  <p>LogCat is,</p>
  
  <pre><code>03-27 05:18:57.692: D/Before text changed(409): 1,Start:1,count:0,after:1
  03-27 05:18:57.692: D/Before text changed(409): 1,Start:1,count:0,after:1
  03-27 05:18:57.702: D/On text changed(409): 15,Start:1,count:1,before:0
  03-27 05:18:57.702: D/position in ChangedListener(409): 0qty15qnty15
  03-27 05:18:57.722: D/On text changed(409): 15,Start:1,count:1,before:0
  03-27 05:18:57.732: D/position in ChangedListener(409): 1qty15qnty15
  03-27 05:18:57.742: D/After text changed(409): 15
  03-27 05:18:57.742: D/After text changed(409): 15
  </code></pre>
  
  <p>Please help me solve This. Help will be appreciated.Thanks in advance.</p>