Android入门:ListView(SimpleAdapter实现)

ListView是类似于将一个界面分为一行一行,如下图:

Android入门:ListView(SimpleAdapter实现)



注意:listView.getItemAtPosition(int pos)内部调用了adapter.getItem(int position)方法,而每种适配器返回的类型都是不一样的:

当SimpleAdapter返回Map<String,Object>

SimpleCursorAdapter返回Cursor;

继承BaseAdapter返回自己实现的类型;


一般ListView都是用来显示列表的,一般列表的数据都是来自数据库的,因此我们这里假设前面已经实现了一个DBService类,里面存在pageQuery(int offset,int length);

比如dbservice.pageQuery(3,5);表示跳过3个记录,插入5条记录;


Android入门:ListView(SimpleAdapter实现)


main.xml


[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <LinearLayout  
  8.         android:layout_width="wrap_content"//注意一定要wrap_content  
  9.         android:layout_height="wrap_content"//注意一定要wrap_content  
  10.         android:orientation="horizontal" >  
  11.         <TextView  
  12.         android:layout_width="100dp"  
  13.         android:layout_height="wrap_content"  
  14.         android:text="ID" />  
  15.         <TextView  
  16.         android:layout_width="100dp"  
  17.         android:layout_height="wrap_content"  
  18.         android:text="NAME" />  
  19.         <TextView  
  20.         android:layout_width="100dp"  
  21.         android:layout_height="wrap_content"  
  22.         android:text="AGE" />  
  23.     </LinearLayout>  
  24.     <ListView  
  25.         android:id="@+id/listview"  
  26.         android:layout_width="wrap_content"  
  27.         android:layout_height="fill_parent" >  
  28.     </ListView>  
  29.   
  30. </LinearLayout>  

item.xml


[html] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="horizontal" >  
  6.     <TextView  
  7.         android:id="@+id/id1"  
  8.         android:layout_width="100dp"  
  9.         android:layout_height="wrap_content"  
  10.         />  
  11.     <TextView  
  12.         android:id="@+id/name"  
  13.         android:layout_width="100dp"  
  14.         android:layout_height="wrap_content"  
  15.         />  
  16.     <TextView  
  17.         android:id="@+id/age"  
  18.         android:layout_width="100dp"  
  19.         android:layout_height="wrap_content"  
  20.         />  
  21. </LinearLayout>  

MainActivity.java


[java] view plain copy
  1. package org.xiazdong.db;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.List;  
  6. import java.util.Map;  
  7.   
  8. import org.xiazdong.db.domain.Person;  
  9. import org.xiazdong.db.service.DBService;  
  10.   
  11. import android.app.Activity;  
  12. import android.os.Bundle;  
  13. import android.util.Log;  
  14. import android.widget.ListView;  
  15. import android.widget.SimpleAdapter;  
  16.   
  17. public class MainActivity extends Activity {  
  18.     private ListView listView;  
  19.     @Override  
  20.     public void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.main);  
  23.         listView = (ListView)this.findViewById(R.id.listview);  
  24.         DBService service = new DBService(this);  
  25.         List<Person> persons = service.pageQuery(010);  
  26.         List<HashMap<String,Object>> data = new ArrayList<HashMap<String,Object>>();  
  27.         for(Person person:persons){  
  28.             HashMap<String,Object>map = new HashMap<String,Object>();  
  29.             map.put("id", person.getId());  
  30.             map.put("name", person.getName());  
  31.             map.put("age", person.getAge());  
  32.             data.add(map);  
  33.         }  
  34.         System.out.println(data);  
  35.         SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item, new String[]{"id","name","age"}, new int[]{R.id.id1,R.id.name,R.id.age});  
  36. //data表示显示的数据,一个Map为一行,List<Map>表示多行  
  37. //R.layout.item表示一个item的布局  
  38. //new String[]{"id","name","age"}表示将key="id"的值映射到R.id.id1上  
  39.         listView.setAdapter(adapter);  
  40.     }  
  41. }  

总结:SimpleAdapter不需要内部实现Adapter,只能实现每个item布局都一样的列表;

二、设置每个Item的监听器


SimpleAdapter:

[java] view plain copy
  1. private OnItemClickListener listener = new OnItemClickListener(){  
  2.     @Override  
  3.     public void onItemClick(AdapterView<?> parent, View view, int position,//parent就是ListView,view表示Item视图,position表示数据索引  
  4.             long id) {  
  5.         ListView lv = (ListView)parent;  
  6.         HashMap<String,Object> person = (HashMap<String,Object>)lv.getItemAtPosition(position);//SimpleAdapter返回Map  
  7.         Toast.makeText(getApplicationContext(), person.toString(), Toast.LENGTH_SHORT).show();  
  8.     }     
  9. };  
  10. listView.setOnItemClickListener(listener);  


SimpleCursorAdapter:

[java] view plain copy
  1. Cursor cursor = (Cursor)lv.getItemAtPosition(position);  

你可能感兴趣的:(android)