我想在我的安卓项目中实现一个这样的功能,读取sqlite数据库中的数据并显示到某个页面的listview控件中。
首先,我建立了一个Service类,来实现对数据库的各种操作,然后在这个类中添加对数据库操作的增删改查方法。具体代码如下:
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.example.cardemo1.Username;
import com.example.cardemo1.db.DBHelper;
public class CarService {
private DBHelper dbHelper;
public CarService(Context context){
dbHelper=new DBHelper(context);
}
//添加车辆信息
public boolean register(Car car,String num){
SQLiteDatabase sdb = dbHelper.getReadableDatabase();
String str = "select carnumber from car where carnumber = ?";
Cursor cursor = sdb.rawQuery(str, new String[]{num});
if(cursor.moveToFirst() != true){
String sql ="insert into car(_id,carbrand,carsign,carmodel,carnumber,enginenum,bodylevel,mileage,gasoline,engineperfor,transperfor,carlight) values(?,?,?,?,?,?,?,?,?,?,?,?)";
Object obj[]={car.get_id(),car.getCarbrand(),car.getCarsign(),car.getCarmodel(),car.getCarnumber(),car.getEnginenum(),
car.getBodylevel(),car.getMileage(),car.getGasoline(),car.getEngineperfor(),car.getTransperfor(),car.getCarlight()};
sdb.execSQL(sql,obj);
return true;
}else{
return false;
}
}
//查询车辆信息,返回类型是ArrayList
public ArrayList select(String _id){
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
SQLiteDatabase sdb = dbHelper.getReadableDatabase();
String sql="select * from car where _id=?";
Cursor cursor = sdb.rawQuery(sql, new String[]{_id});
while(cursor.moveToNext()){
HashMap<String, String> map= new HashMap<String, String>();
map.put("carbrand",cursor.getString(1));
map.put("carsign",cursor.getString(2));
map.put("carmodel",cursor.getString(3));
map.put("carnumber",cursor.getString(4));
map.put("enginenum",cursor.getString(5));
map.put("bodylevel",cursor.getString(6));
map.put("mileage",cursor.getString(7));
map.put("gasoline",cursor.getString(8));
map.put("engineperfor", cursor.getString(9));
map.put("transperfor", cursor.getString(10));
map.put("carlight", cursor.getString(11));
list.add(map);
}
return list;
}
//查询车牌号,返回类型是数组
public String[] select_carnum(String _id){
String[] carnum = new String[20];
String carnumber;
int index;
SQLiteDatabase sdb = dbHelper.getReadableDatabase();
String sql="select carnumber from car where _id=?";
Cursor cursor = sdb.rawQuery(sql, new String[]{_id});
int i = 0;
while(cursor.moveToNext()){
index = cursor.getColumnIndex("carnumber");
carnum[i] = cursor.getString(index);
i++;
}
return carnum;
}
//更新车辆信息
public boolean update(Car car,String num){
SQLiteDatabase sdb = dbHelper.getReadableDatabase();
String str ="select carnumber from car where carnumber = ?";
Cursor cursor = sdb.rawQuery(str, new String[]{num});
if(cursor.moveToFirst() == true){
String sql="update car set carbrand=?,carsign=?,carmodel=?,enginenum=?,bodylevel=?,mileage=?,gasoline=?,engineperfor=?,transperfor=?,carlight=? where _id = ?" ;
Object obj[]={car.getCarbrand(),car.getCarsign(),car.getCarmodel(),car.getEnginenum(),
car.getBodylevel(),car.getMileage(),car.getGasoline(),car.getEngineperfor(),car.getTransperfor(),car.getCarlight(),car.get_id()};
sdb.execSQL(sql,obj);
return true;
}else
{
String sql="update car set carbrand=?,carsign=?,carmodel=?,carnumber=?,enginenum=?,bodylevel=?,mileage=?,gasoline=?,engineperfor=?,transperfor=?,carlight=? where _id = ?" ;
Object obj[]={car.getCarbrand(),car.getCarsign(),car.getCarmodel(),car.getCarnumber(),car.getEnginenum(),
car.getBodylevel(),car.getMileage(),car.getGasoline(),car.getEngineperfor(),car.getTransperfor(),car.getCarlight(),car.get_id()};
sdb.execSQL(sql,obj);
return true;
}
}
其中加粗的是我要提取并和listview绑定的数据。然后我在activity类中是这么写的:
Username uname = (Username) getApplicationContext();
String name = uname.getUsername();
CarService service = new CarService(CarSeeActivity.this);
//调用Service中的select方法 来获取数据库中的数据
service.select(name);
//实例化listview空间
ListView lv = (ListView) findViewById(R.id.lv);
//创建adapter
SimpleAdapter adapter = new SimpleAdapter(CarSeeActivity.this, service.select(name), R.layout.item, new String[]{"carbrand","carsign","carmodel","carnumber","enginenum","bodylevel","mileage","gasoline","engineperfor","transperfor","carlight"},
new int[]{R.id.car_carbrand,R.id.car_carsign,R.id.car_carmodel,R.id.car_carnumber,R.id.car_enginenum,R.id.car_bodylevel,R.id.car_mileage,R.id.car_gasoline,R.id.car_engineperfor,R.id.car_transperfor,R.id.car_light});
//将listview和adapter绑定
lv.setAdapter(adapter);
//设置点击事件
lv.setOnItemClickListener(new ItemClickListener());
前两句是获取用户名的,为了在数据库中进行查找,获取用户名就可以。
public class ItemClickListener implements OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
ListView listview = (ListView)parent;
HashMap<String, String> data = (HashMap<String, String>)listview.getItemAtPosition(position);
String carid = data.get("carnumber");
Toast.makeText(getApplicationContext(), "车牌号为"+carid+"的车辆信息", 1).show();
}