0

This question has been asked frequently ..but nothing seems to work for me.

I have a listview with edittext .. when I scroll the listview value of edittext repeats.

Please suggest !!

code :

public View getView(int position, View view, ViewGroup parent) {

View mView = view;             //trying to reuse a recycled view

    if (mView == null) {
        //The view is not a recycled one: we have to inflate
        mView = getActivity().getLayoutInflater().inflate(
                getResource(), parent, false);
        holder = new ViewHolder();


        holder.row = (OEDataRow) moveLinesfinalData.get(position);

        holder.eProductName = (TextView) mView.findViewById(R.id.textViewProductNameFinal);
        holder.eProductName.setText(holder.row.getString("name").toString());

        holder.innerLinearLayout = (LinearLayout) mView
                .findViewById(R.id.innerLinearLayout);
        holder.editTextProductId = (EditText) holder.innerLinearLayout
                .findViewById(R.id.editTextProductId);
        holder.eProductSerial = (EditText) holder.innerLinearLayout
                .findViewById(R.id.editTextSerialFinal);

        holder.BarCode = (Button) holder.innerLinearLayout.findViewById(R.id.buttonBarcode);
        holder.editTextProductIdFocus = (EditText) holder.innerLinearLayout
                .findViewById(R.id.editTextProductId);

        mView.setTag(holder);
    } else {
        // View recycled !
        // no need to inflate
        // no need to findViews by id
        holder = (ViewHolder) mView.getTag();
    }

    if(holder.row != null) {
        holder.editTextProductId.setText(holder.row.getInt("id").toString());

    }

    holder.eProductSerial.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) {
            String newSerial = s.toString();

            String id = holder.editTextProductIdFocus.getText().toString();
            productData.put(id,newSerial);

        }
    });

    Log.d("list", "list productData "+productData);

    holder.BarCode.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            v.getId();
        }
    });


    return mView;
} 
Maveňツ
  • 1
  • 12
  • 50
  • 89
Nibha Jain
  • 7,742
  • 11
  • 47
  • 71
  • After saving your edittext value, you have in your `getView` to check if the current row (position) fits to the one which has the information stored. If not, put the edittext text to "". – zozelfelfo Sep 26 '14 at 10:37
  • possible duplicate of [Android :EditText loses content on scroll in ListView?](http://stackoverflow.com/questions/9328301/android-edittext-loses-content-on-scroll-in-listview) – Maveňツ Sep 26 '14 at 10:41
  • @maven : I already tried that solution not working for me .. – Nibha Jain Sep 26 '14 at 11:07

3 Answers3

0

You will have to save an array of Strings (something like arrayStrings) that contains the value of the EditText for the current position.

Set the content of arrayStrings[position] on your textChangedListener. Next on your getView set the queryText of your editText to whatever is on the array for the current position. Otherwise, and since the view will get recycled, expect incorrect behavior.

Pedro Oliveira
  • 20,442
  • 8
  • 55
  • 82
0

Instead of

if(holder.row != null) {
    holder.editTextProductId.setText(holder.row.getInt("id").toString());
}

Make it

if(holder.row != null) {
      holder.editTextProductId.setText(((OEDataRow) moveLinesfinalData.get(position)).getInt("id"));
}

In HolderView keep only Views IDs, not some data itself.

M G
  • 1,240
  • 14
  • 26
0

remove:

 holder.row = (OEDataRow) moveLinesfinalData.get(position);

and change

if(holder.row != null) {
        holder.editTextProductId.setText(holder.row.getInt("id").toString());   
}

to

if((OEDataRow) moveLinesfinalData.get(position) != null) {
        holder.editTextProductId.setText(String.valueOf((OEDataRow) moveLinesfinalData.get(position)).getInt("id"))); 
}
mmlooloo
  • 18,937
  • 5
  • 45
  • 64