我引用了這個link並能夠使數據庫查詢過濾。 目前我使用下面的代碼用於過濾7個BusTypes從數據庫和下面的代碼工作正常,但我必須現在boardingpoint和droppingpoints也適用濾波器。我怎樣才能做到這一點 ?使用查詢從sqlite中的多列執行多個值?
我的數據庫中包含多個boarding_point的列表作爲一個字符串,因爲每個城市都有多個登機,等。,[城市pointone,城市pointtwo,城市point_three]如在bording_point字符串。現在我想用一個城市點等。查詢,如果我搜索與城市pointone它應該給我有該點的所有公交車..
公共final類DatabaseConstants {
// To prevent someone from accidentally instantiating the contract class,
// make the constructor private.
private DatabaseConstants(){
}
public static class FeedEntry implements BaseColumns {
public static final String TABLE_NAME = "temp_table";
public static final String ID = "id";
public static final String FROM_CITY_NAME = "fromcityname";
public static final String SRC_TIME = "srctime";
public static final String TO_CITY_NAME = "tocityname";
public static final String DES_TIME = "destime";
public static final String DISTANCE = "distance";
public static final String BUS_ID = "busid";
public static final String OPERATOR_NAME = "operatorname";
public static final String BUS_TYPE = "bustype";
public static final String BUS_SEAT = "busseat";
public static final String DURATION = "total_duration";
public static final String BOARDING_POINT = "boarding_points";
public static final String DROPPING_POINT = "dropping_points";
public static final String CREATE_TABLE = ("CREATE TABLE " + TABLE_NAME + " ("
+ ID + " TEXT PRIMARY KEY , "
+ FROM_CITY_NAME + " TEXT, "
+ SRC_TIME + " TEXT, "
+ TO_CITY_NAME + " TEXT, "
+ DES_TIME + " TEXT, "
+ DISTANCE + " TEXT, "
+ BUS_ID + " TEXT, "
+ OPERATOR_NAME + " TEXT, "
+ BUS_TYPE + " TEXT, "
+ BUS_SEAT + " TEXT, "
+ DURATION + " TEXT, "
+ BOARDING_POINT + " TEXT, "
+ DROPPING_POINT + " TEXT)");
public static final String DROP_TABLE = ("DROP TABLE IF EXISTS " + TABLE_NAME);
}
}
// Model類
public class BusDataModel {
private String id;
private String fromcityname;
private String src_time;
private String tocityname;
private String des_time;
private String distance;
private String busid;
private String operatorname;
private String bustype;
private String busseat;
private String busduration;
private String boarding_point;
private String droppint_point;
public BusDataModel() {
}
public BusDataModel(String id, String fromcityname, String src_time, String tocityname, String des_time,
String distance, String busid, String operatorname, String bustype, String busseat,String busduration,
String boarding_point,String droppint_point) {
this.id = id;
this.src_time = src_time;
this.des_time = des_time;
this.fromcityname = fromcityname;
this.tocityname = tocityname;
this.distance = distance;
this.busid = busid;
this.operatorname = operatorname;
this.bustype = bustype;
this.busseat = busseat;
this.busduration=busduration;
this.boarding_point=boarding_point;
this.droppint_point=droppint_point;
}
public String getBusduration() {
return busduration;
}
public void setBusduration(String busduration) {
this.busduration = busduration;
}
public String getid() {
return id;
}
public String getSrc_time() {
return src_time;
}
public String getDes_time() {
return des_time;
}
public String getFromcityname() {
return fromcityname;
}
public String getTocityname() {
return tocityname;
}
public String getDistance() {
return distance;
}
public String getBusid() {
return busid;
}
public String getOperatorname() {
return operatorname;
}
public String getBustype() {
return bustype;
}
public String getBusseat() {
return busseat;
}
public void setid(String id) {
this.id = id;
}
public void setSrc_time(String src_time) {
this.src_time = src_time;
}
public void setDes_time(String des_time) {
this.des_time = des_time;
}
public void setFromcityname(String fromcityname) {
this.fromcityname = fromcityname;
}
public void setTocityname(String tocityname) {
this.tocityname = tocityname;
}
public void setDistance(String distance) {
this.distance = distance;
}
public void setBusid(String busid) {
this.busid = busid;
}
public void setOperatorname(String operatorname) {
this.operatorname = operatorname;
}
public void setBustype(String bustype) {
this.bustype = bustype;
}
public void setBusseat(String busseat) {
this.busseat = busseat;
}
public String getBoarding_point() {
return boarding_point;
}
public void setBoarding_point(String boarding_point) {
this.boarding_point = boarding_point;
}
public String getDroppint_point() {
return droppint_point;
}
public void setDroppint_point(String droppint_point) {
this.droppint_point = droppint_point;
}
}
//源碼類
public class FilterData extends SQLiteOpenHelper {
private Context context;
List<BusDataModel> filterdData = new LinkedList<>();
public static final String DATABASE_NAME = "temp_user";
public FilterData(Context context) {
super(context, DATABASE_NAME, null, 3);
this.context = context;
onCreate(this.getWritableDatabase());
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME + ";");
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
/**
* Method to save all Data into Database
*
* @param busDataModel to get all Data Generically
*/
public boolean addBusDetails(BusDataModel busDataModel) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(ID, busDataModel.getid());
values.put(FROM_CITY_NAME, busDataModel.getFromcityname());
values.put(SRC_TIME, busDataModel.getSrc_time());
values.put(TO_CITY_NAME, busDataModel.getTocityname());
values.put(DES_TIME, busDataModel.getDes_time());
values.put(DISTANCE, busDataModel.getDistance());
values.put(BUS_ID, busDataModel.getBusid());
values.put(OPERATOR_NAME, busDataModel.getOperatorname());
values.put(BUS_TYPE, busDataModel.getBustype());
values.put(BUS_SEAT, busDataModel.getBusseat());
values.put(DURATION, busDataModel.getBusduration());
values.put(BOARDING_POINT, busDataModel.getBoarding_point());
values.put(DROPPING_POINT, busDataModel.getDroppint_point());
long isInserted = db.insert(TABLE_NAME, null, values);
db.close();
if (isInserted == -1)
return false;
else
return true;
}
public List<BusDataModel> setFilterByBusType(List<String> bus_types, List<String> boarding_types, List<String> dropping_types) {
List<BusDataModel> busList = new LinkedList<>();
SQLiteDatabase db = this.getReadableDatabase();
String[] data = {ID, FROM_CITY_NAME, SRC_TIME, TO_CITY_NAME, DES_TIME,
DISTANCE, BUS_ID, OPERATOR_NAME, BUS_TYPE, BUS_SEAT, DURATION, BOARDING_POINT, DROPPING_POINT};
String selection = BUS_TYPE + " IN (?,?,?,?,?,?,?)";
String[] selectionArgs = (String[]) bus_types.toArray(new String[bus_types.size()]);
Cursor cursor = db.query(TABLE_NAME, data, selection, selectionArgs,
null, null, null);
BusDataModel busDataModel;
if (cursor.moveToFirst()) {
do {
busDataModel = new BusDataModel();
busDataModel.setid(cursor.getString(cursor.getColumnIndex(ID)));
busDataModel.setFromcityname(cursor.getString(cursor.getColumnIndex(FROM_CITY_NAME)));
busDataModel.setSrc_time(cursor.getString(cursor.getColumnIndex(SRC_TIME)));
busDataModel.setTocityname(cursor.getString(cursor.getColumnIndex(TO_CITY_NAME)));
busDataModel.setDes_time(cursor.getString(cursor.getColumnIndex(DES_TIME)));
busDataModel.setDistance(cursor.getString(cursor.getColumnIndex(DISTANCE)));
busDataModel.setBusid(cursor.getString(cursor.getColumnIndex(BUS_ID)));
busDataModel.setOperatorname(cursor.getString(cursor.getColumnIndex(OPERATOR_NAME)));
busDataModel.setBustype(cursor.getString(cursor.getColumnIndex(BUS_TYPE)));
busDataModel.setBusseat(cursor.getString(cursor.getColumnIndex(BUS_SEAT)));
busDataModel.setBusduration(cursor.getString(cursor.getColumnIndex(DURATION)));
busDataModel.setBoarding_point(cursor.getString(cursor.getColumnIndex(BOARDING_POINT)));
busDataModel.setDroppint_point(cursor.getString(cursor.getColumnIndex(DROPPING_POINT)));
busList.add(busDataModel);
} while (cursor.moveToNext());
}
db.close();
cursor.close();
return busList;
}
}
請顯示完整的代碼示例。你應該用適當的領域和方法展示一堂課。同時顯示錶格的創建方式。 –
我已經給出了你說的細節 –
我建議直接使用Cursor而不是將數據加載到列表中。當您擁有一個大型數據集時,這一點尤其重要,您不需要或不能一次加載到內存中。 –