標籤:
在安卓中,ListView和Sqlite都是十分常用的。這次我們來結合這個兩個知識點寫一個Demo。
功能:吧SQLite中的資料用ListView顯示出來。
先看吧
首先是資料庫
然後是運行
接下來是講解,運行程式後,先點擊建立資料庫的按鈕,會建立出50條資料,然後點擊查詢,會將這50條資料查詢並顯示在ListView中。
直接看代碼吧!
布局檔案activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.listviewdemo.MainActivity" > <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="Create_Data" android:text="建立資料" /> <Button android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="Query_Data" android:text="查詢資料" /> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="match_parent" /></LinearLayout>
java檔案
Girl.java
作為javabean,儲存資料類用的
package com.example.listviewdemo;public class Girl { private String name; private int age; private String phone; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } @Override public String toString() { return name+","+age+","+phone; } public Girl(String name, int age, String phone) { super(); this.name = name; this.age = age; this.phone = phone; } }
MainActivity.java
package com.example.listviewdemo;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.content.ContentValues;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ListView;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity { private MyOpenSqlHelp myDatebaseHelper; List<Girl> girList ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myDatebaseHelper = new MyOpenSqlHelp(this, "People.db", null, 1); SQLiteDatabase db = myDatebaseHelper.getWritableDatabase(); girList = new ArrayList<Girl>(); } public void Create_Data(View v) { SQLiteDatabase db = myDatebaseHelper.getWritableDatabase(); for(int i=0;i<=50;i++){ ContentValues values = new ContentValues(); values.put("Name", "朱主愛"+i); values.put("Age", "19"); values.put("Phone", "135708"+i); db.insert("Girl", null, values); } Toast.makeText(this, "資料建立成功", 0).show(); } public void Query_Data(View v) { SQLiteDatabase db = myDatebaseHelper.getWritableDatabase(); Cursor cursor = db.query("Girl", null, null, null, null, null, null); while (cursor.moveToNext()) { String name = cursor.getString(cursor.getColumnIndex("Name")); int age = cursor.getInt(cursor.getColumnIndex("Age")); String phone = cursor.getString(cursor.getColumnIndex("Phone")); Girl girl =new Girl(name,age,phone); girList.add(girl); } //拿到listveiw對象 ListView lv = (ListView) findViewById(R.id.lv); //設定適配器 lv.setAdapter(new MyAapter()); } //適配器類 class MyAapter extends BaseAdapter { //擷取集合中有多少條元素,由系統調用 @Override public int getCount() { // TODO Auto-generated method stub return girList.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } //由系統調用,返回一個view對象作為listview的條目 /* * position:本次getView方法調用所返回的view對象在listView中處於第幾個條目,position的值就為多少 * */ @Override public View getView(int position, View convertView, ViewGroup parent) { TextView tv = new TextView(MainActivity.this); tv.setTextSize(18); //擷取集合中的元素 Girl girl = girList.get(position); tv.setText(girl.toString()); return tv; } }}
MyOpenSqlHelp.java
資料庫協助器,要用sqlite資料庫必須建立的類
package com.example.listviewdemo;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteDatabase.CursorFactory;import android.database.sqlite.SQLiteOpenHelper;import android.widget.Toast;public class MyOpenSqlHelp extends SQLiteOpenHelper{ public static final String CREATE_GIRL = "create table Girl(_id integer primary key autoincrement,Name char(20),Age integer,Phone char(20))" ; private Context mContext; public MyOpenSqlHelp(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); // TODO Auto-generated constructor stub mContext = context ; } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub db.execSQL(CREATE_GIRL); /*Toast.makeText(mContext,"Create Successded",0).show();*/ } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub }}
ListView顯示Sqlite的資料