ListVeiw: 用来展示列表的View。
适配器 : 用来把数据映射到ListView上
数据: 具体的将被映射的字符串,图片,或者基本组件
SimpleAdapter适合比较简单的ListView,比如每一个item中控件比较少,或者控件中不需要相应事件。其他时候一般使用自定义Adapter。
主代码如下:
public class MainActivity extends Activity { private ListView listview; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listview = (ListView)findViewById(R.id.listview); //准备数据。使用SimpleAdapter必须用List<Map<...>>存储数据 List<Map<String, String>> data = new ArrayList<Map<String,String>>(); for(int i = 0;i < 10;i++){ Map<String, String> map = new HashMap<String, String>(); map.put("text", i+""); map.put("picture", R.drawable.ic_launcher+""); map.put("butName", "Button"); data.add(map); } MyAdapter adapter = new MyAdapter(data, getApplicationContext()); //使用自定义Adapter /* 使用SimpleAdapter SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(), data, R.layout.item, new String[]{"text","picture","butName"}, new int[]{R.id.text,R.id.image,R.id.but}); */ listview.setAdapter(adapter); } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_margin="10dp" /> <Button android:id="@+id/but" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_toRightOf="@+id/text" android:layout_margin="10dp"/> <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_margin="10dp" android:layout_centerHorizontal="true" /> </RelativeLayout>
自定义Adapter:
public class MyAdapter extends BaseAdapter{ private List<Map<String,String>> list; private Context context; private Map<String, String> map = new HashMap<String, String>(); public MyAdapter(List<Map<String,String>> list,Context context){ //通过构造函数得到适配的数据。得到Context主要是为了在此类中得到布局资源。这样就可以使用布局中的控件了。 this.context = context; this.list = list; } @Override public int getCount() { // TODO Auto-generated method stub return list.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub convertView = (View)LayoutInflater.from(context).inflate(R.layout.item, null); //得到布局资源 TextView textView = (TextView)convertView.findViewById(R.id.text); Button but = (Button)convertView.findViewById(R.id.but); ImageView image = (ImageView)convertView.findViewById(R.id.image); map = list.get(position); textView.setText(map.get("text")); but.setText(map.get("butName")); image.setImageResource(Integer.parseInt(map.get("picture"))); return convertView; //必须要先初始化,要不然报NullPointException且异常信息不出现出错位置。 } }