Android 我无法按关键字(其中KEY_TYPE=TYPE)和设置适配器ListView获取数据
我的代码有问题:Android 我无法按关键字(其中KEY_TYPE=TYPE)和设置适配器ListView获取数据,android,sqlite,android-listview,Android,Sqlite,Android Listview,我的代码有问题: public class DBAdapter { static final String TAG = "DBAdapter"; static final String KEY_ID = "_id"; static final String KEY_NAME = "name"; static final String KEY_ADDRESS = "formatted_address"; static final String KEY_TYPE = "type"; static
public class DBAdapter {
static final String TAG = "DBAdapter";
static final String KEY_ID = "_id";
static final String KEY_NAME = "name";
static final String KEY_ADDRESS = "formatted_address";
static final String KEY_TYPE = "type";
static final String KEY_LAT = "lat";
static final String KEY_LON = "lon";
static final String DATABASE_NAME = "MyDB";
static final String DATABASE_TABLE = "places";
static final int DATABASE_VERSION = 1;
static final String DATABASE_CREATE = "Create table places (_id integer primary key autoincrement, name text, formatted_address text, type text, lat text, lon text);";
final Context context;
DatabaseHelper DBHelper;
SQLiteDatabase db;
public DBAdapter(Context context){
this.context = context;
DBHelper = new DatabaseHelper(context);
}
//open database
public DBAdapter open() throws SQLException{
db = DBHelper.getWritableDatabase();
return this;
}
//close database
public void close(){
DBHelper.close();
}
//get all places in database
public Cursor getAll(){
return db.query(DATABASE_TABLE, new String[] {KEY_ID, KEY_NAME, KEY_TYPE, KEY_ADDRESS,
KEY_LAT, KEY_LON}, null, null, null, null, null);
}
//get a place by id
public Cursor getPlaceById(String id) throws SQLException{
Cursor cursor = db.query(true, DATABASE_TABLE, new String[] {KEY_ID, KEY_NAME, KEY_TYPE, KEY_ADDRESS,
KEY_LAT, KEY_LON}, KEY_ID + "=" + id, null, null, null, null, null);
if(cursor != null){
cursor.moveToFirst();
}
return cursor;
}
//get places by type
public Cursor getPlaceByType(String type) throws SQLException{
Cursor cursor = null;
try{
cursor = db.query(true, DATABASE_TABLE, new String[] {KEY_ID, KEY_NAME, KEY_TYPE, KEY_ADDRESS,
KEY_LAT, KEY_LON}, KEY_TYPE + "=" + type, null, null, null, null, null);
if(cursor != null){
cursor.moveToFirst();
}
return cursor;
}catch(Exception e){
e.printStackTrace();
}
return cursor;
}
private static class DatabaseHelper extends SQLiteOpenHelper{
DatabaseHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
try{
db.execSQL(DATABASE_CREATE);
}catch(SQLException e){
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.w(TAG, "Upgrade database from version " + oldVersion + "to version " + newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS places");
onCreate(db);
}
}
}
有两个错误,包括:
//Get Place to show in ListView
String type = spType.getSelectedItem().toString();
Database db = new Database(this);
try{
arrPlaces = db.getPlaceByType(type);//Can not get data
}catch(Exception e){
e.printStackTrace();
}
/*
* ERROR CODE
* Can not show places in ListView
*/
int length = arrPlaces.size();
if(length > 0){
for(int i=0; i<length; i++){
Place place = arrPlaces.get(i);
lstID.add(place.get_id());
lstName.add(place.get_name() + "\n" + place.get_address());
}
ArrayAdapter<String> adapterName = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lstName);
adapterName.setNotifyOnChange(true);
lstLocation.setAdapter(adapterName);
}
尝试按类型获取数据时出错:
android.database.sqlite.SQLiteException:near Places:语法错误:,编译时:从type=My Places的位置选择DISTINCT _id、名称、类型、格式化地址、lat、lon
我无法在ListView中显示数据。我犯了错误
非常感谢您提供的任何帮助。您的查询中缺少引号。当您使用的是字符串而不是数字时,需要将其括在引号中。您的查询代码应该如下所示,添加了`:
cursor = db.query(true, DATABASE_TABLE, new String[] {KEY_ID, KEY_NAME, KEY_TYPE, KEY_ADDRESS,
KEY_LAT, KEY_LON}, KEY_TYPE + "='" + type + "'", null, null, null, null, null);
至于您的列表,您是否使用findViewById设置位置?如果您这样做了,您就不会在发布的代码中显示它…发布完整的堆栈跟踪,并尝试将代码缩小到重要部分。对不起,我只是想清楚地显示代码。我剪了一个不需要的。非常感谢。这很有用。我使用findViewById在onCreate final ListView lstlocation=ListView findViewByIdR.id.lstlocation;中设置lstlocation;。我不知道我的错在哪里。
cursor = db.query(true, DATABASE_TABLE, new String[] {KEY_ID, KEY_NAME, KEY_TYPE, KEY_ADDRESS,
KEY_LAT, KEY_LON}, KEY_TYPE + "='" + type + "'", null, null, null, null, null);