前一段时间项目中用ListView ,ListView的item是EditText,然后设置EditText的值,滑动ListVIew,EditText中的值就改变了,当时在网上查了查各种各样的说法,最后找方法先把问题给解决了,今天闲来没事就写篇博客,说说怎么做的:
首先,主布局文件就一个ListView
<ListView接着,在MainActivity中设置adapter
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="@android:color/transparent"
android:divider="@android:color/transparent"
android:listSelector="@android:color/transparent"
android:scrollbars="none">
@Override最后看看 adapter
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
MyAdapter myAdapter = new MyAdapter(this);
listView.setAdapter(myAdapter);
}
view里边就一个TextView,EditText
<?xml version="1.0" encoding="utf-8"?>最后看MyAdapter
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="30dp">
<TextView
android:id="@+id/tv_position"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:gravity="center_vertical"
android:text="第0个"
android:padding="6dp"/>
<EditText
android:id="@+id/et_number"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center_vertical" />
</LinearLayout>
public class MyAdapter extends BaseAdapter {最后测试下:
private Context context;
HashMap<Integer, String> saveMap;//这个集合用来存储对应位置上Editext中的文本内容
public MyAdapter(Context context) {
super();
this.context = context;
saveMap = new HashMap<Integer, String>();
}
@Override
public int getCount() {
return 30;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder vh = null;
if (convertView == null) {
vh = new ViewHolder();
convertView = View.inflate(context, R.layout.item_editext, null);
vh.et_number = (EditText) convertView.findViewById(R.id.et_number);
vh.tv_position = (TextView) convertView
.findViewById(R.id.tv_position);
convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}
vh.tv_position.setText("第" + position + "个:");
vh.et_number.setTag(position);//设置editext一个标记
vh.et_number.clearFocus();//清除焦点 不清除的话因为item复用的原因 多个Editext同时改变
vh.et_number.setText(saveMap.get(position));//将对应保存在集合中的文本内容取出来 并显示上去
final EditText tempEditText=vh.et_number;
vh.et_number.addTextChangedListener(new TextWatcher() {
@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) {
Integer tag= (Integer) tempEditText.getTag();
saveMap.put(tag, s.toString());//在这里根据position去保存文本内容
}
});
Log.e("TAG", saveMap.toString());
return convertView;
}
public class ViewHolder {
EditText et_number;
TextView tv_position;
}
}
分别在第一个,第三个,最后一个中分别写入01,03,029
来回滑动下发现屏幕上显示没问题,看Log
值也保存上了 ,