ListView 的大体作用是填充数据和用户交互后产生的响应。其绑定数据方式多种,将最近学习的三种总结如下:
1、ListView 和ArrayAdapter
(1)建立一个常量类Constant,放置模拟数据
1 public final class Constant { 2 //定义一个数组 3 public static final String[] str = new String[]{"东部", "西部", "南部", "北部", "中部", 4 "东南部", "西北部", "西南部", "东北部", "中西部", "中南部", "中北部", "其他部"}; 5 6 private static final String[] str_name = {"柠檬", "苹果", "土豆", "西红柿", "香蕉"}; 7 private static final String[] str_class = {"水果", "水果", "蔬菜", "蔬菜", "水果"}; 8 private static final String[] str_address = {"海南", "吉林", "河南", "广西", "广东"}; 9 10 //把数据放到list中 11 public static List<HashMap<String, Object>> setList() { 12 List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>(); 13 for (int i = 0; i < str_name.length; i++) { 14 HashMap<String, Object> hashMap = new HashMap<String, Object>(); 15 hashMap.put("name", str_name[i]); 16 hashMap.put("str_class", str_class[i]); 17 hashMap.put("address", str_address[i]); 18 19 list.add(hashMap); 20 } 21 return list; 22 } 23 24 public static String[] str_arr() { 25 return str; 26 } 27 }
(2)建立一个ListView的布局文件(activity_arrary.xml)
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 3 android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 4 android:paddingRight="@dimen/activity_horizontal_margin" 5 android:paddingTop="@dimen/activity_vertical_margin" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 tools:context="com.yt.aboutlistview.activity.ArrayActivity"> 8 9 <TextView android:text="我是ArrayActivity" 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:id="@+id/textView2" /> 13 14 <ListView 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:id="@+id/lv_arr" 18 android:layout_below="@+id/textView2" 19 android:layout_alignParentLeft="true" 20 android:layout_alignParentStart="true" /> 21 22 </RelativeLayout>
(3)在布局对应的ArrayActivity.java中绑定适配器
1 public class ArrayActivity extends Activity { 2 private ListView lv; 3 private String[] str = null; 4 @Override 5 protected void onCreate(Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 // str = Constant.str_arr(); 8 // str = Constant.str;//如果数组公有也可以直接调用 9 setContentView(R.layout.activity_array); 10 //获取listview对象 11 lv = (ListView) findViewById(R.id.lv_arr); 12 //和适配器绑定 13 lv.setAdapter( 14 //ArrayAdapter中三个参数,第一个:上下文,第二个listview显示的样式,但三个listview内容 15 new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,Constant.str_arr())); 16 //监听事件 17 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 18 @Override 19 public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 20 Toast.makeText(ArrayActivity.this,"第 "+i+ "行,值是 "+view.toString(),Toast.LENGTH_LONG).show(); 21 } 22 }); 23 } 24 }
2、Listview 和SimpleAdapter(本例和下例都要引用1-(1)中的数据)
(1)建立布局文件activity_simple.xml(布局文件差不多,看具体使用)
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 3 android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 4 android:paddingRight="@dimen/activity_horizontal_margin" 5 android:paddingTop="@dimen/activity_vertical_margin" 6 android:paddingBottom="@dimen/activity_vertical_margin" 7 tools:context="com.yt.aboutlistview.activity.SimpleActivity"> 8 9 <TextView android:text="SimpleAdapter" 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 android:id="@+id/textView3" /> 13 14 <ListView 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:id="@+id/lv_simple" 18 android:layout_below="@+id/textView3" 19 android:layout_alignParentLeft="true" 20 android:layout_alignParentStart="true" 21 android:layout_marginTop="45dp" /> 22 23 </RelativeLayout>
(2)ListView的属性文件activity_simple_item.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 > 6 7 <TextView 8 android:layout_width="wrap_content" 9 android:layout_height="wrap_content" 10 android:text="tv_sim_item_name" 11 android:id="@+id/tv_sim_item_name" 12 android:layout_alignParentTop="true" 13 android:layout_alignParentLeft="true" 14 android:layout_alignParentStart="true" 15 android:textSize="20dp"/> 16 17 <TextView 18 android:layout_width="wrap_content" 19 android:layout_height="wrap_content" 20 android:text="tv_sim_item_class" 21 android:id="@+id/tv_sim_item_class" 22 android:layout_alignParentTop="true" 23 android:layout_alignParentRight="true" 24 android:layout_alignParentEnd="true" 25 android:textSize="30dp"/> 26 27 <TextView 28 android:layout_width="wrap_content" 29 android:layout_height="wrap_content" 30 android:text="New Text" 31 android:id="@+id/tv_sim_item_address" 32 android:layout_below="@+id/tv_sim_item_name" 33 android:layout_alignParentLeft="true" 34 android:layout_alignParentStart="true" 35 android:textSize="15dp"/> 36 </RelativeLayout>
(3)在布局对应的SimpleActivity.java绑定适配器
1 public class SimpleActivity extends Activity { 2 private ListView lv; 3 List<HashMap<String,Object>> list = null; 4 5 @Override 6 protected void onCreate(Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 setContentView(R.layout.activity_simple); 9 10 lv = (ListView) findViewById(R.id.lv_simple); 11 //构造一个数组 12 list = Constant.setList(); 13 SimpleAdapter adapter = new SimpleAdapter(this//上下文 14 ,list//需要显示的数据 15 ,R.layout.activity_simple_item //用来显示的布局 16 , new String[]{"name","str_class","address"}//将数据映射到空间中去 17 ,new int[]{R.id.tv_sim_item_name,R.id.tv_sim_item_class,R.id.tv_sim_item_address} 18 ); 19 20 //绑定数据源 21 lv.setAdapter(adapter); 22 //监听事件 23 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 24 @Override 25 public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 26 Toast.makeText(SimpleActivity.this, "第 " + i+1 + "行,值是 " + view.toString(), Toast.LENGTH_LONG).show(); 27 } 28 }); 29 } 30 }
3、通用的基础适配器BaseAdapter,直接用2的布局,
(1) 定义一个适配器MyAdapter.java
1 public class Myadapter extends BaseAdapter { 2 private Context context; 3 private List<HashMap<String,Object>> list; 4 public Myadapter(Context context,List<HashMap<String,Object>> list){ 5 this.context = context; 6 this.list = list; 7 } 8 9 @Override 10 public int getCount() { 11 return list.size(); 12 } 13 14 @Override 15 public Object getItem(int position) { 16 return list.get(position); 17 } 18 19 @Override 20 public long getItemId(int position) { 21 return position; 22 } 23 24 @Override 25 public View getView(final int position, View view, ViewGroup viewGroup) { 26 ViewHoder hoder = null; 27 if(view == null){ 28 view = View.inflate(context,R.layout.activity_simple_item,null); 29 hoder = new ViewHoder(); 30 31 hoder.name = (TextView) view.findViewById(R.id.tv_sim_item_name); 32 hoder.str_class = (TextView) view.findViewById(R.id.tv_sim_item_class); 33 hoder.address = (TextView) view.findViewById(R.id.tv_sim_item_address); 34 //绑定ViewHolder对象 35 view.setTag(hoder); 36 }else { 37 hoder = (ViewHoder) view.getTag(); 38 } 39 hoder.name.setText(list.get(position).get("name").toString()); 40 hoder.str_class.setText(list.get(position).get("str_class").toString()); 41 hoder.address.setText(list.get(position).get("address").toString()); 42 hoder.str_class.setOnClickListener(new View.OnClickListener() { 43 @Override 44 public void onClick(View view) { 45 Toast.makeText(context, "第 " + position + "行,name:" + list.get(position).get("name").toString(), Toast.LENGTH_SHORT).show(); 46 } 47 }); 48 return view; 49 } 50 51 public final class ViewHoder{ 52 public TextView name; 53 public TextView str_class; 54 public TextView address; 55 } 56 }
(2)Activity文件和Adapter进行绑定
1 public class MyAdapterActivity extends Activity { 2 private ListView lv ; 3 private List<HashMap<String,Object>> list; 4 @Override 5 protected void onCreate(Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.activity_simple);//和simple共用 8 //setContentView(R.layout.activity_my_adapter); 9 list = Constant.setList(); 10 lv = (ListView) findViewById(R.id.lv_simple); 11 Myadapter myadapter = new Myadapter(this, list); 12 lv.setAdapter(myadapter); 13 //如果需要下面可用,需要在activity_simple_item.xml把控件焦点置为失焦 14 // lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 15 // @Override 16 // public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 17 // Log.i("","第 "+i+"行,name:"+list.get(i+1).get("name").toString()); 18 // } 19 // }); 20 } 21 }
都说岁月是一把杀猪刀,美好的一天即将又要开始了,明天还要上班,写得比较简单,主要是为了帮助本人学习记忆,如果对你也有用处,那更好,如果有不明白之处,欢迎留言。如果发现有错误,不管大神和小神路过,请告诉我,以便及时改过,愿我们每天都进步一点点。
时间: 2024-10-23 22:02:09