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!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…