ListView中的EditText没有它回收输入

mae*_*ebe 5 android listview cursor recycle android-edittext

还是Android的新手,甚至更多的自定义游标适配器,所以我无法理解如何防止我的listview回收视图,以防止滚动时一个edittext的输入显示在另一个.我在其他帖子上看到过要改变转换视图的名称,但是如何做到这一点我正在画一个空白.我希望有人能够根据我迄今为止编写的代码提供更多详细信息或示例.

public class editview extends ListActivity {
    private dbadapter mydbhelper;
    private PopupWindow pw;
    public static int editCount;
    public static ListView listView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mydbhelper = new dbadapter(this);
        mydbhelper.open();


        View footer = getLayoutInflater().inflate(R.layout.footer_layout, null);
        ListView listView = getListView();
        listView.addFooterView(footer);
        showResults();
        }

    //Populate view
    private void showResults (){
        Cursor cursor = mydbhelper.getUserWord();
        startManagingCursor(cursor);
        String[] from = new String[] {dbadapter.KEY_USERWORD};
         int[] to = new int[] {R.id.textType};
         ItemAdapter adapter = new ItemAdapter(this, R.layout.edit_row, cursor,
                        from, to);
            adapter.notifyDataSetChanged();
            this.setListAdapter(adapter);
            editCount = adapter.getCount();

    }


            //footer button
            public void onClick(View footer){
                    final MediaPlayer editClickSound = MediaPlayer.create(this, R.raw.button50);
                    editClickSound.start();
                    startActivity(new Intent("wanted.pro.madlibs.OUTPUT"));

                }

//custom cursor adapter
class ItemAdapter extends SimpleCursorAdapter {
    private LayoutInflater mInflater;
    private Cursor cursor;


    public ItemAdapter(Context context, int layout, Cursor cursor, String[] from,
            int[] to) {
        super(context, layout, cursor, from, to);
        this.cursor = cursor;
        mInflater = LayoutInflater.from(context);

    }


    static class ViewHolder {
        protected TextView text;
        protected EditText edittext;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {


        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.edit_row, null);


             holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(R.id.textType);
            holder.edittext = (EditText) convertView.findViewById(R.id.editText);



            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();

        }
        cursor.moveToPosition(position);
        int label_index = cursor.getColumnIndex("userword"); 
        String label = cursor.getString(label_index);

        holder.text.setText(label);

        return convertView;

    }

}
Run Code Online (Sandbox Code Playgroud)

把它改成了

class ItemAdapter extends SimpleCursorAdapter {
    private LayoutInflater mInflater;
    private Cursor cursor;
    Map<Integer, String> inputValues = new HashMap<Integer, String>();
    public View getView(final int position, View convertView, ViewGroup parent) {
        ....

        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.edit_row, null);


             holder = new ViewHolder();
            holder.text = (TextView) convertView.findViewById(R.id.textType);
            holder.edittext = (EditText) convertView.findViewById(R.id.editText);


            convertView.setTag(holder);

        } else {
            holder = (ViewHolder) convertView.getTag();

        }
        cursor.moveToPosition(position);
        int label_index = cursor.getColumnIndex("userword"); 
        String label = cursor.getString(label_index);

        holder.text.setText(label);
        String oldText =  inputValues.get(position);
        holder.edittext.setText(oldText == null ? "" : oldText); 
        holder.edittext.addTextChangedListener(new TextWatcher(){
            public void afterTextChanged(Editable editable) {
                inputValues.put(position, editable.toString());
            }
Run Code Online (Sandbox Code Playgroud)

但是在所有edittext都有数据后它才会被回收.尝试使用holder.edittext.setText(oldText),但效果相同.

开始填写字段

完成填写字段

向上滑动.

Sam*_*udd 4

首先,您确实不想阻止列表视图回收其视图。视图回收是一个巨大的优化。有关列表的许多非常好的信息,请参阅 google IO 演讲:http://www.youtube.com/watch?v =wDBM6wVEO70

话虽这么说,您已经正确地识别了您的问题:您的 EditText 数量远远少于列表中的项目数量。当您滚动列表时,这些 EditText 会被回收,因此您会一遍又一遍地看到相同的输入。

基本上,您需要做的是将 EditTexts 的输入保存在某种数据结构中(如果它们只编辑几个值,则为 HashMap;如果它们将更改大多数值,则可能为 List,两者都可以),将位置映射到输入。您可以通过在 getView 中的编辑文本中添加 textChangedListener 来完成此操作:

@Override
public View getView(final int position, View convertView, ViewGroup parent){
    ...
    cursor.moveToPosition(position);
    int label_index = cursor.getColumnIndex("userword");
    String label = cursor.getString(label_index);

    holder.text.setText(label);

    //clear whatever text was there from some other position
    //and set it to whatever text the user edited for the current 
    //position if available
    String oldText = yourMapOfPositionsToValues.get(position);
    holder.setText(oldText == null ? "" : oldText); 

    //every time the user adds/removes a character from the edit text, save 
    //the current value of the edit text to retrieve later
    holder.edittext.addTextChangedListener(new TextWatcher(){
        @Override
        public void afterTextChanged(Editable editable) {
            yourMapOfPositionsToValues.put(position, editable.toString());
        }
        ....
    };

    return convertView;
}
Run Code Online (Sandbox Code Playgroud)

每当您的用户完成编辑时,您都可以运行数据结构并对这些值执行任何操作。

编辑:

我将 onTextChanged 更改为 afterTextChanged 因为我以前使用过它并且我知道它有效。请记住,每次字母更改时都会调用 afterTextChanged,而不是在用户完成输入单词后调用。如果用户输入“dog”,afterTextChanged 将被调用三次,首先使用“d”,然后使用“do”,然后使用“dog”。

HashMap 很简单:Map yourMapOfPositionsToValues = new HashMap();

添加或更新项目: yourMap.put(position, someText); 获取项目: yourMap.get(position);

如果哈希图没有意义,请花一些时间研究它们。它们是非常重要的数据结构。

您的 TextWatcher 实现不正确。您的数据结构不应该属于单个视图,而应该属于活动或适配器。在您看来,位置不稳定,因为您的列表由每个视图拥有。位置本身是稳定的,除非基础数据发生更改,否则游标每次都会为同一位置返回相同的数据。但是,编辑文本用于多个不同的位置。

创建一个哈希图作为我上面在适配器的构造函数中演示的实例变量。然后准确添加我原来写的TextWatcher,不需要命名类,匿名更简单。你的代码应该可以工作。