码迷,mamicode.com
首页 > 移动开发 > 详细

Android学习笔记_ListView(列表视图)(一)

时间:2015-01-14 21:18:07      阅读:313      评论:0      收藏:0      [点我收藏+]

标签:

一、概念。

  ListView是以垂直列表的形式显示所有列表项。

二、使用

  假设数据库中有50个数据信息(persons,包括name、number、money),通过ListView显示出来。

  1、向数据库中50个person信息。具体添加方式可参考数据库章节介绍,采用数据库例子中的dao2的add方法结合for循环增加。

添加数据代码如下:

技术分享
1 public void testAdd() throws Exception {
2         PersonDao2 dao = new PersonDao2(getContext());
3         long number = 885900000l;
4         Random random = new Random();
5         for(int i=0;i<50;i++){
6             dao.add("wangwu"+i, Long.toString(number+i),random.nextInt(5000));
7         }    
8     }
View Code

  2、将数据库信息展现至界面。

  (1)采用ListView组件实现。ListView是典型的MVC设计模式。M:mode 数据类型,也就是List集合;V:view 视图,也就是ListView;C:controller 控制器,adapter 数据适配器。

    ①.在布局(Layout)文件中使用线性布局(LinearLayout),垂直方向。

    ②.增加ListView控件,设置id和宽高(填充窗体)

    ③.在主代码中创建并通过findViewById找到ListView控件;

    ④.采用ListView的setAdapt(adapter(ListAdapter接口))方法。由于参数adaper是接口类型的,所以需要创建接口的实现类。

    ⑤.创建一个类(MyAdapt)实现ListAdapter接口,并实现接口中为实现的方法。由于ListAdapter接口中为实现的方法比较多,因此新建的类(MyAdapter)只要去继承ListAdapter接口的简单实现类(BaseAdapter),由于BaseAdapter是抽象类,所以需要实现其中的抽象方法。(规律:如果一个接口的实现类非常多,一般就会存在BaseXXX、SimpleXXX、DefaultXXX等简单实现类,然后只要继承这些简单实现类即可)。

    ⑥.BaseAdapter抽象类里的抽象方法。getCount():控制ListView里面共有多少个条目,也就是从数据库返回的List集合的条目数量,即条目个数=集合的size;getView(int position, View convertView, ViewGroup parent),获取到一个view对象,用来展现数据集合某一个位置对应的数据,方法参数position表示当前数据在集合中的位置。

    ⑦.在getView(int position, View convertView, ViewGroup parent)方法中,new出一个TextView对象并通过getApplicationContext()方法放入上下文再设置样式,然后通过person的get(position)方法得到position位置对应的person对象,再通过position对象的toString()方法获得String对象并通过TextView对象的setText方法加至TextView对象中,最后该方法返回TextView对象。

通过ListVie组件展示代码如下:

主代码:

技术分享
 1 public class MainActivity extends Activity {
 2     private ListView lv;
 3     private List<Person> persons;
 4 
 5     @Override
 6     protected void onCreate(Bundle savedInstanceState) {
 7         super.onCreate(savedInstanceState);
 8         setContentView(R.layout.activity_main);
 9         LinearLayout ll_root = (LinearLayout) findViewById(R.id.ll_root);
10         PersonDao dao = new PersonDao(this);
11         persons = dao.findAll();
12         
13         lv = (ListView) findViewById(R.id.lv);
14         lv.setAdapter(new MyAdapter());        
15     }
16     
17     public class MyAdapter extends BaseAdapter{
18         private static final String TAG = "MyaAdapter";
19         
20         @Override
21         public int getCount() {
22             return persons.size();
23         }
24 
25         @Override
26         public Object getItem(int position) {
27             // TODO Auto-generated method stub
28             return null;
29         }
30 
31         @Override
32         public long getItemId(int position) {
33             // TODO Auto-generated method stub
34             return 0;
35         }
36 
37         @Override
38         public View getView(int position, View convertView, ViewGroup parent) {
39             Log.i(TAG, "返回View对象,位置:"+position);
40             TextView tv = new TextView(getApplicationContext());
41             tv.setTextSize(20);
42             tv.setTextColor(color.black);
43             Person person =  persons.get(position);
44             tv.setText(person.toString());
45             return tv;
46         }        
47     }
48 }
View Code

layout代码:

技术分享
 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:id="@+id/ll_root"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical"
 7     tools:context=".MainActivity" >
 8     
 9     <ListView 
10         android:id="@+id/lv"
11         android:layout_height="match_parent"
12         android:layout_width="match_parent"></ListView>
13 
14 </LinearLayout>
View Code

   

  (2)采用非ListView组件实现

    ①.界面定义为线性布局LinearLayout,定义id;

    ②.在主代码中通过findviewbyid找到该线性布局;

    ③.new出一个PersonDao对象,通过PersonDao对象里面的findAll()方法得到所有person对象的集合;

    ④.采用for循环遍历person集合;

    ⑤.在for中将每个person对象通过tostring()方法转成String字符串,并new出一个TextView()对象,设置TextView()对象的字体大小、颜色等相关数据,然后用TextView()对象的setText()方法将每个person信息加至对应的TextView()中。

    ⑥.由于LinearLayout是个View对象的容器,所以可以采用LinearLayout的addView(View chind)方法动态地将每个TextView()对象增加至LinearLayout中。

    ⑦.采用ScrollView对象包裹LinearLayout对象,实现多数据的上下滚动显示效果。

主程序代码如下:

技术分享
 1 public class MainActivity extends Activity {
 2 
 3     @Override
 4     protected void onCreate(Bundle savedInstanceState) {
 5         super.onCreate(savedInstanceState);
 6         setContentView(R.layout.activity_main);
 7         LinearLayout ll_root = (LinearLayout) findViewById(R.id.ll_root);
 8         PersonDao dao = new PersonDao(this);
 9         List<Person> persons = dao.findAll();
10         for(Person person : persons){
11             String info = person.toString(); 
12             TextView tv = new TextView(this);
13             tv.setTextSize(20);
14             tv.setTextColor(Color.BLACK);
15             tv.setText(info);
16             ll_root.addView(tv);
17         }
18     }
19 }
View Code

Layout代码如下:

技术分享
 1 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent" >
 5 
 6     <LinearLayout
 7         android:id="@+id/ll_root"
 8         android:layout_width="match_parent"
 9         android:layout_height="match_parent"
10         android:orientation="vertical"
11         tools:context=".MainActivity" >
12     </LinearLayout>
13 
14 </ScrollView>
View Code

 

Android学习笔记_ListView(列表视图)(一)

标签:

原文地址:http://www.cnblogs.com/Red-Shark/p/4222429.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!