接着http://3599672.blog.51cto.com/3589672/887524SQlite数据库的使用(一)。
首先打开MyEclipse和android虚拟机,如果使用Eclipse的话,应该也都差不多。
首先是整个项目的机构
- package com.fcm.app;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.EditText;
- public class MainActivity extends Activity {
- Button mButton1,mButton2,mButton3;
- EditText mEditText;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mEditText = (EditText) findViewById(R.id.editText1);
- mButton1 = (Button) findViewById(R.id.button1);
- mButton2 = (Button) findViewById(R.id.button2);
- mButton3 = (Button) findViewById(R.id.button3);
- mButton1.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View view) {
- Intent intent = new Intent();
- intent.setClass(MainActivity.this,Students1Activity.class);
- startActivity(intent);
- }
- });
- mButton2.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View view) {
- Intent intent = new Intent();
- intent.setClass(MainActivity.this,Students2Activity.class);
- intent.putExtra("Sname", mEditText.getText().toString());
- startActivity(intent);
- }
- });
- mButton3.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View view) {
- Intent intent = new Intent();
- intent.setClass(MainActivity.this,Students3Activity.class);
- startActivity(intent);
- }
- });
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <Button android:text="查询所有数据-不查询图片" android:id="@+id/button1"
- android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
- <EditText android:id="@+id/editText1" android:text="输入姓名如张三或者李四或者小梅"
- android:layout_width="match_parent" android:layout_height="wrap_content"
- android:selectAllOnFocus="true"></EditText>
- <Button android:text="根据姓名查询一组数据-包括图片的查询" android:id="@+id/button2"
- android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
- <Button android:text="查询所有数据-包括图片的查询" android:id="@+id/button3"
- android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
- </LinearLayout>
- package com.fcm.app;
- import android.content.Context;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteOpenHelper;
- public class DataBaseOpenHelper extends SQLiteOpenHelper{
- // 类没有实例化,是不能用作父类构造器的参数,必须声明为静态
- private static String dbname = "School.db";
- private static int version = 1;
- public DataBaseOpenHelper(Context context) {
- // 第一个参数是应用的上下文
- // 第二个参数是应用的数据库名字
- // 第三个参数CursorFactory指定在执行查询时获得一个游标实例的工厂类,设置为null,代表使用系统默认的工厂类
- // 第四个参数是数据库版本,必须是大于0的int
- super(context, dbname, null, version);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- db.execSQL("CREATE TABLE IF NOT EXISTS students(id integer primary key autoincrement,Sno varchar(10), Sname varchar(10),Ssex char(2),Sphoto BLOB)");
- }
- // onUpgrade()方法在数据库版本每次发生变化时都会把用户手机上的数据库表删除,然后再重新创建。
- // 一般在实际项目中是不能这样做的,正确的做法是在更新数据库表结构时,还要考虑用户存放于数据库中的数据不会丢失,从版本几更新到版本几。
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- db.execSQL("DROP TABLE IF EXISTS students");
- onCreate(db);
- }
- }
- package com.fcm.app;
- import android.content.Context;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- public class DataBaseService {
- private DataBaseOpenHelper dbOpenHelper;
- public DataBaseService(Context context) {
- dbOpenHelper = new DataBaseOpenHelper(context);
- }
- //查询表students中的所有数据,返回一个cursor
- public Cursor getAllData(){
- SQLiteDatabase database = dbOpenHelper.getReadableDatabase();
- Cursor cursor = database.rawQuery("select id as _id ,Sno,Sname,Ssex,Sage,Sphoto from students",null);
- return cursor;
- }
- //根据姓名查询一组数据
- public Cursor getDataByName(String Sname){
- SQLiteDatabase database = dbOpenHelper.getReadableDatabase();
- Cursor cursor = database.rawQuery("select id as _id ,Sno,Sname,Ssex,Sage,Sphoto from students where Sname=?",new String[] {Sname.toString()});
- return cursor;
- }
- }
Students1Activity.java
- package com.fcm.app;
- import android.app.Activity;
- import android.database.Cursor;
- import android.os.Bundle;
- import android.widget.ListView;
- import android.widget.SimpleCursorAdapter;
- public class Students1Activity extends Activity{
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.students);
- ListView mListView = (ListView) findViewById(R.id.studentslistView);
- DataBaseService mDataBaseService = new DataBaseService(this);
- Cursor cursor = mDataBaseService.getAllData();
- SimpleCursorAdapter adapter = new SimpleCursorAdapter(Students1Activity.this,
- R.layout.students_listview, cursor,
- new String[]{"Sno","Sname","Ssex","Sage",},
- new int[]{R.id.Sno,R.id.Sname,R.id.Ssex,R.id.Sage,});
- mListView.setAdapter(adapter);
- }
- }
students.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent" android:layout_height="match_parent">
- <ListView android:layout_width="match_parent" android:id="@+id/studentslistView"
- android:layout_height="wrap_content"></ListView>
- </LinearLayout>
students_listview.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent" android:layout_height="match_parent"
- android:orientation="vertical">
- <TextView android:text="TextView" android:id="@+id/Sno"
- android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
- <TextView android:text="TextView" android:id="@+id/Sname"
- android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
- <TextView android:text="TextView" android:id="@+id/Ssex"
- android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
- <TextView android:text="TextView" android:id="@+id/Sage"
- android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
- <ImageView android:src="@drawable/icon" android:layout_width="wrap_content"
- android:id="@+id/Sphoto" android:layout_height="wrap_content"></ImageView>
- </LinearLayout>
先运行看看吧。点击主界面中的第一个“查询所有数据-不查询图片”按钮,结果报错了,那是因为数据库中还没有数据。现在就需要把数据库导入。如下图所示:School.db数据库已经创建,只是里面没有数据。
点击右上角的“push a file onto the device”按钮,将保存的School.db导入
接着点击主界面的第一个按钮效果如下:
这里没有把图片给取出
如果将代码:
- SimpleCursorAdapter adapter = new SimpleCursorAdapter(Students1Activity.this,
- R.layout.students_listview, cursor,
- new String[]{"Sno","Sname","Ssex","Sage",},
- new int[]{R.id.Sno,R.id.Sname,R.id.Ssex,R.id.Sage,});
改成:
- SimpleCursorAdapter adapter = new SimpleCursorAdapter(Students1Activity.this,
- R.layout.students_listview, cursor,
- new String[]{"Sno","Sname","Ssex","Sage","Sphoto"},
- new int[]{R.id.Sno,R.id.Sname,R.id.Ssex,R.id.Sage,R.id.Sphoto});
那么就会报错,因为Sphoto是BLOB型,而不是String型
- package com.fcm.app;
- import android.app.Activity;
- import android.database.Cursor;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.os.Bundle;
- import android.widget.ImageView;
- import android.widget.TextView;
- public class Students2Activity extends Activity{
- TextView mTextView1,mTextView2,mTextView3,mTextView4;
- ImageView mImageView;
- String Sname;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.students_listview);
- mTextView1 = (TextView) findViewById(R.id.Sno);
- mTextView2 = (TextView) findViewById(R.id.Sname);
- mTextView3 = (TextView) findViewById(R.id.Ssex);
- mTextView4 = (TextView) findViewById(R.id.Sage);
- mImageView = (ImageView) findViewById(R.id.Sphoto);
- DataBaseService mDataBaseService = new DataBaseService(this);
- String Sname= this.getIntent().getExtras().getString("Sname");
- Cursor cursor = mDataBaseService.getDataByName(Sname);
- if (cursor.moveToNext()) {
- String Sno = cursor.getString(cursor.getColumnIndex("Sno"));
- String Ssex=cursor.getString(cursor.getColumnIndex("Ssex"));
- String Sage=cursor.getString(cursor.getColumnIndex("Sage"));
- byte[] imagequery=null;
- imagequery=cursor.getBlob(cursor.getColumnIndex("Sphoto"));//将Blob数据转化为字节数组
- Bitmap imagebitmap=BitmapFactory.decodeByteArray(imagequery, 0, imagequery.length); //将字节数组转化为位图
- mTextView1.setText("学号:"+Sno);
- mTextView2.setText(Sname);
- mTextView3.setText(Ssex);
- mTextView4.setText(Sage);
- mImageView.setImageBitmap(imagebitmap);
- }
- cursor.close();
- }
- }
直接看效果吧:
在文本框中输入“小梅”,点击第二个“根据姓名查询。。。”按钮。
结果如下:
这样图片也就读取出来了。
ListViewAdapter.java
- package com.fcm.app;
- import java.util.List;
- import java.util.Map;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.ImageView;
- import android.widget.TextView;
- public class ListViewAdapter extends BaseAdapter{
- private List<Map<String,Object>> listItems;
- private LayoutInflater listContainer;
- public final class ListItemView{
- public TextView mTextView1;
- public TextView mTextView2;
- public TextView mTextView3;
- public TextView mTextView4;
- public ImageView mImageView;
- }
- public ListViewAdapter(Context context, List<Map<String, Object>> listItems) {
- super();
- listContainer = LayoutInflater.from(context);
- this.listItems = listItems;
- }
- @Override
- public int getCount() {
- return listItems.size();
- }
- @Override
- public Object getItem(int arg0) {
- return null;
- }
- @Override
- public long getItemId(int arg0) {
- return 0;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- ListItemView listItemView = null;
- if(convertView ==null){
- listItemView = new ListItemView();
- convertView = listContainer.inflate(R.layout.students_listview, null);
- //获得控件对象
- listItemView.mTextView1 =(TextView) convertView.findViewById(R.id.Sno);
- listItemView.mTextView2 =(TextView) convertView.findViewById(R.id.Sname);
- listItemView.mTextView3 =(TextView) convertView.findViewById(R.id.Ssex);
- listItemView.mTextView4 =(TextView) convertView.findViewById(R.id.Sage);
- listItemView.mImageView =(ImageView) convertView.findViewById(R.id.Sphoto);
- convertView.setTag(listItemView); //设置空间集到convertView
- }else{
- listItemView = (ListItemView) convertView.getTag();
- }
- //设置文字图片
- listItemView.mTextView1.setText((String)listItems.get(position).get("Sno"));
- listItemView.mTextView2.setText((String)listItems.get(position).get("Sname"));
- listItemView.mTextView3.setText((String)listItems.get(position).get("Ssex"));
- listItemView.mTextView4.setText((String)listItems.get(position).get("Sage"));
- listItemView.mImageView.setImageBitmap((Bitmap)listItems.get(position).get("Sphoto"));
- return convertView;
- }
- }
Students3Activity.java
- package com.fcm.app;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import android.app.Activity;
- import android.database.Cursor;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.os.Bundle;
- import android.widget.ImageView;
- import android.widget.ListView;
- import android.widget.TextView;
- public class Students3Activity extends Activity{
- private List<Map<String,Object>> listItems;
- ListView mListView;
- ImageView mImageView;
- TextView mTextView1,mTextView2,mTextView3,mTextView4;
- private ListViewAdapter listViewAdapter;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.students);
- listItems = getList();
- mListView=(ListView) findViewById(R.id.studentslistView);
- listViewAdapter = new ListViewAdapter(this,listItems);
- mListView.setAdapter(listViewAdapter);
- }
- private List<Map<String, Object>> getList() {
- DataBaseService mDataBaseService = new DataBaseService(this);
- Cursor cursor = mDataBaseService.getAllData();
- List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
- while(cursor.moveToNext()){
- Map<String, Object> map = new HashMap<String, Object>();
- map.put("Sno", cursor.getString(cursor.getColumnIndex("Sno")));
- map.put("Sname", cursor.getString(cursor.getColumnIndex("Sname")));
- map.put("Ssex", cursor.getString(cursor.getColumnIndex("Ssex")));
- map.put("Sage", cursor.getString(cursor.getColumnIndex("Sage")));
- byte[] imagequery=cursor.getBlob(cursor.getColumnIndex("Sphoto"));
- Bitmap imagebitmap=BitmapFactory.decodeByteArray(imagequery, 0, imagequery.length);
- map.put("Sphoto", imagebitmap);
- list.add(map);
- }
- return list;
- }
- }
点击第三个“查询所有数据-包括图片的查询”按钮 ,结果如下图:
OK!这样就完事了!喝喝。。。。