Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
198 views
in Technique[技术] by (71.8m points)

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.

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;

    }

}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...