如何用listview显示sqlite里的数据?
之前在网上找的几个例子,自己改了都不能成功显示,不知道是我改的不对还是什么。新手求指导?
顺便求问,怎么查看程序里新建sqlite表? --------------------编程问答-------------------- 下个SQLite Database Browser之类的,反正都是些很简单的小工具。
你最好把你下载的例子贴出来,别人能给你纠正一下错误。 --------------------编程问答--------------------
--------------------编程问答--------------------
public class Firm_List extends ListActivity{
ListView listView;
firm_infoService firm_infoService;
OnItemClickListener listViewListener;
public void onCreat(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.list_test);
listViewListener = new OnItemClickListener(){
public void onItemClick(AdapterView<?>parent, View view, int position, long id){
ListView listView = (ListView)parent;
//firm_info firm_info = (firm_info)listView.getItemAtPosition(position);
//Toast.makeText(getApplicationContext(), firm_info.toString(), Toast.LENGTH_LONG).show();
//Cursor cursor = (Cursor)listView.getItemAtPosition(position);
//int firm_id = cursor.getInt(cursor.getColumnIndex("_id"));
//Toast.makeText(getApplicationContext(), firm_id+"", Toast.LENGTH_LONG).show();
@SuppressWarnings("unchecked")
Map<String,Object> map = (Map)listView.getItemAtPosition(position);
String name = map.get("name").toString();
String personid = map.get("personid").toString();
Toast.makeText(getApplicationContext(), personid +"-"+ name, Toast.LENGTH_LONG).show();
}
};
listView = (ListView) this.findViewById(R.id.listView);
listView.setOnItemClickListener(listViewListener);
firm_infoService = new firm_infoService(this);
showList();
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
jumpToHome();
}
});
}
public void jumpToHome(){
Intent intent = new Intent();
intent.setClass(Firm_List.this, PeActivity.class);
startActivity(intent);
Firm_List.this.finish();
}
private void showList(){
List<firm_info>firm_infos = firm_infoService.getScrollData(0,50);
List<HashMap<String,Object>>data = new ArrayList<HashMap<String,Object>>();
for(firm_info firm_info:firm_infos){
HashMap<String,Object>item = new HashMap<String,Object>();
item.put("firm_name",firm_info.getName());
item.put("firm_time",firm_info.getTime());
item.put("firm_address",firm_info.getAdd());
item.put("firm_controller",firm_info.getController());
item.put("controller_phone",firm_info.getControllerPhone());
item.put("contact_people",firm_info.getContactPeople());
item.put("contact_phone",firm_info.getContactPhone());
data.add(item);
}
SimpleAdapter adapter = new SimpleAdapter(this,data,R.layout.item, new String[]{"fimr_name","firm_time","firm_address","firm_controller","controller_phone","contact_people","contact_phone"},new int[]{R.id.firm_name,R.id.firm_time,R.id.firm_address,R.id.firm_controller,R.id.controller_phone,R.id.contact_people,R.id.contact_phone});
listView.setAdapter(adapter);
}
//public void showList2(){
// Cursor cursor = firm_infoService.getCursorScrollData(0, 50);
//该适配器要求返回的结果集cursor必须包含_id字段,所以需要对取得结果集进行处理
// SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.item,cursor,new String[]{"fimr_name","firm_time","firm_address","firm_controller","controller_phone","contact_people","contact_phone"}, new int[]{R.id.firm_name,R.id.firm_time,R.id.firm_address,R.id.firm_controller,R.id.controller_phone,R.id.contact_people,R.id.contact_phone} );
// listView.setAdapter(adapter);
// }
// public void showList3(){
// List<firm_info> firm_infos = firm_infoService.getScrollData(0, 50);
///**
//* 第一个参数:上下文context,第二个参数:要显示的数据,第三个参数:绑定的条目界面
//*/
//Firm_infoAdapter adapter = new Firm_infoAdapter(this, firm_infos, R.layout.item);
//listView.setAdapter(adapter);
//}
}
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import ezpe.pe.firm_info;
import ezpe.pe.dbHelper;
public class firm_infoService {
private dbHelper dbOperateHelper;
public firm_infoService (Context context) {
this.dbOperateHelper = new dbHelper(context);
}
/**
* 保存记录
* @param person
*/
public void save(firm_info firm_info){
//得到数据库实例,里面封装了数据库操作方法
SQLiteDatabase sqldb = dbOperateHelper.getWritableDatabase();
//sqldb.execSQL("insert into person(name,phone) values('"+person.getName()+"','"+person.getPhone()+"')");
//利用占位符可以避免注入,但是注意数组参与与占位符对应的字段要一一对应
sqldb.execSQL("insert into firm_info(firm_name,firm_time,firm_address,firm_controller,controller_phone,contact_people,contact_phone) values(?,?,?,?,?,?,?)",new Object[]{firm_info.getName(),firm_info.getTime(),firm_info.getAdd(),firm_info.getController(),firm_info.getControllerPhone(),firm_info.getContactPeople(),firm_info.getContactPhone()});
//关闭数据库
sqldb.close();
}
/**
* 删除记录
* @param id
*/
public void delete(Integer id){
SQLiteDatabase sqldb = dbOperateHelper.getWritableDatabase();
sqldb.execSQL("delete from firm_info where firm_id=?",new Object[]{id});
sqldb.close();
}
/**
* 更新记录
* @param person
*/
public void update(firm_info firm_info){
SQLiteDatabase sqldb = dbOperateHelper.getWritableDatabase();
sqldb.execSQL("update firm_info set firm_name=?,firm_time=?,firm_add=?,firm_controller=?,controller_phone=?,contact_people=?,contact_phone=? where firm_id=?",new Object[]{firm_info.getName(),firm_info.getTime(),firm_info.getAdd(),firm_info.getController(),firm_info.getControllerPhone(),firm_info.getContactPeople(),firm_info.getContactPhone()});
sqldb.close();
}
/**
* 通过ID查询记录
* @param id
* @return
*/
public firm_info find(Integer id){
/**
* getWritableDatabase 与 getReadableDatabase 的区别:
* getReadableDatabase会先返回getWritableDatabase(可写),如果调用getWritableDatabase失败
* 则才会调用getReadableDatabase后续方法,使数据库只读
* 当写入的数据超过数据库大小则调用getWritableDatabase会失败
* 所以只读时则可以使用此方法,其它情况(只要不是超过数据库大小)也可以使用此方法
*/
SQLiteDatabase sqldb = dbOperateHelper.getReadableDatabase();
Cursor cursor = sqldb.rawQuery("select * from firm_info where firm_id=?", new String[]{String.valueOf(id)});
int firm_id;
String firm_name;
String firm_time;
String firm_address;
String firm_controller;
String controller_phone;
String contact_people;
String contact_phone;
firm_info firm_info = null;
if(cursor.moveToFirst()){
firm_id = cursor.getInt(cursor.getColumnIndex("firm_id"));
firm_name = cursor.getString(cursor.getColumnIndex("firm_name"));
firm_time = cursor.getString(cursor.getColumnIndex("firm_time"));
firm_address = cursor.getString(cursor.getColumnIndex("firm_address"));
firm_controller = cursor.getString(cursor.getColumnIndex("firm_controller"));
controller_phone = cursor.getString(cursor.getColumnIndex("controller_phone"));
contact_people = cursor.getString(cursor.getColumnIndex("contact_people"));
contact_phone = cursor.getString(cursor.getColumnIndex("contact_phone"));
firm_info = new firm_info(firm_id,firm_name,firm_time,firm_address,firm_controller,controller_phone,contact_people,contact_phone);
}
cursor.close();
return firm_info;
}
/**
* 返回指定长度记录,limit 3,5,适用于分页
* @param offset 起始
* @param maxResult 长度
* @return
*/
public List<firm_info> getScrollData(int offset,int maxResult){
SQLiteDatabase sqldb = dbOperateHelper.getReadableDatabase();
Cursor cursor = sqldb.rawQuery("select * from firm_info order by firm_id asc limit ?,?", new String[]{String.valueOf(offset),String.valueOf(maxResult)});
int firm_id;
String firm_name;
String firm_time;
String firm_address;
String firm_controller;
String controller_phone;
String contact_people;
String contact_phone;
firm_info firm_info = null;
List<firm_info> firm_infos = new ArrayList<firm_info>();
while(cursor.moveToNext()){
firm_id = cursor.getInt(cursor.getColumnIndex("firm_id"));
firm_name = cursor.getString(cursor.getColumnIndex("firm_name"));
firm_time = cursor.getString(cursor.getColumnIndex("firm_time"));
firm_address = cursor.getString(cursor.getColumnIndex("firm_address"));
firm_controller = cursor.getString(cursor.getColumnIndex("firm_controller"));
controller_phone = cursor.getString(cursor.getColumnIndex("controller_phone"));
contact_people = cursor.getString(cursor.getColumnIndex("contact_people"));
contact_phone = cursor.getString(cursor.getColumnIndex("contact_phone"));
firm_info = new firm_info(firm_id,firm_name,firm_time,firm_address,firm_controller,controller_phone,contact_people,contact_phone);
firm_infos.add(firm_info);
}
cursor.close();
return firm_infos;
}
/**
* 返回cursor
* @param offset 起始
* @param maxResult 长度
* @return
*/
public Cursor getCursorScrollData(int offset,int maxResult){
SQLiteDatabase sqldb = dbOperateHelper.getReadableDatabase();
Cursor cursor = sqldb.rawQuery("select firm_id as _firm_id,firm_name,firm_time,firm_address,firm_controller,controller_phone,contact_people,contact_phone from firm_info order by firm_id asc limit ?,?", new String[]{String.valueOf(offset),String.valueOf(maxResult)});
return cursor;
}
/**
* 返回总记录数
* @return
*/
public long getCount(){
SQLiteDatabase sqldb = dbOperateHelper.getReadableDatabase();
Cursor cursor = sqldb.rawQuery("select count(*) from firm_info", null);
//该查询语句值返回一条语句
cursor.moveToFirst();
long result = cursor.getLong(0);
cursor.close();
return result;
}
/*
public void payment(){
SQLiteDatabase sqldb = dbOperateHelper.getWritableDatabase();
sqldb.beginTransaction();//开启事务
try{
sqldb.execSQL("update person set amount = amount -10 where personid=1");
sqldb.execSQL("update person set amount = amount + 10 where personid=2");
sqldb.setTransactionSuccessful();//设置事务标志位true
} finally {
//结束事务:有两种情况:commit\rollback,事务提交或者回滚是由事务的标识决定的
//事务为ture则提交,事务为flase则回滚,默认为false
sqldb.endTransaction();
}
}*/
}
public class firm_info {
private Integer firm_id;
private String firm_name;
private String firm_time;
private String firm_address;
private String firm_controller;
private String controller_phone;
private String contact_people;
private String contact_phone;
public Integer getId(){
return firm_id;
}
public void setId(Integer id){
this.firm_id=id;
}
public String getName(){
return firm_name;
}
public void setName(String name){
this.firm_name=name;
}
public String getTime(){
return firm_time;
}
public void setTime(String time){
this.firm_time=time;
}
public String getAdd(){
return firm_address;
}
public void setAdd(String add){
this.firm_address=add;
}
public String getController(){
return firm_controller;
}
public void setController(String controller){
this.firm_controller=controller;
}
public String getControllerPhone(){
return controller_phone;
}
public void setControllerPhone(String controllerphone){
this.firm_name=controllerphone;
}
public String getContactPeople(){
return contact_people;
}
public void setContactPeople(String contactpeople){
this.contact_people=contactpeople;
}
public String getContactPhone(){
return contact_phone;
}
public void setContactPhone(String contactphone){
this.contact_phone=contactphone;
}
public firm_info(int id, String name, String time, String add, String controller, String controllerphone, String contact, String contactphone)
{
this.firm_id=id;
this.firm_name=name;
this.firm_time=time;
this.firm_address=add;
this.firm_controller=controller;
this.controller_phone=controllerphone;
this.contact_people=contact;
this.contact_phone=contactphone;
}
public firm_info(String name, String time, String add, String controller, String controllerphone, String contact, String contactphone)
{
this.firm_name=name;
this.firm_time=time;
this.firm_address=add;
this.firm_controller=controller;
this.controller_phone=controllerphone;
this.contact_people=contact;
this.contact_phone=contactphone;
}
public firm_info(){
super();
}
public String toString(){
return "firm_info [firm_id="+firm_id+",firm_name="+firm_name+",firm_time="+firm_time+"firm_address="+firm_address+"firm_controller="+firm_controller+"controller_phone="+controller_phone+"contact_people="+contact_people+"contact_phone="+contact_phone+"]";
}
}
--------------------编程问答--------------------
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteDatabase;
import android.database.Cursor;
import android.database.sqlite.SQLiteOpenHelper;
public class dbHelper extends SQLiteOpenHelper{
private final static String DATABASE_NAME="pe_db";
private final static int DATABASE_VERSION=1;
private final static String TABLE_NAME="firm_info";
public final static String FIRM_ID="firm_id";
public final static String FIRM_NAME="firm_name";
public final static String FIRM_TIME="firm_time";
public final static String FIRM_ADDRESS="firm_address";
public final static String FIRM_CONTROLLER="firm_controller";
public final static String CONTROLLER_PHONE="controller_phone";
public final static String CONTACT_PEOPLE="contact_people";
public final static String CONTACT_PHONE="contact_phone";
public dbHelper (Context context)
{
super(context, DATABASE_NAME,null,DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db){
String sql="Create table "+TABLE_NAME+"("+FIRM_ID+" char primary key autoincrement,"
+FIRM_NAME+"char,"+FIRM_TIME+" char,"+FIRM_ADDRESS+" char, "+FIRM_CONTROLLER+" char,"+CONTROLLER_PHONE+" char,"+CONTACT_PEOPLE+" char,"+CONTACT_PHONE+" char, );";
db.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
String sql=" DROP TABLE IF EXISTS "+TABLE_NAME;
db.execSQL(sql);
onCreate(db);
}
public Cursor select()
{
SQLiteDatabase db=this.getReadableDatabase();
Cursor cursor=db.query(TABLE_NAME, null, null, null, null, null, " firm_name");
return cursor;
}
public long insert(String Title)
{
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(FIRM_TIME, Title);
cv.put(CONTACT_PEOPLE, Title);
cv.put(FIRM_ADDRESS, Title);
cv.put(FIRM_CONTROLLER, Title);
cv.put(CONTROLLER_PHONE, Title);
cv.put(CONTACT_PEOPLE, Title);
cv.put(CONTACT_PHONE, Title);
long row=db.insert(TABLE_NAME, null, cv);
return row;
}
public void delete(int id)
{
SQLiteDatabase db=this.getWritableDatabase();
String where=FIRM_NAME+"=?";
String[] whereValue={Integer.toString(id)};
db.delete(TABLE_NAME, where, whereValue);
}
public void update(int id,String Title)
{
SQLiteDatabase db=this.getWritableDatabase();
String where=FIRM_NAME+"=?";
String[] whereValue={Integer.toString(id)};
ContentValues cv=new ContentValues();
cv.put(FIRM_TIME, Title);
cv.put(CONTACT_PEOPLE, Title);
cv.put(FIRM_ADDRESS, Title);
cv.put(FIRM_CONTROLLER, Title);
cv.put(CONTROLLER_PHONE, Title);
cv.put(CONTACT_PEOPLE, Title);
cv.put(CONTACT_PHONE, Title);
db.update(TABLE_NAME, cv, where, whereValue);
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/firm_name"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="@string/firm_name"
android:gravity="center"/>
<TextView
android:id="@+id/firm_time"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="@string/firm_time"
android:gravity="center"/>
<TextView
android:id="@+id/firm_address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/firm_address"
android:gravity="center"/>
<TextView
android:id="@+id/firm_controller"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="@string/firm_controller"
android:gravity="center"/>
<TextView
android:id="@+id/controller_phone"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="@string/controller_phone"
android:gravity="center"/>
<TextView
android:id="@+id/contact_people"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="@string/contact_people"
android:gravity="center"/>
<TextView
android:id="@+id/contact_phone"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="@string/contact_phone"
android:gravity="center"/>
</LinearLayout>
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ListView>
</LinearLayout>
--------------------编程问答--------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<TextView
android:id="@+id/firm_name"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="@string/firm_name"
android:gravity="center"/>
<TextView
android:id="@+id/firm_time"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:text="@string/firm_time"
android:gravity="center"/>
<TextView
android:id="@+id/firm_address"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/firm_address"
android:gravity="center"/>
<TextView
android:id="@+id/firm_controller"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/firm_controller"
android:gravity="center"/>
<TextView
android:id="@+id/controller_phone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/controller_phone"
android:gravity="center"/>
<TextView
android:id="@+id/contact_people"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/contact_people"
android:gravity="center"/>
<TextView
android:id="@+id/contact_phone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/contact_phone"
android:gravity="center"/>
</LinearLayout>
private void showList(){
List<firm_info>firm_infos = firm_infoService.getScrollData(0,50);
List<HashMap<String,Object>>data = new ArrayList<HashMap<String,Object>>();
for(firm_info firm_info:firm_infos){
HashMap<String,Object>item = new HashMap<String,Object>();
item.put("firm_name",firm_info.getName());
item.put("firm_time",firm_info.getTime());
item.put("firm_address",firm_info.getAdd());
item.put("firm_controller",firm_info.getController());
item.put("controller_phone",firm_info.getControllerPhone());
item.put("contact_people",firm_info.getContactPeople());
item.put("contact_phone",firm_info.getContactPhone());
data.add(item);
} //--看data有没有值,下面一行“fimr_name”拼写错误。
SimpleAdapter adapter = new SimpleAdapter(this,data,R.layout.item, new String[]{"fimr_name","firm_time","firm_address","firm_controller","controller_phone","contact_people","contact_phone"},new int[]{R.id.firm_name,R.id.firm_time,R.id.firm_address,R.id.firm_controller,R.id.controller_phone,R.id.contact_people,R.id.contact_phone});
listView.setAdapter(adapter);
}
--------------------编程问答-------------------- --------------------编程问答--------------------
我后来改了个实现的方法。
但是发现sqlite创建的时候没问题,插入的时候没有反应,也不报错。
在DDMS里导出那个db文件查看里面有创建的表,但是没有插入的数据。
创建table
public void onCreate(SQLiteDatabase db){
String sql="Create table "+TABLE_NAME+"("+BaseColumns._ID+" integer primary key autoincrement,"
+FIRM_NAME+" char(50),"+FIRM_TIME+" char(50),"+FIRM_ADDRESS+" char(100), "+FIRM_CONTROLLER+" char(50),"+CONTROLLER_PHONE+" char(50),"+CONTACT_PEOPLE+" char(50),"+CONTACT_PHONE+" char(50));";
db.execSQL(sql);
}
insert
public static void InsertFirmInfo()
{
//String name="FIRM_INFO";
//String f_id="firm_id";
try
{
//String sql="insert into firm_info values('"+"null',"+"'"+PeActivity.firm_name+"','"+PeActivity.firm_time+"'," +
// "'"+PeActivity.firm_address+"','"+PeActivity.firm_controller+"','"+PeActivity.controller_phone+"'," +
// "'"+PeActivity.contact_people+"','"+PeActivity.contact_phone+"');";
dbUtil.getWritableDatabase();
ContentValues v = new ContentValues();
v.put("firm_name", PeActivity.firm_name);
v.put("firm_time", PeActivity.firm_time);
v.put("firm_address", PeActivity.firm_address);
v.put("firm_controller", PeActivity.firm_controller);
v.put("controller_phone", PeActivity.controller_phone);
v.put("contact_people", PeActivity.contact_people);
v.put("contact_phone", PeActivity.contact_phone);
db.insertOrThrow("firm_info",null,v);
// String sql= "insert into firm_info(firm_name,firm_time,firm_address,firm_controller,controller_phone,contact_people,contact_phone) values(?,?,?,?,?,?,?)";
// db.execSQL(sql, new Object[]{mFirm_info.getName(),FIRM_INFO.getTime(),FIRM_INFO.getAdd(),FIRM_INFO.getController(),FIRM_INFO.getControllerPhone(),FIRM_INFO.getContactPeople(),FIRM_INFO.getContactPhone()});
}
catch(Exception e)
{
e.printStackTrace();
}
}
补充:移动开发 , Android