007_02ListView与BaseAdapter的参数

 BaseAdapter适配器里有个getView()需要重写public View getView(int position,View converView,ViewGroup parent)

  这个convertView其实就是最关键的部分。原理上讲,当ListView滑动的过程中,会有item被滑出屏幕而不再被使用。这时候Android会回收这个条目的view 这个view也就是这里的convertView当item1被移除屏幕的时候。我们会重新new一个View给新显示的item_new,而如果使用了这个convertView 我们其实可以复用它,这样就省去了new View的大量开销。

 1 package com.example.showdatafromsqlite;
 2 
 3  
 4 public class UserPhone {
 5 
 6      private int id ;
 7      private String name;
 8      private String telephone;
 9     public int getId() {
10         return id;
11     }
12     public void setId(int id) {
13         this.id = id;
14     }
15     public String getName() {
16         return name;
17     }
18     public void setName(String name) {
19         this.name = name;
20     }
21     public String getTelephone() {
22         return telephone;
23     }
24     public void setTelephone(String telephone) {
25         this.telephone = telephone;
26     }
27     @Override
28     public String toString() {
29         return "UserPhone [id=" + id + ", name=" + name + ", telephone="
30                 + telephone + "]";
31     }
32     public UserPhone(int id, String name, String telephone) {
33         super();
34         this.id = id;
35         this.name = name;
36         this.telephone = telephone;
37     }
38     public UserPhone() {
39         super();
40         // TODO Auto-generated constructor stub
41     }
42  
43 }
44 
45 UserPhone.java
 1 package com.example.showdatafromsqlite;
 2 
 3 import android.content.ContentValues;
 4 import android.content.Context;
 5 import android.database.sqlite.SQLiteDatabase;
 6 import android.database.sqlite.SQLiteDatabase.CursorFactory;
 7 import android.database.sqlite.SQLiteOpenHelper;
 8 import android.text.AlteredCharSequence;
 9 import android.util.Log;
10 
11 public class MydbOpenHelper extends SQLiteOpenHelper {
12 
13     //
14     public MydbOpenHelper(Context context, String name, CursorFactory factory,
15             int version) {
16         super(context, name, factory, version);
17         // TODO Auto-generated constructor stub
18     }
19 
20     
21     //oncreate函数当数据库第一次被创建的时候会调用
22     @Override
23     public void onCreate(SQLiteDatabase db) {
24         // TODO Auto-generated method stub
25         String createtable = "create table userphone(id int, name varchar(20), telephone char(11));";
26         db.execSQL(createtable);
27         
28         
29         ContentValues c = new ContentValues();
30         //id int ,name string ,password string
31         for(int i=1;i<50;i++){
32             c.put("id", i);
33             c.put("name", "user"+i);
34             if(i<10)
35                 c.put("telephone", "1371111111"+i);
36             else {
37                 c.put("telephone", "137111111"+i);
38             }
39             db.insert("userphone", null, c);
40         }
41     
42         System.out.println("MydbOpenHelper.onCreate()");
43     }
44 
45     
46     //onUpgrade当你的数据库版本升级的时候会调用
47     @Override
48     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
49         // TODO Auto-generated method stub
50         //更改数据库的表结构
51         //  user(id,name,password)  user(id,name,password,gender)
52         //
53         //  db.execSQL(""); 
54         Log.i("MydbOpenHelper", "oldervserion"+oldVersion+"newversion"+newVersion);
55         System.out.println("MydbOpenHelper.onUpgrade()");
56 
57     }
58 
59 }
60 
61 MydbOpenHelper.java
 1 package com.example.showdatafromsqlite;
 2 
 3 import java.util.ArrayList;
 4 import java.util.List;
 5 
 6 import android.app.Activity;
 7 import android.database.Cursor;
 8 import android.database.sqlite.SQLiteDatabase;
 9 import android.os.Bundle;
10 import android.util.Log;
11 import android.view.Menu;
12 import android.view.MenuItem;
13 import android.view.View;
14 import android.view.ViewGroup;
15 import android.widget.BaseAdapter;
16 import android.widget.ListView;
17 import android.widget.TextView;
18 
19 public class MainActivity extends Activity {
20     List <UserPhone> list = new ArrayList <UserPhone>();
21     UserPhone userPhone;
22     
23     @Override
24     protected void onCreate(Bundle savedInstanceState) {
25         super.onCreate(savedInstanceState);
26         setContentView(R.layout.activity_main);
27         
28         MydbOpenHelper helper = new MydbOpenHelper(this, "telphone.db", null, 1);
29         SQLiteDatabase db = helper.getReadableDatabase();
30         Cursor c = db.rawQuery("select * from userphone", null);
31         
32         while(c.moveToNext()){
33             int id = c.getInt(0);
34             String name = c.getString(1);
35             String telephone = c.getString(2);
36             
37             Log.i("test", id+","+name+","+telephone);
38             userPhone = new UserPhone(id, name, telephone);
39             list.add(userPhone);
40         }
41         db.close();
42         
43         ListView listView = (ListView)findViewById(R.id.listview);
44         BaseAdapter ba = new BaseAdapter(){
45 
46             @Override
47             public int getCount() {
48                 // TODO Auto-generated method stub
49                 return list.size();
50             }
51 
52             @Override
53             public Object getItem(int position) {
54                 // TODO Auto-generated method stub
55                 return null;
56             }
57 
58             @Override
59             public long getItemId(int position) {
60                 // TODO Auto-generated method stub
61                 return 0;
62             }
63 
64             @Override
65             public View getView(int position, View convertView, ViewGroup parent) {
66                 View v;
67                 if(convertView == null){
68                     v = View.inflate(MainActivity.this, R.layout.itemlayout, null);
69                 }
70                 else{
71                     v = convertView;
72                 }
73                 //往该布局内填充数据
74                 UserPhone user = list.get(position);
75                 TextView tv_id = (TextView) v.findViewById(R.id.tv_id);
76                 TextView tv_name = (TextView) v.findViewById(R.id.tv_name);
77                 TextView tv_telephone = (TextView) v.findViewById(R.id.tv_telephone);
78                 
79                 tv_id.setText(user.getId() + "");
80                 tv_name.setText(user.getName());
81                 tv_telephone.setText(user.getTelephone());
82                 return v;
83             }
84             
85         };
86         listView.setAdapter(ba);
87         
88         
89     }
90 
91 
92 }
93 
94 MainActivity.java
 1 <LinearLayout 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     android:paddingBottom="@dimen/activity_vertical_margin"
 6     android:paddingLeft="@dimen/activity_horizontal_margin"
 7     android:paddingRight="@dimen/activity_horizontal_margin"
 8     android:paddingTop="@dimen/activity_vertical_margin"
 9     tools:context="com.example.showdatafromsqlite.MainActivity" 
10     android:orientation="vertical">
11 
12     <TextView
13         android:layout_width="wrap_content"
14         android:layout_height="wrap_content"
15         android:text="@string/hello_world" />
27     <ListView
28         android:id="@+id/listview"
29         android:layout_width="fill_parent"
30         android:layout_height="wrap_content"
31         android:dividerHeight="5dp" >
32     </ListView>
33    
34 </LinearLayout>
35 
36 activity_main.xml
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     android:orientation="vertical" >
 6     <TextView 
 7         android:id="@+id/tv_id"
 8          android:layout_width="fill_parent"
 9          android:layout_height="wrap_content"
10         />
11     <LinearLayout
12          android:layout_width="fill_parent"
13          android:layout_height="wrap_content"
14          android:orientation="horizontal">
15         <TextView 
16         android:id="@+id/tv_name"
17          android:layout_width="wrap_content"
18          android:layout_height="wrap_content"
19         />
20          <TextView 
21         android:id="@+id/tv_telephone"
22          android:layout_width="wrap_content"
23          android:layout_height="wrap_content"
24         />
25         
26     </LinearLayout>
27 </LinearLayout>
28 
29 itemlayout.xml

效果图:(可上下滑动)

物随心转,境由心造,一切烦恼皆由心生。
原文地址:https://www.cnblogs.com/woodrow2015/p/4515609.html