标签:
在安卓中,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 } }
标签:
原文地址:http://www.cnblogs.com/linfenghp/p/5415112.html