title: EditText inside Listview and its reclycling issue in android
body: <p>"Don't use edittext inside a listview" this is what most of the people say.. now I am thinking should have listened to them. I had a problem earlier when I added an EditText inside a ListView and encountered a problem related to click and I also posted a question regarding this(<a href="https://stackoverflow.com/questions/32946043/edittext-and-listview-are-not-working-together">EditText and ListView are not working together</a>) and also found a solution for that in(<a href="https://stackoverflow.com/questions/20696631/focus-on-edittext-in-listview-when-block-descendants-android">Focus on EditText in ListView when block descendants (Android)</a>). After having so many obstacles I have been able to implement this, </p>
  
  <p>But now I am having a recycle issue problem, I have an addTextChangedListener for the EditText in the ListView. Most of the people are suggesting to use setOnFocusChangeListener, but that is not an option for me as i have to change the EditText value of one row depending the value entered in the EditText of the other row. 
  The problem currently I am facing is when I enter the value in the last row of the EditText, the same thing is reflecting in the first row. Can anybody help me to solve this issue.
  Here is the code snippet (Sorry for bad english) </p>
  
  <pre><code>public class MyBaseAdapter extends BaseAdapter {
      ArrayList &lt; ListData &gt; myList = new ArrayList &lt; &gt; ();
      LayoutInflater inflater;
      Activity activity;
      ListView lvDetail;
      TextView total_amount;
      TextView spendAmount;
      Button proceed;
      HashMap &lt; Integer, ListData &gt; selectedRow = new HashMap &lt; &gt; ();
  
      public MyBaseAdapter(Activity activity, ArrayList &lt; ListData &gt; myList, ListView lvDetail) {
          this.myList = myList;
          this.activity = activity;
          this.lvDetail = lvDetail;
          inflater = LayoutInflater.from(this.activity);
          total_amount = (TextView) activity.findViewById(R.id.total_amount);
          spendAmount = (TextView) activity.findViewById(R.id.spendAmount);
          proceed = (Button) activity.findViewById(R.id.proceed);
  
      }
  
      @Override
      public int getCount() {
          return myList.size();
      }
  
      @Override
      public Object getItem(int position) {
          return myList.get(position);
      }
  
      @Override
      public long getItemId(int position) {
          return 0;
      }
      public void setItem(int position, ListData data) {
  
          myList.set(position, data);
      }@Override
      public View getView(final int position, View convertView, ViewGroup parent) {
          final MyViewHolder mViewHolder;
          final ListData currentListData = (ListData) getItem(position);
          if (convertView == null) {
              convertView = inflater.inflate(R.layout.layout_list_item, parent, false);
              mViewHolder = new MyViewHolder(convertView);
              convertView.setTag(mViewHolder);
  
          } else {
              mViewHolder = (MyViewHolder) convertView.getTag();
              mViewHolder.enterAmount.setText("");
              mViewHolder.enterAmount.setVisibility(View.INVISIBLE);
              mViewHolder.tvDesc.setText("");
              mViewHolder.tvTitle.setText("");
              mViewHolder.ivIcon.setImageResource(R.drawable.ic_launcher_1);
          }
  
          if (selectedRow.containsKey(position)) {
  
  
              mViewHolder.enterAmount.setVisibility(View.VISIBLE);
          mViewHolder.enterAmount.setText(currentListData.getEditTextValue());
              mViewHolder.ivIcon.setImageResource(R.drawable.ic_check_orange);
  
          } else if (currentListData.getEditTextValue().toString().length() == 0) {
              mViewHolder.enterAmount.setVisibility(View.INVISIBLE);
              mViewHolder.enterAmount.setText(currentListData.getEditTextValue());
              mViewHolder.ivIcon.setImageResource(currentListData.getImgResId());
  
          }
  
          mViewHolder.tvTitle.setText(currentListData.getTitle());
          mViewHolder.tvDesc.setText(currentListData.getDescription());
  
          mViewHolder.enterAmount.addTextChangedListener(new TextWatcher() {
  
              String enterAmount, enterDescption;@Override
              public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  
              }
  
              @Override
              public void onTextChanged(CharSequence s, int start, int before, int count) {
                  enterAmount = s.toString();
                  enterDescption = mViewHolder.tvDesc.getText().toString().trim();
  
                  if ((count == 1 &amp;&amp; start == 0) &amp;&amp; (before == 1)) {
  
                      mViewHolder.enterAmount.setText("");
                      currentListData.setEditTextValue("");
                      setItem(position, currentListData);
  
                  }
              }
  
              @Override
              public void afterTextChanged(Editable s) {
                  String[] separated;
                  int totalBal = 0;
  
                  if ((enterAmount.length() != 0 &amp;&amp; enterDescption.length() != 0)) {
  
                      separated = enterDescption.split(":");
                      totalBal = Integer.parseInt(separated[1]);
  
                      if ((Integer.parseInt(enterAmount) &gt; totalBal)) {
  
                          mViewHolder.enterAmount.setText("");
                          currentListData.setEditTextValue("");
                          setItem(position, currentListData);
  
                          new AlertDialog.Builder(activity)
                              .setTitle("Alert")
                              .setMessage("You do not have sufficient balance")
                              .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                              public void onClick(DialogInterface dialog, int which) {
  
                                  dialog.dismiss();
                              }
                          })
                              .setIcon(android.R.drawable.ic_dialog_alert)
                              .setCancelable(false)
                              .show();
  
  
                      } else if (Integer.parseInt(enterAmount) &gt;= 0) {
                          System.out.println(position + "positioninside else if enterAmount:" + Integer.parseInt(enterAmount));
                          currentListData.setEditTextValue(enterAmount);
                          setItem(position, currentListData);
  
                      }
  
                  }
  
              }
          });
  
          convertView.setOnClickListener(new View.OnClickListener() {@Override
              public void onClick(View view) {
                  int i = selectedRow.size();
                  if (selectedRow.containsKey(position)) {
                      selectedRow.remove(position);
                      mViewHolder.enterAmount.setVisibility(View.INVISIBLE);
                      mViewHolder.enterAmount.setText("");
                  mViewHolder.ivIcon.setImageResource(R.drawable.ic_launcher1);
                  } else if (i &lt; 2) {
                      ListData currentListData = (ListData) getItem(position);
                      selectedRow.put(position, currentListData);
                      mViewHolder.enterAmount.setVisibility(View.VISIBLE);
                  mViewHolder.ivIcon.setImageResource(R.drawable.ic_check_orange);
                  } else if (i &gt;= 2) {
                      Toast.makeText(activity, "Select only two cards", Toast.LENGTH_SHORT).show();
                  }
              }
          }
  
          );
  
          return convertView;
      }
  
      private static class MyViewHolder {
          TextView tvTitle, tvDesc;
          ImageView ivIcon;
          EditText enterAmount;
  
          public MyViewHolder(View item) {
              tvTitle = (TextView) item.findViewById(R.id.tvTitle);
              tvDesc = (TextView) item.findViewById(R.id.tvDesc);
              ivIcon = (ImageView) item.findViewById(R.id.ivIcon);
              enterAmount = (EditText) item.findViewById(R.id.edittext);
  
          }
      }
  }
  </code></pre>