当前位置:Gxlcms > 数据库问题 > ListView显示Sqlite的数据美化版与性能优化

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

时间:2021-07-01 10:21:17 帮助过:3人阅读

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的数据美化版与性能优化

标签:

人气教程排行