ListView中的EditText没有它回收输入

时间:2012-02-24 22:01:09

标签: android listview cursor android-edittext recycle

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

    }

}

将其更改为

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

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

Start filling in fields

Finish filling in fields

Scroll up.

2 个答案:

答案 0 :(得分:4)

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

话虽如此,您已经正确地确定了您的问题:您的EditTexts远远少于列表中的项目。当您滚动列表时,这些EditTexts将被回收,因此您可以反复查看相同的输入。

基本上你需要做的是在一些数据结构中保存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;
}

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

编辑:

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

HashMap很简单:映射你的MapOfPositionsToValues = new HashMap();

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

如果hashmaps没有意义,花点时间研究它们。它们是一个非常重要的数据结构。

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

创建一个hashmap作为我在适配器的构造函数中演示的实例变量。然后添加我原来写的TextWatcher,不需要命名类,匿名更简单。你的代码应该有效。

答案 1 :(得分:1)

解决方法是在设置文本之前删除添加的textwatcher。否则,该视图上的前一个textwatcher仍将与新的textwatcher一起被调用。将textwatcher存储为EditText上的标记以跟踪它。

Object oldWatcher = viewHolder.quantitySold.getTag();
    if(oldWatcher != null){
        viewHolder.quantitySold.removeTextChangedListener((CustomTextWatcher)oldWatcher);
    } 
    String oldText =  inputValues.get("key"+position);
    Log.d(TAG, "oldText: "+oldText+" position: "+position);
    viewHolder.quantitySold.setText(oldText == null ? "" : oldText);
    CustomTextWatcher watcher = new CustomTextWatcher(
            cursor.getString(SKUFragment.COL_NAME),
            cursor.getInt(SKUFragment.COL_ID),
            cursor.getDouble(SKUFragment.COL_UNIT_PRICE),
            position
    ) {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            if (s != null) {
                int quantity = 0;
                if (!TextUtils.isEmpty(s.toString())) {
                    quantity = Integer.parseInt(s.toString());
                    inputValues.put("key"+mPosition, "" + quantity);
                }else{
                    inputValues.put("key"+mPosition, "");
                }
                double value = quantity * skuPrice;
                mListener.onQuantityChanged(skuName+", position: "+mPosition, skuId, quantity, value);
            }
        }
    };
    viewHolder.quantitySold.setTag(watcher);
    viewHolder.quantitySold.addTextChangedListener(watcher);