假设我有一个像这样的SQLite表:
public static final String DataBaseName = "test1.db";
public static final String DataTableName = "jobsTable";
public static final String ColID = "ID";
public static final String ColPlace = "placeName";
public static final String ColPosition = "position";
public static final String ColStartingTime = "startingTime";
public static final String ColHours = "hours";
public static final String ColAddress = "address";
public static final String ColPhoneNumber = "phoneNumber";
我已经知道如何为一行编写查询并将值存储在ArrayList中。 Bellow代码获取placeName列的所有成员并将它们存储在arraylist中。例如
"placename1","placeName2","placeName3","placeName4",...
代码:
public ArrayList<String> getAllPlacesNames() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor result = db.rawQuery("select placeName FROM " + DataTableName,null);
ArrayList<String> companyNames = new ArrayList<String>();
while(result.moveToNext()) {
String places = result.getString(result.getColumnIndex("placeName"));
companyNames.add(places);
}
return companyNames;
}
我正在尝试编写一个上面的方法来获取两列(PlaceName
和position
)中的值并将它们存储起来:
"placeName1/position1","placeName2/position2","placeName3/position3","placeName4/position4",...
如何做到这一点?谢谢
答案 0 :(得分:0)
public ArrayList<String> getAllCompanyPositionsAndNamesAndPhone() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor result = db.rawQuery("select placeName, position, startingTime FROM " + DataTableName,null);
ArrayList<String> companyPositions = new ArrayList<String>();
while(result.moveToNext()) {
String position = result.getString(result.getColumnIndex("position"));
String places = result.getString(result.getColumnIndex("placeName"));
String phoneNum = result.getString(result.getColumnIndex("startingTime"));
String merged = places + "/" + position + "\n" + "Starts on: " + phoneNum;
companyPositions.add(merged);
}
return companyPositions;
答案 1 :(得分:0)
i建议使用Hashmap保存键/值数据这是我的解决方案。
public ArrayList<String> getAllPlacesNames() {
SQLiteDatabase mydb = this.getWritableDatabase();
Cursor result = mydb.rawQuery("select placeName, position FROM " + DataTableName,null);
HashMap<String, Integer> data= new HashMap<String, String>();
while(result.moveToNext()) {
map.put(result.getString(result.getColumnIndex("placeName")), result.getString(result.getColumnIndex("position")));
}
H @ ppy c0de !!