在安卓开发时,遇到了一个问题,就是在listview中使用了EditText控件,需要获取edittext里面的输入进行数据更新,想了很久,想了一个办法:在adapter的定义中使用一个hashmap来存储已经加载的数据,同时,为edittext添加监听器,当数据更新时,更新hashmap中相应的值; 其中,listview的layout中是一个textview和一个edittext,TextView里面的值就理所应当地成为了map里面的key值。
代码如下:
private void bind(final CmdandRes cmdandRes, View view) { ViewHolder holder = (ViewHolder) view.getTag(); String cmdString = cmdandRes.getCommand(); String resulte = cmdandRes.getRes(); holder.cmdTextView.setText(cmdString); holder.resTextView.setText(name); holder.changeEdit.setText(value); String valueString = holder.changeEdit.getText().toString(); if(valueString.length()!=0)map.put(cmdandRes.getCommand().substring(0,cmdandRes.getCommand().length()-1),valueString); holder.changeEdit.addTextChangedListener(new TextWatcher() { @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) { map.put(cmdandRes.getCommand().substring(0,cmdandRes.getCommand(),s.toString()); } }); }
adapter的布局代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:tag="cmdstring" android:layout_height="match_parent" android:layout_weight="1"/> <EditText android:tag="resedit" android:layout_width="0dp" android:textSize="12sp" android:layout_height="match_parent" android:layout_weight="1" android:maxLines="1" android:imeOptions="actionNone"/> </LinearLayout>
目前还有一个问题,是如何动态配置edittext的可修改性,对一个button添加事件监听器,点击之后可以将listview里面的edittext变为可修改,如有解决方案,望告知。