【问题标题】:EditText input recycled even with Hashmap即使使用 Hashmap,EditText 输入也会被回收
【发布时间】:2012-02-26 10:57:21
【问题描述】:

我有一个带有自定义光标适配器的 Listview。我试图防止在 edittext 字段中的输入被回收。我一直在使用 Hashmap。它在您第一次向下滚动时起作用(在新显示的视图中不使用来自 0 等的数据)。但是当您向上滚动时,它会再次回收输入。我希望这里的人可以发现我的问题,因为我已经查看了其他关于使用 edittext 回收输入的问题的帖子,但与其他人的工作相比,我看不出我做错了什么。

public class editview extends ListActivity {
    private dbadapter mydbhelper;
    public static int editCount;
    public static ListView listView;
    public ItemAdapter adapter;

    @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};
         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;
    static Map<Integer, String> inputValues = new LinkedHashMap<Integer, String>();
    static String oldText;


    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(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);
        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());
                //Log.e(String.valueOf(position), "position in Hashmap");
                //Log.e(inputValues.get(position), "data in Hashmap");
            }

            public void beforeTextChanged(CharSequence s, int start,
                    int count, int after) {
                // TODO Auto-generated method stub

            }

            public void onTextChanged(CharSequence s, int start,
                    int before, int count) {
                // TODO Auto-generated method stub

            }

        });

        return convertView;

    }
}

我尝试了下面的代码,但它最终将我的回收视图称为位置 0。

static class ViewHolder implements TextWatcher {
    protected TextView text;
    protected EditText edittext;
    protected int position;

    public void afterTextChanged(Editable editable) {
        Log.e(String.valueOf(position), "Position in array");
        Log.e(editable.toString(), "data in array");
        inputValues.put(position, editable.toString());

    }
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
        // TODO Auto-generated method stub

    }
    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        // TODO Auto-generated method stub

    }

    }

@Override
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);
        holder.position = position;
        holder.edittext.addTextChangedListener(holder);

        convertView.setTag(holder);

    } else {....

【问题讨论】:

    标签: android listview hashmap android-edittext


    【解决方案1】:

    您可能希望在添加新实例之前删除旧的 TextWatcher 实例。看起来您有多个 TextWatcher 为列表中的多个位置观看相同 EditText 的范围,这最终可能看起来像视图回收出错。

    【讨论】:

    • 如果没有设置 textwatcher 子类,如果已经附加了一个新的,有没有办法可以删除或阻止添加一个新的?恐怕如果我对它进行子类化,我将很难获得 inputValues.put(position, editable.toString());根据需要工作(需要确保将数据放在与编辑文本相同的位置)
    • 你不需要这样做。只需在 ViewHolder 类中实现 TextWatcher 并仅在膨胀视图时执行 addTextChangedListener() 。这为每个 EditText 提供了一个 TextWatcher。然后您还需要将“位置”添加到 ViewHolder,以便您可以在 afterTextChanged() 中找到该行对应的数据。
    猜你喜欢
    • 2017-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-24
    • 1970-01-01
    相关资源
    最近更新 更多