13 获取外部数据库 以及数据库游标适配器(SimpleCursorAdapter)

获取外部数据库

  • API
    SQLiteDatabase db = SQLiteDatabase.openDatabase(path, factory, flags);
  • 参数详解:
    • path:数据库路径
    • factory:游标工厂
    • flags:以什么模式打开
      • SQLiteDatabase.OPEN_READWRITE 可读可写
      • SQLiteDatabase.OPEN_READONLY 可读
  • 代码例子:
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "info.db");
        SQLiteDatabase db = SQLiteDatabase.openDatabase(file.toString(), null, SQLiteDatabase.OPEN_READONLY);

SimpleCursorAdapter

  • API:
SimpleCursorAdapter adapter = new SimpleCursorAdapter(context, layout, c, from, to, flags);   
  • 参数详解

    • context:上下文
    • layout:需要填充的山下文
    • c:数据库游标
    • from:从游标中某一列名(数据库某列名,用于待会填充如布局文件中)
    • to:填充到layout的控件中 控件ID
    • flasg:SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER 内容观察者
    • 注意使用此方法主键名必须_id
    • cursor更新时可以用cursor.requery();刷新填充的ListView或者其他控件

    • 代码例子:

package com.fmy.dsas;

import java.io.File;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.widget.SimpleCursorAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class SecondActivity extends Activity {

    private ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.lv);
        set();
    }

    public void set() {
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "info.db");
        SQLiteDatabase db = SQLiteDatabase.openDatabase(file.toString(), null, SQLiteDatabase.OPEN_READONLY);
        Cursor cursor = db.query("person", null, null, null, null, null, null);
        SimpleCursorAdapter adpter = new SimpleCursorAdapter(this, R.layout.item, cursor, new String[] { "name" },
                new int[] { R.id.tv }, SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
        lv.setAdapter(adpter);
    }
}
  • 自定义CursorAdapter
    • 代码例子:
package com.fmy.dsas;

import java.io.File;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class ThirdlyActivity extends Activity {

    private ListView lv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //一个ListView
        lv = (ListView) findViewById(R.id.lv);
        //获取一个数据库的文件地址
        File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "info.db");
        //获得数据库对象
        SQLiteDatabase db = SQLiteDatabase.openDatabase(file.toString(), null, SQLiteDatabase.OPEN_READWRITE);
        //得到游标
        Cursor cursor = db.query("person", null, null, null, null, null, null);

        //创建自定义游标
        MyCurAdpter adpter = new MyCurAdpter(this, cursor, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);

        lv.setAdapter(adpter);
    }

    class MyCurAdpter extends CursorAdapter {

        public MyCurAdpter(Context context, Cursor c, int flags) {
            super(context, c, flags);

        }
        /** * 返回被填充的View */
        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {

            LayoutInflater inflater = LayoutInflater.from(context);

            View view = inflater.inflate(R.layout.item, parent, false);

            return view;
        }

        /** * 给被填充的view初始化 */
        @Override
        public void bindView(View view, Context context, Cursor cursor) {
            TextView tv = (TextView) view.findViewById(R.id.tv);
            tv.setText(cursor.getString(cursor.getColumnIndex("name")));
        }

    }
}

你可能感兴趣的:(13 获取外部数据库 以及数据库游标适配器(SimpleCursorAdapter))