【问题标题】:EditText in ListView without it recycling inputListView 中的 EditText 没有它回收输入
【发布时间】:2012-03-15 08:54:15
【问题描述】:

仍然是 android 的新手,甚至是自定义光标适配器的新手,所以我无法理解如何防止我的列表视图回收视图以防止滚动时来自一个编辑文本的输入显示在另一个中。我在其他帖子上看到说要更改 convertview 的名称,但如何做到这一点我是空白的。我希望这里有人能够根据我迄今为止编写的代码提供更多详细信息或示例。

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) 但效果相同。

【问题讨论】:

    标签: android listview cursor android-edittext recycle


    【解决方案1】:

    解决方案是在设置文本之前删除添加的文本观察器。否则,该视图上的前一个 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);
    

    【讨论】:

      【解决方案2】:

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

      话虽如此,您已经正确地确定了您的问题:您的 EditTexts 比您列表中的项目少得多。当您滚动浏览列表时,这些 EditTexts 会被回收,因此您会一遍又一遍地看到相同的输入。

      基本上,您需要做的是将 EditTexts 的输入保存在某些数据结构中(如果他们只会编辑几个值,则为 HashMap,如果他们将更改大部分值,则可能是 List,两者都可以)映射输入的位置。您可以通过将 textChangedListener 添加到 getView 中的编辑文本来做到这一点:

      @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 将被调用 3 次,首先是 'd',然后是 'do',然后是 'dog'。

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

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

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

      您的 TextWatcher 实现不正确。您的数据结构不应属于单个视图,而应属于活动或适配器。在您看来,职位并不稳定,因为您的 List 归每个视图所有。位置本身是稳定的,除非基础数据发生变化,否则光标每次都会为相同的位置返回相同的数据。但是,编辑文本用于多个不同的位置。

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

      【讨论】:

      • 我在玩 aftertextchange 但我发现它并没有按照我想要的方式存储值。 IE 它只会将字符串的一部分存储到数组的不同部分中。我可以在最后一个 edittext 中输入类似 dog 的内容,以及何时调用 ArrayList editTextList 中的位置。它看起来像 pos1 dog pos2 do pos3 do。我不明白为什么它只抓取了edittext字符串的一部分。
      • 职位似乎不可靠,但也许我说错了。当我使用 simplecursor 适配器时,我通过 for 循环更改了 edittext 的 id,并使用该循环存储并在下一个活动中调用它们。但还是遇到了回收问题。在过去的两周里,我断断续续地查看了 hashmap,但似乎找不到任何解释如何使用它们的好信息。这里和那里只有一些代码。
      • //TextWatcher,将数据保存到Array以备后用。类观察者实现 TextWatcher { private int position;公共观察者(int position){ this.position = position; } public void afterTextChanged(Editable s) { Log.e(String.valueOf(position), "is the position int");字符串文本 = s.toString(); editview.editTextList.add(位置,文本);是我想出的我的文本观察器
      • 哦,是的,我的列表视图将有 4-10 个编辑文本,但要确保它们在大多数情况下不会在屏幕上太小,列表将是可滚动的。这个想法是他们将为每个人输入,然后在下一个活动中,我将接受他们的输入并使用它使用我的正则表达式替换我的数据库中的字符串中的字符。
      • 链接为 Ty。前段时间开始看,忘记看完了。现在尝试一下,对 holder.setText(oldText == null ? "" : oldText); 有点困惑不确定 viewholder 中的 setText() 应该是什么。尝试 holder.edittext.settext... 在我第一次向下滚动时似乎可以工作,但是如果我向上滚动,它会从底部回收输入以替换我放在顶部的内容。尝试使用它,但是任何关于 settext() 应该包含什么的见解都会有所帮助。到目前为止。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-19
      • 2012-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多