10

I have list item with EditText in it, I don't know how many items there will be. I have a problem when I enter some text in EditText, and then scroll down a ListView, after I've scroll up again there is no text in my first EditText, or there is some text from other EditText from ListView.

I've tried TextWatcher, and saving data to array, but problems is that returned position of view in ListView isn't always right, so I lost some data from array.

Here is my code:

public class EfficientAdapter extends BaseAdapter {

    private LayoutInflater mInflater;
    public String[] Current;
    ArrayList<String> MeterName, PreviousReading, Current_Reading;
    JSONArray getArray_Meter_Reading;

    public EfficientAdapter(Context context, JSONArray getArray_Meter_Reading) {
        mInflater = LayoutInflater.from(context);
        this.getArray_Meter_Reading = getArray_Meter_Reading;
        MeterName = new ArrayList<String>();
        PreviousReading = new ArrayList<String>();
        for (int i = 0; i < getArray_Meter_Reading.length(); i++) {
            try {
                String Meter_Name = getArray_Meter_Reading.getJSONObject(i)
                        .getString("MeterName").toString();
                String previous_Meter_Reading = getArray_Meter_Reading
                        .getJSONObject(i).getString("PrevMeterReading")
                        .toString();
                MeterName.add(Meter_Name);
                PreviousReading.add(previous_Meter_Reading);

                // Meter[i]=MeterName.get(i);
                // Previous[i]=PreviousReading.get(i);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    public int getCount() {

        return getArray_Meter_Reading.length();
    }

    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;

        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.meter_reading_list, null);
            holder = new ViewHolder();
            holder.adp_MeterName = (TextView) convertView
                    .findViewById(R.id.txt_Meter_Name);
            holder.adp_Previous = (TextView) convertView
                    .findViewById(R.id.txt_Previous);
            holder.adp_Current = (EditText) convertView
                    .findViewById(R.id.ed_Current);

            holder.adp_Current.addTextChangedListener(new TextWatcher() {

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

                }

                public void beforeTextChanged(CharSequence s, int start,
                        int count, int after) {
                    // TODO Auto-generated method stub

                }

                public void afterTextChanged(Editable s) {

                    Current[holder.ref] = s.toString();

                }
            });
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.ref = position;
        holder.adp_MeterName.setText(MeterName.get(position));
        holder.adp_Previous.setText(PreviousReading.get(position));
        // holder.adp_Current.setHint(MeterName.get(position));

        // holder.adp_Current.setText(PreviousReading.get(position));

        return convertView;
    }

    class ViewHolder {
        TextView adp_MeterName, adp_Previous;
        EditText adp_Current;
        int ref;

    }

}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Sachin Gurnani
  • 2,444
  • 7
  • 36
  • 45

4 Answers4

10

try this:

public class EfficientAdapter extends BaseAdapter {

private LayoutInflater mInflater;
public String[] Current;
ArrayList<String> MeterName, PreviousReading, Current_Reading;
JSONArray getArray_Meter_Reading;
public static HashMap<Integer,String> myList=new HashMap<Integer,String>();

public EfficientAdapter(Context context, JSONArray getArray_Meter_Reading) {
    mInflater = LayoutInflater.from(context);
    this.getArray_Meter_Reading = getArray_Meter_Reading;
    MeterName = new ArrayList<String>();
    PreviousReading = new ArrayList<String>();

    for (int i = 0; i < getArray_Meter_Reading.length(); i++) {
        try {
            String Meter_Name = getArray_Meter_Reading.getJSONObject(i)
                    .getString("MeterName").toString();
            String previous_Meter_Reading = getArray_Meter_Reading
                    .getJSONObject(i).getString("PrevMeterReading")
                    .toString();
            MeterName.add(Meter_Name);
            PreviousReading.add(previous_Meter_Reading);

            // Meter[i]=MeterName.get(i);
            // Previous[i]=PreviousReading.get(i);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    // initialize myList
    for(int i=0;i<JSON_ARRAY_LENGTH;i++)
    {
       myList.put(i,"");
    }
}

public int getCount() {

    return getArray_Meter_Reading.length();
}

public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder holder;
    final int pos=position;
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.meter_reading_list, null);
        holder = new ViewHolder();
        holder.adp_MeterName = (TextView) convertView
                .findViewById(R.id.txt_Meter_Name);
        holder.adp_Previous = (TextView) convertView
                .findViewById(R.id.txt_Previous);
        holder.adp_Current = (EditText) convertView
                .findViewById(R.id.ed_Current);


        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }
     holder.adp_Current.addTextChangedListener(new TextWatcher() {

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

            }

            public void beforeTextChanged(CharSequence s, int start,
                    int count, int after) {
                // TODO Auto-generated method stub

            }

            public void afterTextChanged(Editable s) {

                Current[holder.ref] = s.toString();
                myList.put(pos,s.toString.trim());
            }
        });
    holder.ref = position;
    holder.adp_MeterName.setText(MeterName.get(position));
    holder.adp_Previous.setText(PreviousReading.get(position));
    // holder.adp_Current.setHint(MeterName.get(position));

    // holder.adp_Current.setText(PreviousReading.get(position));

    holder.adp_Current.setText(myList.get(position));

    return convertView;
}

class ViewHolder {
    TextView adp_MeterName, adp_Previous;
    EditText adp_Current;
    int ref;

}

}

Here I have included a HashMap object which will keep an eye on if EditText contains value or not.And when you scroll the listview,it will be rendered again by calling its getView method along with the text associated with each edittext.

In this code,when you firstly load listview,all your edittext will be with no text.once you enter some text,it will be noted in myList.So when you again render the list,your text would be prevented.

One more thing,you should implement textwatcher outside if(convertView==null)..else.. block.That's a better practice!

Hiral Vadodaria
  • 19,158
  • 5
  • 39
  • 56
  • have you looked into the code i suggested? it contains a HashMap object there.go through the code first! – Hiral Vadodaria Feb 17 '12 at 13:01
  • 1
    what problem i face is when type some value in 1st edittext and i scroll down to the list,value in 1st edittext is shown is some other edittext box. can plz give solution for that im facing this problem from last three day . – Sachin Gurnani Feb 17 '12 at 13:03
  • Try this code out.i think,it will solve your problem.just give it a try and let me know!! – Hiral Vadodaria Feb 17 '12 at 13:05
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/7849/discussion-between-hiral-and-sachin-gurnani) – Hiral Vadodaria Feb 17 '12 at 13:15
  • it works fine thank you very much and i will tell after testing the app thank you so much – Sachin Gurnani Feb 17 '12 at 13:26
  • 1
    @Hiral would you mind explaining why it's better practice to implement the textWatcher outside the if(convertView==null) block? – stephen Sep 06 '13 at 19:57
9

I was having the exact same problem with a project I was working on. All solutions I had found also mentioned using the textChangeListener, however due to the nature of the list, and with an EditText in each row view, this was very inefficient. I took a separate approach using the EditText.setOnFocusChangeListener().

I attached this to each EditText within the getView method. The reason this works so well is it is called when the user switches to a different edittext or scrolls so the current row is off the screen which causes the EditText to lose focus.

In the onFocusChange(View v, boolean hasFocus) within the listener I simply put:

if (!hasFocus) {
                EditText et = (EditText) v.findViewById(R.id.groupAmount);                  
                data.get(position).put("dueAmount", et.getText().toString().trim());
            }

In my example I am storing the entered value into my data used to populate the views on each getView call, but this should still show how to access the data the User entered. Just make sure you attempt to take this stored data and try populating the editText at the corresponding position.

Hope this helps.

Posting my full adapter here for reference

public class memberlistadapter extends BaseAdapter {
private LayoutInflater mInflater;
public ArrayList<Hashtable<String, String>> data;
ViewHolder viewHolder;
public static HashMap<Integer,String> myList=new HashMap<Integer,String>();

public memberlistadapter(Context context) {
    mInflater = LayoutInflater.from(context);
}

public memberlistadapter(Activity activity, ArrayList<Hashtable<String, String>> objects) {
    super();
    mInflater = activity.getLayoutInflater();
    //this.activity = activity;
    this.data = objects;
    Log.d("data", data.toString());
}

public void clear() {
    this.data.clear();
    viewHolder = null;
}

public void setData(ArrayList<Hashtable<String, String>> data) {
    this.data = data;
}

public int getCount() {
    return data.size();
}

public Object getItem(int item) {
    return data.get(item);
}

public long getItemId(int position) {
    return position;
}

public View getView(final int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.member_amount_child, null);
        viewHolder = new ViewHolder();

        viewHolder.nameText = (TextView) convertView.findViewById(R.id.grp_amt_child);
        viewHolder.amountText = (EditText) convertView.findViewById(R.id.groupAmount);
        viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.memberCheckNotif);

        convertView.setTag(viewHolder);

    } else {
        viewHolder = (ViewHolder) convertView.getTag();
    }
    //Log.d("data at "+position, data.get(position).toString());
    String amt = data.get(position).get("dueAmount");
    String name = data.get(position).get("name");
    String check = data.get(position).get("reminder");

    viewHolder.nameText.setText(name);
    try {
    if (amt.length() > 0 && !amt.equalsIgnoreCase("0")) {
        viewHolder.amountText.setText(amt);
    } else {
        viewHolder.amountText.setText("");
    }
    } catch (Exception e) {
        viewHolder.amountText.setText("");
    }

    viewHolder.amountText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
                EditText et = (EditText) v.findViewById(R.id.groupAmount);
                data.get(position).put("dueAmount", et.getText().toString().trim());
            }
        }
    });

    try {
    if (check.equalsIgnoreCase("true")) {
        viewHolder.checkBox.setChecked(true);
    } else {
        viewHolder.checkBox.setChecked(false);
    }
    } catch (Exception e) {
        viewHolder.checkBox.setChecked(false);
    }
    viewHolder.checkBox.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            if (((CheckBox) v).isChecked()) {
                data.get(position).put("reminder", "True");
            } else {
                data.get(position).put("reminder", "False");
            }
        }
    });
    if (position == 0) {
        viewHolder.checkBox.setVisibility(View.GONE);
        viewHolder.amountText.setVisibility(View.GONE);
        viewHolder.nameText.setVisibility(View.GONE);
    } else {
        viewHolder.checkBox.setVisibility(View.VISIBLE);
        viewHolder.amountText.setVisibility(View.VISIBLE);
        viewHolder.nameText.setVisibility(View.VISIBLE);
    }

    return convertView;
}

static class ViewHolder {
    TextView nameText;
    EditText amountText;
    CheckBox checkBox;
}
}
Maveňツ
  • 1
  • 12
  • 50
  • 89
defpillz
  • 111
  • 1
  • 4
  • 4
    this doesnt solve the problem at all. If you put some text on a Edit Text and then scroll, the first edit text doesnt lose focus and the input get lost. – kiduxa Jun 09 '13 at 19:05
  • @kiduxa Use `addTextChangedListener` instead of `setOnClickListener` for EditText and put `vendorList.get(position).put("preference", et_preference.getText().toString().trim());` in **afterTextChanged** – Chaman Saini Feb 17 '16 at 13:23
0

maybe you can try this or see on the right the related posts section

EditText loses content on scroll in ListView

Community
  • 1
  • 1
0

First, declare a String[] or List<String> member variable (inside the class outside the getView()) for storing the input text from your EditText object, and record the text in addTextChangeListener() you set on the EditText object.

Second, put your setText() after (not inside) the addTextChangeListener().

    viewHolder.editText.removeTextChangedListener(viewHolder.textWatcher);

    viewHolder.textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        }

        @Override
        public void afterTextChanged(Editable editable) {
            mInputs[position] = editable.toString();            //record input

            viewHolder.editText.setSelection(viewHolder.editText.getText().length());       //set cursor to the end
        }
    };

    viewHolder.editText.addTextChangedListener(viewHolder.textWatcher);
    viewHolder.editText.setText(mInputs[position]);

private class ViewHolder {
    EditText editText;
    TextWatcher textWatcher;

    public ViewHolder(View itemView) {
        editText = itemView.findViewById(R.id.main_editText);
    }
}
Sam Chen
  • 7,597
  • 2
  • 40
  • 73
  • Please don't add the same answer to multiple questions. Answer the best one and flag the rest as duplicates. See [Is it acceptable to add a duplicate answer to several questions?](//meta.stackexchange.com/q/104227) – Dharman Aug 16 '19 at 22:06