Android采用ListView实现数据列表显示

来源:互联网 发布:购买淘宝网店铺 编辑:程序博客网 时间:2024/04/30 21:40

要将数据库中的数据列表显示在屏幕上,我们要使用ListView这个控件,当用户从数据库中取出数据时,要将数据绑定到显示控件上,如何绑定呢,我们需要创建适配器进行绑定,创建适配器有两种方式:

第一种是用SimpleAdapter创建(要求绑定的数据是List<HashMap<String, Object>>数据类型)

第二种是用SimpleCursorAdapter创建(要求绑定的数据是Cursor数据类型)

显示效果如图所示:

 

界面布局:

item.xml

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!--item -->  
  3. <LinearLayout  
  4.   xmlns:android="http://schemas.android.com/apk/res/android"  
  5.   android:orientation="horizontal"  
  6.   android:layout_width="fill_parent"  
  7.   android:layout_height="fill_parent">  
  8.   <!-- 名称 -->  
  9.   <TextView  
  10.    android:layout_width="130dp"  
  11.    android:layout_height="wrap_content"  
  12.    android:id="@+id/name"  
  13.   />  
  14.   <!-- 电话 -->  
  15.   <TextView  
  16.    android:layout_width="150dp"  
  17.    android:layout_height="wrap_content"  
  18.    android:id="@+id/phone"  
  19.   />  
  20.   <!-- 存款 -->  
  21.   <TextView  
  22.    android:layout_width="fill_parent"  
  23.    android:layout_height="wrap_content"  
  24.    android:id="@+id/amount"  
  25.   />  
  26. </LinearLayout>  


main.xml

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.  <!-- 标题 -->  
  8.  <LinearLayout  
  9.   android:orientation="horizontal"  
  10.   android:layout_width="fill_parent"  
  11.   android:layout_height="wrap_content">  
  12.     
  13.   <TextView  
  14.    android:layout_width="130dp"  
  15.    android:layout_height="wrap_content"  
  16.    android:text="姓名"  
  17.   />  
  18.    
  19.    <TextView  
  20.    android:layout_width="150dp"  
  21.    android:layout_height="wrap_content"  
  22.    android:text="电话"  
  23.   />  
  24.     
  25.   <TextView  
  26.    android:layout_width="fill_parent"  
  27.    android:layout_height="wrap_content"  
  28.    android:text="存款"  
  29.   />  
  30.      
  31. </LinearLayout>  
  32.  <!-- ListView控件 -->  
  33. <ListView    
  34.     android:layout_width="fill_parent"   
  35.     android:layout_height="fill_parent"   
  36.     android:id="@+id/listView"  
  37.     />  
  38. </LinearLayout>  

 使用SimpleAdapter进行数据绑定

public class MainActivity extends Activity {    private PersonService service;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        service = new PersonService(this);        ListView listView = (ListView) this.findViewById(R.id.listView);                //获取到集合数据        List<Person> persons = service.getScrollData(0, 10);        List<HashMap<String, Object>> data = new ArrayList<HashMap<String,Object>>();        for(Person person : persons){        HashMap<String, Object> item = new HashMap<String, Object>();        item.put("id", person.getId());        item.put("name", person.getName());        item.put("phone", person.getPhone());        item.put("amount", person.getAmount());        data.add(item);        }       //创建SimpleAdapter适配器将数据绑定到item显示控件上       SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item,         new String[]{"name", "phone", "amount"}, new int[]{R.id.name, R.id.phone, R.id.amount});       //实现列表的显示       listView.setAdapter(adapter);       //条目点击事件       listView.setOnItemClickListener(new ItemClickListener());    }       //获取点击事件        private final class ItemClickListener implements OnItemClickListener{public void onItemClick(AdapterView<?> parent, View view, int position, long id) {ListView listView = (ListView) parent;HashMap<String, Object> data = (HashMap<String, Object>) listView.getItemAtPosition(position);String personid = data.get("id").toString();Toast.makeText(getApplicationContext(), personid, 1).show();}    }}

 

 使用SimpleCursorAdapter进行数据绑定

public class MainActivity extends Activity {    private PersonService service;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        service = new PersonService(this);        ListView listView = (ListView) this.findViewById(R.id.listView);        //获取游标        Cursor cursor = service.getCursorScrollData(0, 10);        //创建SimpleCursorAdapter适配器将数据绑定到item显示控件上        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.item, cursor,         new String[]{"name", "phone", "amount"}, new int[]{R.id.name, R.id.phone, R.id.amount});        listView.setAdapter(adapter);        //条目点击事件        listView.setOnItemClickListener(new ItemClickListener());    }        private final class ItemClickListener implements OnItemClickListener{public void onItemClick(AdapterView<?> parent, View view, int position, long id) {ListView listView = (ListView) parent;Cursor cursor = (Cursor) listView.getItemAtPosition(position);String personid = String.valueOf(cursor.getInt(cursor.getColumnIndex("_id")));Toast.makeText(getApplicationContext(), personid, 1).show();}    }}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 房间里有鼻涕虫怎么办 花盆泥土有虫怎么办 菠萝蜜粘液吃了怎么办 被鼻涕虫咬了怎么办 家里水斗有鼻涕虫怎么办? 厨房里潮虫子怎么办 楼房有潮虫子怎么办 家里的鼻涕虫泛滥成灾怎么办 菜叶上有蜗牛怎么办 家里面有鼻涕虫怎么办 菜地里的蜗牛怎么办 菜地里有蜗牛怎么办 菜地里有虫子怎么办 心情焦躁多想怎么办 打胎后心情焦躁怎么办 心情不好想发火怎么办 孩子心烦气燥怎么办 心烦气燥睡不着怎么办 心烦心燥睡不着怎么办 小孩心烦气燥怎么办 情绪总是很烦躁怎么办 情绪不好爱生气怎么办 心里每天烦躁该怎么办 心情低落想哭怎么办 心老是烦躁急怎么办 易烦躁睡眠不好怎么办 天气热心情烦躁怎么办 嘴巴里有溃疡怎么办 嘴巴里面反复烂怎么办 多处口腔溃烂怎么办 口腔黏膜破了怎么办 有鼻炎鼻子痒怎么办 感冒就清鼻涕怎么办 怀孕感冒流鼻涕打喷嚏怎么办 过敏性鼻炎打喷嚏流鼻涕怎么办 宝宝黄鼻涕鼻塞怎么办 鼻炎总是打喷嚏流鼻涕怎么办 新生儿流黄鼻涕怎么办 感冒鼻塞流鼻涕喷嚏怎么办 宝宝总是打喷嚏流鼻涕怎么办 不停的打喷嚏流鼻涕怎么办