Android基础--ListView的显示几种方式
来源:互联网 发布:淘宝网主要特色 编辑:程序博客网 时间:2024/06/07 09:54
目前常用的ListView显示的方式有三种
【1】BaseAdapter
【2】ArrayAdapter
【3】SimpleAdapter
通过API Code 可以看出,实际上ArrayAdapter,SimpleAdater都是继承了BaseAdapter.所以这里主要讨论BaseAdapter里面的方法。
BaseAdapter的方法主要有四个
【1.1】getCount() //当前数据的总条数
【1.2】getItem(int position) //获取某一项
【1.3】getItemId(int position) //获取某一项的ID
【1.4】getView(int position,View convertView,ViewGroup parent)//生成一个View的视图
若要实现ListView的显示,必须实现 getCount和getView 的方法
首先基于BaseAdapter来显示ListView
主页面布局
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="listview.wineworld.lizhijie.listviewadapter.MainActivity"> <Button android:text="查询" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="selectList" /> <Button android:text="ArrayAdapter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="showArrayAdapter"/> <Button android:text="SimpleAdapter" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="showSimpleAdapter"/> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView></LinearLayout>
ListView 实际上是有一项一项的数据组成的,那么就需要抽象出来一个item
Item的布局如下
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/item" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/name" android:text="2345645" android:textColor="#000000" android:layout_width="0dp" android:layout_weight="1" android:layout_height="wrap_content" /> <TextView android:id="@+id/phone" android:textColor="#000000" android:text="2345645" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /></LinearLayout>
MainActivity
private ListView listView; private DBHelper dbHelper; private List<Person> personList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); dbHelper=new DBHelper(getApplicationContext()); listView=(ListView) findViewById(R.id.lv); personList=new ArrayList<>(); } //显示ArrayAdapter public void showArrayAdapter(View view){ Intent intent=new Intent(getApplicationContext(),ArrayAdapterActivity.class); startActivity(intent); } //显示SimpleAdapter public void showSimpleAdapter(View view){ Intent intent=new Intent(getApplicationContext(),SimpleAdapterActivity.class); startActivity(intent); } public void selectList(View view){ SQLiteDatabase db=dbHelper.getReadableDatabase(); Cursor cursor= db.query("person",null,null,null,null,null,null); if(cursor!=null&&cursor.getCount()>0){ while (cursor.moveToNext()){ String name=cursor.getString(1); String phone=cursor.getString(2); Person person=new Person(); person.setName(name); person.setPhone(phone); personList.add(person); } listView.setAdapter(new MyAdapter()); } } private class MyAdapter extends BaseAdapter{ @Override public int getCount() { return personList.size(); } @Override public Object getItem(int i) { return null; } @Override public long getItemId(int i) { return 0; } @Override public View getView(int i, View view, ViewGroup viewGroup) { View newView; if(view==null){ newView=View.inflate(getApplicationContext(),R.layout.item,null); //打气筒 }else{ newView=view; } TextView tv_name=newView.findViewById(R.id.name); TextView tv_phone=newView.findViewById(R.id.phone); Person person=personList.get(i); tv_name.setText(person.getName()); tv_phone.setText(person.getPhone()); return newView; } }
ArrayAdapter 选择构造函数的时候,需要注意你当前显示数据的MODEL,选择构造函数是带MODEL,否则是无法执行getView()方法
布局如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_array_adapter" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="listview.wineworld.lizhijie.listviewadapter.ArrayAdapterActivity"> <ListView android:id="@+id/array_lv" android:layout_width="match_parent" android:layout_height="match_parent" > </ListView></LinearLayout>
实现方式
private List<Person> personList; private DBHelper dbHelper; private ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_array_adapter); lv=(ListView)findViewById(R.id.array_lv); dbHelper=new DBHelper(getApplicationContext()); personList=getPersonList(); if(personList.size()>0){ MyArrayAdapter myArrayAdapter=new MyArrayAdapter(getApplicationContext(),R.layout.item,personList); lv.setAdapter(myArrayAdapter); }else{ Toast.makeText(getApplicationContext(),"显示数据失败",Toast.LENGTH_LONG).show(); } } private class MyArrayAdapter extends ArrayAdapter<Person> { //选择构造函数的时候,如果是带类型的,就一定要将MODEL,放入到构造函数中 public MyArrayAdapter(Context context, int resource, List<Person> objects) { super(context, resource, objects); } //重写方法 @Override public View getView(int position, View convertView, ViewGroup parent) { View view; if(convertView==null){ view=View.inflate(getApplicationContext(),R.layout.item,null);//打气筒 }else{ view=convertView; } TextView tv_name=view.findViewById(R.id.name); TextView tv_phone=view.findViewById(R.id.phone); Person person=personList.get(position); tv_name.setText(person.getName()); tv_phone.setText(person.getPhone()); return view; } } private List<Person> getPersonList(){ List<Person> list=new ArrayList<>(); SQLiteDatabase db=dbHelper.getReadableDatabase(); Cursor cursor= db.query("person",null,null,null,null,null,null); if(cursor!=null&&cursor.getCount()>0){ while (cursor.moveToNext()){ String name=cursor.getString(1); String phone=cursor.getString(2); Person person=new Person(); person.setName(name); person.setPhone(phone); list.add(person); } } return list; }
【1.3】SimpleAdapter的显示方式比较简单,布局方式与ArrayAdapter是一致的,所以就直接上是实现方式了
private DBHelper dbHelper; private ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_simple_adapter); lv=(ListView) findViewById(R.id.simple_lv); dbHelper=new DBHelper(getApplicationContext()); SimpleAdapter simpleAdapter=new SimpleAdapter(getApplicationContext(),getPersonList(),R.layout.item, new String[]{"name","phone"},new int[]{R.id.name,R.id.phone}); lv.setAdapter(simpleAdapter); } private List<Map<String,String>> getPersonList(){ List<Map<String,String>> mapList=new ArrayList<>(); SQLiteDatabase db=dbHelper.getReadableDatabase(); Cursor cursor= db.query("person",null,null,null,null,null,null); if(cursor!=null&&cursor.getCount()>0){ while (cursor.moveToNext()){ Map<String,String> map=new HashMap<>(); String name=cursor.getString(1); String phone=cursor.getString(2); map.put("name",name); map.put("phone",phone); mapList.add(map); } } return mapList; }
注意的要点:
[1]这个几个适配器实际上都是为了将数据展现在ListView上,都是需要获取每一项数据,来拼接成当前页面的数据。
而,页面手机的可视范围是有限,所以,构造显示的数据的时候。可以将getView()里面的item重复利用。
就是代码中提到的“打气筒"
[2]使用什么适配器,就构造成需要的MODEL.可以直接重写getView()里面的方式,实现具体的业务
- Android基础--ListView的显示几种方式
- Android中ListView的几种常见优化方式
- 【Android基础】Android Toast显示消息的几种方法
- Android - 实现图片圆角显示的几种方式
- Android实现图片圆角显示的几种方式
- ListView的几种优化方式
- ListView的几种优化方式
- 实现ListView的几种方式
- Android中IPC的基础和几种常用方式
- android 网络基础和几种方式
- 几种图像显示的编码方式
- NSView显示颜色的几种方式
- Flex显示图片的几种方式
- html 图片显示的几种方式
- input 显示提示的几种方式。
- Android ListView显示数据的常见处理方式
- android 几种listview的整理
- android 日期+周几的显示方式,DigitalClock自定义
- 文章标题
- Angular2 父子路由问题
- 31、propertise解析之通用标记解析器与标记处理器
- uml关联符号说明
- 32、解析MetaObject
- Android基础--ListView的显示几种方式
- 1001. 求高精度幂
- 快速Android开发系列网络篇之Android-Async-Http
- 新的启程,新的进步。
- 33、SQL解析基础
- 《操作系统真象还原》-阅读笔记(中)
- 34、SqlSource解析
- Maven ProfileFilter 分环境运行
- 简单搜索poj 2243(水