Android:EditText在ListView中滚动内容丢失内容?

前端之家收集整理的这篇文章主要介绍了Android:EditText在ListView中滚动内容丢失内容?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有列表项目与其中的EditText,我不知道将有多少项目.当我在EditText中输入一些文本,然后向下滚动ListView时,我有一个问题,在我再次滚动之后,我的第一个EditText中没有文本,或者有来自ListView的其他EditText的一些文本.

我已经尝试了TextWatcher,并将数据保存到数组,但问题是ListView中返回的视图位置并不总是正确的,所以我从数组中丢失了一些数据.

请帮忙.

这是我的代码

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 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;

    }

}

解决方法

尝试这个:
public class EfficientAdapter extends BaseAdapter {

private LayoutInflater mInflater;
public String[] Current;
ArrayList<String> MeterName,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,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 count) {

            }

            public void beforeTextChanged(CharSequence s,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;

}

}

这里我包括了一个HashMap对象,如果EditText包含值,它将会保持注意.当您滚动列表视图时,它将再次通过调用其getView方法以及与每个edittext关联的文本再次呈现.

在这段代码中,当你首次加载listview时,所有的edittext都将没有text.once,你会输入一些文本,它将在myList中被注明.当你再次渲染列表时,你的文本将被阻止.

还有一件事,你应该在外部实现textwatcher if(convertView == null).. else .. block.这是一个更好的做法!

原文链接:/android/313722.html

猜你在找的Android相关文章