SQLite數據庫內容顯示到ListView


我們有了數據庫的內容之后,需要將其顯示到手機屏幕上時,要怎樣進行界面布局呢?

那么第一個知識點就是,對於ListView的布局,我們需要創建兩個界面,一個用來顯示表頭與ListView,另一個用來顯示ListView中每個item。我們現在需要顯示的是一個具有三個屬性:學號,姓名,年齡的表格,所以第一個界面布局的設計方法是:整個界面是一個垂直屬性的線性布局,表頭用一個線性布局表示,學號、姓名、年齡三列平均分,剩下的部分用來顯示ListView;第二個界面的設計方法:與表頭一致。代碼如下:

activity_my_open_helper.xml:
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:orientation="vertical"
 7     android:paddingBottom="@dimen/activity_vertical_margin"
 8     android:paddingLeft="@dimen/activity_horizontal_margin"
 9     android:paddingRight="@dimen/activity_horizontal_margin"
10     android:paddingTop="@dimen/activity_vertical_margin"
11     tools:context="com.administrator.sqlite.MyOpenHelper">
12 
13     <LinearLayout
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content">
16 
17         <TextView
18             android:id="@+id/stu_number"
19             android:text="學號"
20             android:textSize="25sp"
21             android:layout_weight="1"
22             android:gravity="center"
23             android:layout_width="wrap_content"
24             android:layout_height="wrap_content" />
25 
26         <TextView
27             android:id="@+id/stu_name"
28             android:text="姓名"
29             android:textSize="25sp"
30             android:layout_weight="1"
31             android:gravity="center"
32             android:layout_width="wrap_content"
33             android:layout_height="wrap_content" />
34 
35         <TextView
36             android:id="@+id/stu_age"
37             android:text="年齡"
38             android:textSize="25sp"
39             android:layout_weight="1"
40             android:gravity="center"
41             android:layout_width="wrap_content"
42             android:layout_height="wrap_content" />
43 
44     </LinearLayout>
45 
46     <TextView
47         android:layout_width="match_parent"
48         android:layout_height="1dp"
49         android:background="@android:color/background_dark"/>
50 
51     <ListView
52         android:id="@+id/student_lv"
53         android:layout_width="match_parent"
54         android:layout_height="match_parent"></ListView>
55 </LinearLayout>
studentlayout.xml:
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical" android:layout_width="match_parent"
 4     android:layout_height="match_parent">
 5 
 6     <LinearLayout
 7         android:layout_width="match_parent"
 8         android:layout_height="wrap_content">
 9         <TextView
10             android:id="@+id/stu_number"
11             android:textSize="20dp"
12             android:gravity="center"
13             android:layout_weight="1"
14             android:text="學號"
15             android:layout_width="0dp"
16             android:layout_height="50dp" />
17         <TextView
18             android:id="@+id/stu_name"
19             android:textSize="20dp"
20             android:gravity="center"
21             android:layout_weight="1"
22             android:text="姓名"
23             android:layout_width="0dp"
24             android:layout_height="50dp" />
25         <TextView
26             android:id="@+id/stu_age"
27             android:textSize="20dp"
28             android:gravity="center"
29             android:layout_weight="1"
30             android:text="年齡"
31             android:layout_width="0dp"
32             android:layout_height="50dp" />
33 
34     </LinearLayout>
35 
36 
37 </LinearLayout>
View Code

  第二個知識點就是通過ListView顯示,我們需要先把數據庫的信息取到一個數組中然后一條條讀取數組中的數據,並把它輸出到ListView的一個個條目中。因為每一條信息都有三個屬性,建立一個類把數據模型化,並在這個類中實現對屬性的初始化、get與set,因為屬性都是私有成員變量,需要利用公共的方法對其進行賦值與讀取。利用游標與一個while訓話可以掃描整個數據庫,並把掃描的每一行添加到數組中,這樣之后,我們就成功把數據庫的信息寫入到數組中,接下來我們要為ListView設置一個適配器,它有四個方法,我們主要對getCount(),getView()方法進行操作。代碼及注釋見下:

 1 package com.administrator.sqlite;
 2 
 3 import android.app.Activity;
 4 import android.database.Cursor;
 5 import android.database.sqlite.SQLiteDatabase;
 6 import android.os.Bundle;
 7 import android.os.PersistableBundle;
 8 import android.view.View;
 9 import android.view.ViewGroup;
10 import android.widget.BaseAdapter;
11 import android.widget.ListView;
12 import android.widget.TextView;
13 
14 import java.util.ArrayList;
15 
16 /**
17  * Created by Administrator on 2016/7/23.
18  */
19 public class student extends Activity {
20     private MyOpenHelper moh;
21     private SQLiteDatabase sd;
22     private ArrayList<student_info> studentlist;
23     private ListView lv;
24 
25     @Override
26     protected void onCreate(Bundle savedInstanceState) {
27         super.onCreate(savedInstanceState);
28         setContentView(R.layout.activity_my_open_helper);
29         //創建或打開數據庫
30         moh=new MyOpenHelper(this,"student.db",null,2);
31         sd= moh.getReadableDatabase();
32         studentlist = new ArrayList<>();
33         //掃描數據庫,將數據庫信息放入studentlist
34         Cursor cursor = sd.rawQuery("select * from student",null);
35         while (cursor.moveToNext()){
36             String number = cursor.getString(cursor.getColumnIndex("number"));
37             String name = cursor.getString(cursor.getColumnIndex("name"));
38             String age = cursor.getString(cursor.getColumnIndex("age"));
39             student_info st = new student_info(number,name,age);    //student_info存一個條目的數據
40             studentlist.add(st);//把數據庫的每一行加入數組中
41         }
42         //獲取ListView,並通過Adapter把studentlist的信息顯示到ListView
43         //為ListView設置一個適配器,getCount()返回數據個數;getView()為每一行設置一個條目
44         lv=(ListView)findViewById(R.id.student_lv);
45         lv.setAdapter(new BaseAdapter() {
46             @Override
47             public int getCount() {
48                 return studentlist.size();
49             }
50 
51             //ListView的每一個條目都是一個view對象
52             @Override
53             public View getView(int position, View convertView, ViewGroup parent) {
54                 View view;
55                 //對ListView的優化,convertView為空時,創建一個新視圖;convertView不為空時,代表它是滾出
56                 //屏幕,放入Recycler中的視圖,若需要用到其他layout,則用inflate(),同一視圖,用fiindViewBy()
57                 if(convertView==null){
58                     view = View.inflate(getBaseContext(),R.layout.studentlayout,null);
59                 }
60                 else{
61                     view = convertView;
62                 }
63 
64                 //從studentlist中取出一行數據,position相當於數組下標,可以實現逐行取數據
65                 student_info st = studentlist.get(position);
66                 TextView number = (TextView)view.findViewById(R.id.stu_number);
67                 TextView name = (TextView)view.findViewById(R.id.stu_name);
68                 TextView age = (TextView)view.findViewById(R.id.stu_age);
69                 number.setText(st.getNumber());
70                 name.setText(st.getName());
71                 age.setText(st.getAge());
72                 return view;
73             }
74 
75             @Override
76             public Object getItem(int position) {
77                 return null;
78             }
79 
80             @Override
81             public long getItemId(int position) {
82                 return 0;
83             }
84         });
85     }
86 }

下面我們就來看一下結果吧~~

是不是感覺很美~~哈哈哈~~

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM