android学习日记03--常用控件ListView
常用控件
8、ListView
列表视图,比如游戏的排行榜。列表数据可以根据屏幕大小自适应
列表的显示需要三个元素:
a、ListVeiw:用来展示列表的View。
b、适配器:用来把数据映射到ListView上的中介。
c、数据:具体的将被映射的字符串,图片,或者基本组件。
ListView用到两种适配器:
1、ArrayAdapter--简单适配器,只显示文字
2、SimpleAdapter--自定义适配器,可以显示自定义内容
3、SimpleCursorAdapter可以认为是SimpleAdapter对数据库的简单结合,可以方面的把数据库的内容以列表的形式展示出来,暂时不讲。
使用简单适配器可直接
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
this:当前context
android.R.layout.simple_list_item_1是系统的布局文件
list:ListView的各项数据
SimpleAdapter各项参数
new SimpleAdapter(context, data, resource, from, to)
context:当前context内容
data:ListView的各项数据
resource:ListView的每项布局
from:ListView的组件索引
to:ListView的组件ID
如:
new SimpleAdapter(MainActivity.this, list, R.layout.activity_main, new String[] {"item1","item2","item3","item4","item5"}
, new int[] {R.id.iv,R.id.bigtv,R.id.smalltv,R.id.btn,R.id.cb});
但是SimpleAdapter 不能触发组件的事件,还需
自己写适配器继承BaseAdapter
BaseAdapter主要有四个方法
getCount --列表每一项的长度
getItem
getItemId
getView --绘制每一项的具体组件
例子演示自定义MyAdapter
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout 3 android:orientation="horizontal" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 xmlns:android="http://schemas.android.com/apk/res/android" 7 > 8 <ImageView 9 android:id="@+id/iv" 10 android:layout_width="wrap_content" 11 android:layout_height="wrap_content" 12 /> 13 <LinearLayout 14 android:orientation="vertical" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 > 18 <TextView 19 android:layout_width="wrap_content" 20 android:layout_height="wrap_content" 21 android:textSize="20sp" 22 android:id="@+id/bigtv" 23 /> 24 25 <TextView 26 android:layout_width="wrap_content" 27 android:layout_height="wrap_content" 28 android:textSize="10sp" 29 android:id="@+id/smalltv" 30 /> 31 32 </LinearLayout> 33 34 <Button 35 android:layout_width="wrap_content" 36 android:layout_height="wrap_content" 37 android:text="button" 38 android:id="@+id/btn" 39 android:focusable="false" 40 /> 41 42 <CheckBox 43 android:layout_width="wrap_content" 44 android:layout_height="wrap_content" 45 android:id="@+id/cb" 46 android:focusable="false" 47 /> 48 49 50 </LinearLayout>
布局文件要特别说明两个属性
android:textSize="10sp" 设置text的大小
android:focusable="false" 设置焦点属性不可见
1 package com.example.listview2; 2 3 import java.util.List; 4 import java.util.Map; 5 6 import android.app.AlertDialog; 7 import android.content.Context; 8 import android.view.LayoutInflater; 9 import android.view.View; 10 import android.view.View.OnClickListener; 11 import android.view.ViewGroup; 12 import android.widget.BaseAdapter; 13 import android.widget.Button; 14 import android.widget.CheckBox; 15 import android.widget.CompoundButton; 16 import android.widget.CompoundButton.OnCheckedChangeListener; 17 import android.widget.ImageView; 18 import android.widget.TextView; 19 20 public class MyAdapter extends BaseAdapter { 21 22 23 private LayoutInflater mInflater; 24 private List<Map<String, Object>> list; 25 private int layId; 26 private String flag[]; 27 private int itemId[]; 28 29 30 31 public MyAdapter(Context context, List<Map<String, Object>> list, 32 int layId, String[] flag, int itemId[]) { 33 this.mInflater = LayoutInflater.from(context); 34 this.list = list; 35 this.layId = layId; 36 this.flag = flag; 37 this.itemId = itemId; 38 } 39 40 @Override 41 public int getCount() { 42 // TODO Auto-generated method stub 43 return list.size(); 44 } 45 46 @Override 47 public Object getItem(int arg0) { 48 // TODO Auto-generated method stub 49 return null; 50 } 51 52 @Override 53 public long getItemId(int arg0) { 54 // TODO Auto-generated method stub 55 return 0; 56 } 57 58 @Override 59 public View getView(int arg0, View arg1, ViewGroup arg2) { 60 // TODO Auto-generated method stub 61 arg1 = mInflater.inflate(layId, null); 62 for (int i = 0; i < flag.length; i++) { 63 if (arg1.findViewById(itemId[i]) instanceof ImageView) { 64 ImageView iv = (ImageView)arg1.findViewById(itemId[i]); 65 iv.setBackgroundResource((Integer) list.get(arg0).get(flag[i])); 66 }else if (arg1.findViewById(itemId[i]) instanceof TextView) { 67 TextView tv = (TextView)arg1.findViewById(itemId[i]); 68 tv.setText((String)list.get(arg0).get(flag[i])); 69 } 70 } 71 72 ((Button) arg1.findViewById(R.id.btn)) 73 .setOnClickListener(new OnClickListener() { 74 75 @Override 76 public void onClick(View v) { 77 // TODO Auto-generated method stub 78 new AlertDialog.Builder(MainActivity.ma) 79 .setTitle("自定义SimpleAdapter") 80 .setMessage("按钮成功触发监听事件!") 81 .show(); 82 } 83 }); 84 CheckBox cb = (CheckBox) arg1.findViewById(R.id.cb); 85 86 cb.setOnCheckedChangeListener(new OnCheckedChangeListener() { 87 88 @Override 89 public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 90 // TODO Auto-generated method stub 91 new AlertDialog.Builder(MainActivity.ma) 92 .setTitle("自定义 adapter") 93 .setMessage("触发选择框") 94 .show(); 95 } 96 }); 97 98 return arg1; 99 } 100 101 }
MyAdapter代码里
new AlertDialog.Builder(MainActivity.ma)
.setTitle("自定义 adapter")
.setMessage("触发选择框")
.show();
表示弹出对话框,与Toast不同的是 该对话框要按返回才会退出,而Toast是一闪而过的,对话框在下一讲详细阐述
MainActivity.ma是定义在MainActivity的静态变量指向this自己
MainActivity代码
1 package com.example.listview2; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 import java.util.List; 6 import java.util.Map; 7 8 import android.app.Activity; 9 import android.os.Bundle; 10 import android.view.Menu; 11 import android.widget.ListView; 12 import android.widget.SimpleAdapter; 13 14 public class MainActivity extends Activity { 15 private ListView lv; 16 private List<Map<String, Object>> list; 17 //private SimpleAdapter sp; 18 public static MainActivity ma; 19 @Override 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.activity_main); 23 24 ma = this; 25 26 lv = new ListView(this); 27 28 list = new ArrayList<Map<String,Object>>(); 29 30 Map<String, Object> map = new HashMap<String, Object>(); 31 map.put("item1", R.drawable.ic_launcher); 32 map.put("item2", "bigtv"); 33 map.put("item3", "smalltv"); 34 35 list.add(map); 36 37 38 //sp = new SimpleAdapter(MainActivity.this, list, R.layout.activity_main, new String[] {"item1","item2","item3","item4","item5"}, new int[] {R.id.iv,R.id.bigtv,R.id.smalltv,R.id.btn,R.id.cb}); 39 //lv.setAdapter(sp); 40 41 // --使用自定义适配器,可监听其ListView中每一项的事件监听 42 MyAdapter adapter = new MyAdapter(this, list, R.layout.activity_main, new String[] {"item1","item2","item3"}, new int[] {R.id.iv,R.id.bigtv,R.id.smalltv}); 43 // 为列表视图设置适配器(将数据映射到列表视图中) 44 lv.setAdapter(adapter); 45 // //显示列表视图 46 47 this.setContentView(lv); 48 49 } 50 51 52 @Override 53 public boolean onCreateOptionsMenu(Menu menu) { 54 // Inflate the menu; this adds items to the action bar if it is present. 55 getMenuInflater().inflate(R.menu.main, menu); 56 return true; 57 } 58 59 }
运行效果