Sachin Gurnani Sachin Gurnani - 25 days ago 10
Android Question

Android :EditText loses content on scroll in ListView?

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.

Please help.

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;

}

}

Answer

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!