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:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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>
第二个知识点就是通过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 }
下面我们就来看一下结果吧~~
是不是感觉很美~~哈哈哈~~