Gincy
Gincy

Reputation: 36

Setting values into edittext in a list view

I am using a custom list view which contains two Buttons Yes or No. When I clicked on No button, a layout containing an edit text will be displayed. There are more than 15 items in the list view. When I tried to type and save the value in the edittext of the 1st item using TextWatcher(), then the value is save for both 5th and 1st position ie, the same value is saving in both 1st and 5th position. The code I am using is:

    holder.subQuestionAnswer.addTextChangedListener( new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            questionsModel.get(position).getQuestion_subquestion().get(0).setAnswer(holder.subQuestionAnswer.getText()
                    .toString());   

            Log.e("value", "no_active<>"+position+"<>"+questionsModel.get(position).getQuestion_subquestion().get(0).getAnswer().toString());
        }
    });
}

How can i avoid this?

Upvotes: 0

Views: 1016

Answers (1)

iZBasit
iZBasit

Reputation: 1313

It is because of recycling of view. Please check the following adapter. Use the logic according to your needs.

public class MultiSelectionProductAdapter extends ArrayAdapter<ProductListBean> {

private Context context;
private ArrayList<ProductListBean> productList;
private ArrayList<ProductListBean> mOriginalValues;
private LayoutInflater li;


public MultiSelectionProductAdapter(Context ctx, int simpleListItem1,
                                    ArrayList<ProductListBean> data) {
    super(ctx, simpleListItem1, data);

    this.context = ctx;
    this.productList = data;
    li = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override
public int getCount() {
    return productList.size();
}

@Override
public ProductListBean getItem(int position) {
    return productList.get(position);
}

@Override
public long getItemId(int position) {

    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    final ViewHolder holder;

    if (convertView == null) {

        convertView = li.inflate(R.layout.component_inventory_prod_list, null);
        holder = new ViewHolder();

        holder.tvProductName = (TextView) convertView.findViewById(R.id.tvProductName);
        holder.tvProdShortCode = (TextView) convertView.findViewById(R.id.tvShortName);
        holder.tvLastOrderedQty = (TextView) convertView.findViewById(R.id.tvLastOrderedQty);
        holder.etQty = (EditText) convertView.findViewById(R.id.etQty);
        holder.parentRL = (LinearLayout) convertView.findViewById(R.id.parentRL);
        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    final ProductListBean tempBean = productList.get(position);
    holder.etQty.setId(position);
    final String productName = tempBean.getProductName();
    final String productCode = tempBean.getProductCode();
    final String productShortCode = tempBean.getProductShortCode();

    if (mSelectedProd != null && mSelectedProd.contains(productCode)) {
        final int indexOfProd = mSelectedProd.indexOf(productCode);
        holder.etQty.setText(mSelectedProducts.get(indexOfProd).getEnteredQty());
    }

    holder.tvProductName.setText(productName);
    holder.tvProdShortCode.setText(productShortCode);
    holder.tvLastOrderedQty.setText(tempBean.getLastOrderedQty());
    holder.etQty.setText(tempBean.getEnteredQty());

    holder.parentRL.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            holder.etQty.requestFocus();
        }
    });


    holder.etQty.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            final int pos = holder.etQty.getId();
            final String qty = holder.etQty.getText().toString();
            if (qty.length() > 0) {
                if (String.valueOf(qty.charAt(0)).equals("0")) {
                    holder.etQty.setError("Invalid Quantity");
                    holder.etQty.setText("");
                } else {
                    productList.get(pos).setEnteredQty(holder.etQty.getText().toString());
                }
            } else {
                productList.get(pos).setEnteredQty("");
            }
        }
    });

    return convertView;
}

static class ViewHolder {
    TextView tvProductName, tvProdShortCode, tvLastOrderedQty;
    EditText etQty;
    LinearLayout parentRL;
}

Upvotes: 1

Related Questions