Android ListView的Item中含有EditText,获取EditText里面的值并且刷新ListView值不改变

时间:2023-01-28 22:44:44

最近项目中用ListView ,ListView的item是EditText,然后设置EditText的值,一拉ListVIew,EditText中的值就改变了,然后自己就上网找了资料,现在将方法总结一下,菜鸟第一次写博客,大牛勿喷

 

main.xml 里面只有一个ListView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ListView
android:id="@+id/listView"
android:cacheColorHint="#000000"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>

</LinearLayout>


listView 每一个item布局,里面只有一个EditText

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <EditText        android:id="@+id/editText1"        android:layout_width="fill_parent"        android:layout_height="wrap_content">    </EditText></LinearLayout>

 

java 代码:注释写的很清楚

package com.xiaanming;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import android.app.Activity;import android.os.Bundle;import android.text.Editable;import android.text.TextWatcher;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.EditText;import android.widget.ListView;public class ListViewActivity extends Activity {private ListView listView;private Adapter adapter;List<String> dataList = new ArrayList<String>();//定义一个HashMap,用来存放EditText的值,Key是positionHashMap<Integer, String> hashMap = new HashMap<Integer, String>();    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                //加入数据        dataList = addData();                //实例化ListView 并设置Adapter        listView = (ListView)findViewById(R.id.listView);        adapter = new Adapter();        listView.setAdapter(adapter);    }        //往ListView 里面添加数据的方法    private List<String> addData(){    List<String> list = new ArrayList<String>();    list.add("为你写诗");    list.add("我爱你");    list.add("傻瓜");    list.add("这就是爱");    list.add("我们在一起");    list.add("永远不分离");    list.add("想你的夜");    list.add("十年");    list.add("飞机场");    list.add("爱很简单");    list.add("普通朋友");    list.add("我们两");    list.add("富士三下");    list.add("离别");    list.add("们两");    return list;    }                    //自定义Adapter    class Adapter extends BaseAdapter{@Overridepublic int getCount() {return dataList.size();}@Overridepublic Object getItem(int position) {return dataList.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(final int position, View convertView, ViewGroup parent) {String str = dataList.get(position);convertView = LayoutInflater.from(getApplication()).inflate(R.layout.listview_item, null);final EditText editText = (EditText)convertView.findViewById(R.id.editText1);editText.setText(str);//为editText设置TextChangedListener,每次改变的值设置到hashMap//我们要拿到里面的值根据position拿值editText.addTextChangedListener(new TextWatcher() {@Overridepublic void onTextChanged(CharSequence s, int start, int before, int count) {}@Overridepublic void beforeTextChanged(CharSequence s, int start, int count,int after) {}@Overridepublic void afterTextChanged(Editable s) {//将editText中改变的值设置的HashMap中hashMap.put(position, s.toString());}});//如果hashMap不为空,就设置的editTextif(hashMap.get(position) != null){editText.setText(hashMap.get(position));}return convertView;}        }    }


如果你想得到每个editText中的值,根据position就可以拿到啦