0

コードに問題があります:

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);
    }

}

}

public class Database {

private static DBAdapter da;

public Database(Context context){
    da = new DBAdapter(context);
}

/**
 * Get ArrayList of Places from database
 * 
 * @param Cursor cursor
 * @return ArrayList<Place> arrPlaces 
 */
private ArrayList<Place> getArrayListPlace(Cursor cursor){
    ArrayList<Place> arrPlaces = new ArrayList<Place>();
    try{
        if(cursor.moveToFirst()){
            do{ 
                Place place = getPlace(cursor);
                arrPlaces.add(place);
            }while(cursor.moveToNext());
        }
    }catch(Exception e){
        e.printStackTrace();
    }   
    return arrPlaces;
}

/**
 * Get Place from database
 * 
 * @param Cursor cursor
 * @return Place place
 */
private Place getPlace(Cursor cursor){
    Place place = new Place();
    place.set_id(cursor.getString(0));
    place.set_name(cursor.getString(1));
    place.set_type(cursor.getString(2));
    place.set_address(cursor.getString(3));
    place.set_lat(cursor.getString(4));
    place.set_lon(cursor.getString(5));
    return place;
}

/**
 * Get all list places from database
 * 
 * @return ArrayList<Place>
 */
public ArrayList<Place> getAll(){
    da.open();
    Cursor cursor = da.getAll();
    ArrayList<Place> arrPlaces = getArrayListPlace(cursor); 
    da.close();
    return arrPlaces;
}

/**
 * Get place by id from database
 * 
 * @return Place place
 */
public Place getPlaceById(String id){
    da.open();
    Cursor cursor = da.getPlaceById(id);
    Place place = getPlace(cursor);
    da.close(); 
    return place;
}

/**
 * Get list of places by type from database
 * 
 * @return ArrayList<Place>
 */
public ArrayList<Place> getPlaceByType(String type){
    da.open();
    Cursor cursor = da.getPlaceByType(type);
    ArrayList<Place> arrPlaces = getArrayListPlace(cursor); 
    da.close();
    return arrPlaces;
}

次の 2 つのエラーがあります。

//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);    
    }
  1. タイプ別にデータを取得しようとするとエラーが発生します:

    android.database.sqlite.SQLiteException: 「場所」の近く: 構文エラー: 、コンパイル中: SELECT DISTINCT _id、名前、タイプ、formatted_address、緯度、経度 FROM 場所 WHERE タイプ = マイ プレイス

  2. ListView でデータを表示できません。私はエラーです。

あなたが提供できるどんな助けにもとても感謝しています。

4

1 に答える 1

0

クエリに引用符がありません。数値ではなく文字列を使用する場合は、引用符で囲む必要があります。クエリ コードは次のようになります (追加された ` に注意してください)。

        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設定するために使用しましたlstlocationか? あなたが投稿したコードにそれを表示していない場合...

于 2012-06-17T15:26:15.217 に答える