码迷,mamicode.com
首页 > 数据库 > 详细

ListView显示Sqlite的数据美化版与性能优化

时间:2016-04-21 06:56:45      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

在上一篇文章中,我们已经实现在listview显示数据库内容的。但是我们listview中,排版不是很好看,所以这篇文章呢,我们来对listveiw进行美化。哈哈,说白了,就是对listview添加一个布局文件,然后把布局文件转化为view对象显示出来。

这里代码我们不贴全部,就贴上新增加的布局文件和有做修改的方法。

先看图片吧。

技术分享

然后看listview的布局文件,其实从这个例子,大家可以知道,listview的项目可以显示很复杂的东西,只要是view对象都是可以显示的额,只要你的布局文件写好了。都可以。

list_layout.xml

<?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" >

    <TextView
        android:textSize="20sp"
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="名字" />

    <LinearLayout
        android:layout_alignParentRight="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/age"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="年龄" />

        <TextView
            android:id="@+id/phone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="手机" />
    </LinearLayout>

</RelativeLayout>

 MainActivity.java  这里只贴出修改的地方,其实也就是修改了 getView的几条语句而已。这里写出 getView方法的内容

public View getView(int position, View convertView, ViewGroup parent) {
            Girl girl = girList.get(position); 
        /*    TextView tv = new TextView(MainActivity.this);
            tv.setTextSize(18);
            //获取集合中的元素
            tv.setText(girl.toString());*/
            View v ;
            /*
             * 做判断是为了listview的性能优化,converView表示一个缓冲区域。
             * 在看过的条目,离开屏幕后,其实会被缓冲起来,所以我们没必要
             * 每一次都去 填充,可以先判断是否有缓冲
             * 
             * */
            if(convertView ==null){
            //把ListView的布局文件填充为View对象
               v = View.inflate(MainActivity.this, R.layout.list_layout, null);
            }else{
                v = convertView ;
            }
            /*v.findViewById(R.id.name);前面加v是因为找的布局文件元素是listView对象的
             * 而v加载的布局文件就是listview的布局文件
            */
            TextView name = (TextView)v.findViewById(R.id.name);
            name.setText(girl.getName());
            TextView age = (TextView) v.findViewById(R.id.age);
            age.setText(girl.getAge()+"");
            TextView phone = (TextView)v.findViewById(R.id.phone);
            phone.setText(girl.getPhone());
            return v;
        }
        

 

ListView显示Sqlite的数据美化版与性能优化

标签:

原文地址:http://www.cnblogs.com/linfenghp/p/5415153.html

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