File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "info.db");
SQLiteDatabase db = SQLiteDatabase.openDatabase(file.toString(), null, SQLiteDatabase.OPEN_READONLY);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(context, layout, c, from, to, flags);
参数详解
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);
}
}
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")));
}
}
}