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

时间:2012-02-17 12:36:09

标签: android listview android-edittext

我的列表项目中包含EditText,我不知道会有多少项目。我在EditText中输入一些文字后出现问题,然后向下滚动ListView,在我再次向上滚动后,我的第一个EditText中没有文字,或者有来自EditText的其他ListView的一些文字。

我已尝试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 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;

    }

}

4 个答案:

答案 0 :(得分:9)

试试这个:

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;

}

}

这里我包含了一个HashMap对象,它会关注EditText是否包含值。当你滚动列表视图时,它将通过调用其getView方法以及与每个edittext关联的文本再次呈现。 / p>

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

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

答案 1 :(得分:9)

我正在处理与我正在进行的项目完全相同的问题。我发现的所有解决方案也提到了使用textChangeListener,但由于列表的性质,并且每个行视图中都有EditText,因此效率非常低。我使用EditText.setOnFocusChangeListener()采取了单独的方法。

我将它附加到getView方法中的每个EditText。这种方法运行良好的原因是当用户切换到不同的edittext或滚动时调用它,因此当前行离开屏幕会导致EditText失去焦点。

在监听器中的onFocusChange(View v,boolean hasFocus)中,我只是简单地说:

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

在我的示例中,我将输入的值存储到用于填充每个getView调用的视图的数据中,但这仍应显示如何访问用户输入的数据。只需确保您尝试获取此存储的数据并尝试在相应位置填充editText。

希望这有帮助。

在此处发布我的完整适配器以供参考

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

答案 2 :(得分:0)

也许您可以尝试这个或在右侧查看相关帖子部分

EditText loses content on scroll in ListView

答案 3 :(得分:0)

首先,声明一个String[]List<String>成员变量(在getView() inside 类中, inside ),以存储输入EditText对象中的文本,并将文本记录在您在addTextChangeListener()对象上设置的EditText中。

第二,将setText()放在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);
    }
}