教育行业A股IPO第一股(股票代码 003032)

全国咨询/投诉热线:400-618-4000

Android培训之ListView获取item中EditText值(2)

更新时间:2016年10月31日17时24分 来源:传智播客Android培训学院 浏览次数:

 class MyTextWatcher implements TextWatcher {
                public MyTextWatcher(ViewHolder holder) {
                    mHolder = holder;
                }
                private ViewHolder mHolder;
                @Override
                public void onTextChanged(CharSequence s, int start,
                        int before, int count) {
                }
                @Override
                public void beforeTextChanged(CharSequence s, int start,
                        int count, int after) {
                }
                @Override
                public void afterTextChanged(Editable s) {
                    if (s != null && !"".equals(s.toString())) {
                        int position = (Integer) mHolder.value.getTag();
                        mData.get(position).put("list_item_inputvalue",
                                s.toString());// 当EditText数据发生改变的时候存到data变量中
                    }
                }
            }
            holder.value.addTextChangedListener(new MyTextWatcher(holder));
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
            holder.value.setTag(position);
        }
        Object value = mData.get(position).get("list_item_name");
        if (value != null) {
            holder.name.setText((String) value);
        }
        value = mData.get(position).get("list_item_title");
        if (value != null) {
            holder.title.setText(value.toString());
        }
        value = mData.get(position).get("user_key");
        if (value != null) {
            holder.userkey.setText(value.toString());
        } else {
            holder.userkey.setText("-1");
        }
        value = mData.get(position).get("list_item_inputvalue");
        if (value != null && !"".equals(value)) {
            holder.value.setText(value.toString());
        } else {
            String key = mData.get(position).get("user_key").toString();
            String inputValue = editorValue.get(key);
            holder.value.setText(inputValue);
        }
        holder.value.clearFocus();
        if (index != -1 && index == position) {
            holder.value.requestFocus();
        }
        return convertView;
    }
    public final class ViewHolder {
        public TextView name;
        public TextView title;
        public EditText value;// ListView中的输入
        public TextView userkey;// 用来定义的标志性主键,可不用关心
    }
}</map<string,></string,></string,></map<string,>

使用方法:

private List<map> mCheckItemList = new ArrayList<map>();</map</map

mAdapter = new ListEditorAdapter(this, mCheckItemList);

m_lvListView.setAdapter(mAdapter);

直接改变mCheckItemList中的数据,然后调用mAdapter.notifyDataSetChanged();就可以更新ListView的数据

当然,直接从mCheckItemList也可以获得用户在EditText中输入的数据.比较方便

其他:

ListView list= (ListView)findViewById(R.id.list);//获得listview

for (int i = 0; i < list.getChildCount(); i++) {
LinearLayout layout = (LinearLayout)list.getChildAt(i);// 获得子item的layout
EditText et = (EditText) layout.findViewById(R.id.et);// 从layout中获得控件,根据其id
// EditText et = (EditText) layout.getChildAt(1)//或者根据位置,在这我假设TextView在前,EditText在后

System.out.println("the text of "+i+"'s EditText:----------->"+et.getText());
}

本文版权归传智播客Android培训学院所有,欢迎转载,转载请注明作者出处。谢谢!
作者:传智播客Android培训学院
首发:http://www.itcast.cn/Android
0 分享到:
和我们在线交谈!