最近工作需要,做了类似于QQ群投票的功能,整理了一下关于listView中嵌套EditText,动态获取EditText中的值。
最重要的就是TextWatch的使用。
@Override public View getView(final int position, View convertView, ViewGroup parent) { final ViewHolder holder; if (convertView==null){ holder=new ViewHolder(); convertView = LayoutInflater.from(context).inflate(R.layout.option_item, parent, false);; holder.editOptions= (EditText) convertView.findViewById(R.id.editOption); holder.btnDelete= (TextView) convertView.findViewById(R.id.btnDelete); holder.layoutOption=convertView.findViewById(R.id.layoutOption); holder.editOptions.setTag(position); 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 ) { int position = (Integer) mHolder.editOptions.getTag(); mData.get(position).setText(s.toString());// 当EditText数据发生改变的时候存到data变量中 } } } holder.editOptions.addTextChangedListener(new MyTextWatcher(holder)); convertView.setTag(holder); }else{ holder = (ViewHolder) convertView.getTag(); holder.editOptions.setTag(position); } final Option option=mData.get(position); holder.editOptions.setText(option.getText()); holder.editOptions.setHint(option.hint); if(position<2){ holder.btnDelete.setVisibility(View.GONE); }else{ holder.btnDelete.setVisibility(View.VISIBLE); } holder.btnDelete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.d("TEST", "remove:" + mData.get(position).getText()); mData.remove(position); notifyDataSetChanged(); } }); return convertView; }demo 下载
http://download.csdn.net/detail/qq_31303013/9747999
新闻热点
疑难解答