SimpleAdapter,SimpleCursorAdapter,自定义适配器CursorAdapter绑定数据库中的数据到控件并显示
注意:在配置文件中加入权限:
将数据库放到外部存储storage/Download文件下
结果如图:
第一种:SimpleAdapter
逻辑代码文件:
package com.example.day14_sqlitedb;
import java.io.File;
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.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Environment;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class MainActivity extends Activity {
ListView lv;
List
逻辑代码文件:
package com.SimpleCursorAdapter;
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 com.example.day14_sqlitedb.R;
public class SimpleCursoradapter extends Activity {
ListView lv;
SQLiteDatabase db;
String path = Environment.getExternalStorageDirectory().getAbsolutePath()
+ File.separator + "Download" + File.separator + "test.db";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv);
db = SQLiteDatabase.openDatabase(path, null,
SQLiteDatabase.OPEN_READWRITE);
Cursor c = db.rawQuery("select * from person", null);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
getApplicationContext(), R.layout.style, c, new String[] {
"name", "sex", "age", "love" }, new int[] { R.id.name,
R.id.sex, R.id.age, R.id.love },
SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
lv.setAdapter(adapter);
}
}
第三种自定义适配器CursorAdapter:
package com.zidiyiAdapter;
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.support.v4.widget.CursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TextView;
import com.example.day14_sqlitedb.R;
public class MyCursorAdapter extends Activity{
ListView lv;
SQLiteDatabase db;
String path = Environment.getExternalStorageDirectory().getAbsolutePath()
+ File.separator + "Download" + File.separator + "test.db";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView) findViewById(R.id.lv);
db=SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READWRITE);
String sql="select * from person";
Cursor c=db.rawQuery(sql, null);
MyAdapter adapter=new MyAdapter(getApplicationContext(), c);
}
public class MyAdapter extends CursorAdapter
{
public MyAdapter(Context context, Cursor c) {
super(context, c);
// TODO Auto-generated constructor stub
}
// newView 返回item布局 view 给 bindView
@Override
public View newView(Context context, Cursor c, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.style,null);
}
// 参数1:newVIew 里返回的View视图
@Override
public void bindView(View view, Context context, Cursor c) {
TextView name=(TextView) view.findViewById(R.id.name);
TextView sex=(TextView) view.findViewById(R.id.sex);
TextView age=(TextView) view.findViewById(R.id.age);
TextView love=(TextView) view.findViewById(R.id.love);
name.setText(c.getString(c.getColumnIndex("name")));
sex.setText(c.getString(c.getColumnIndex("sex")));
age.setText(c.getInt(c.getColumnIndex("age")));
love.setText(c.getString(c.getColumnIndex("love")));
}
}
}