我已經設置了一個應用程序,它目前可以在數據庫中查找一個輸入id,然後給出一個結果。例如。用戶輸入ID = 1,數據庫包含一個ID爲1的記錄,然後返回名稱或編號等...使用數組列表查詢數據庫並獲取結果數組列表
現在我想通過查詢我的數據庫,使用一個包含一系列id例如3,456,731等...我希望我的數據庫搜索。我也將多個值分組到特定的ID,例如數據庫可能會搜索3的ID它會找到5個結果我希望它將每個結果的電話號碼返回到另一個ArrayList,我可以打印到日誌。
我希望我已經解釋過了,但如果您需要更多信息,請提問。
下面的代碼演示了用於獲取單個結果的查詢的修改版本,但我無法看到我在做錯了什麼以獲得多個結果。
活動....
// New array list which is going to be used to store values from the database
ArrayList<String> contactsList;
// This arrayList has been received from another activity and contains my id's
ArrayList<String> contacts = intent.getStringArrayListExtra("groupCode");
// The database which i'm using
ContactDBHandler contactDBHandler = new ContactDBHandler(getApplicationContext(), null, null, 1);
//getAllValues is used to pass my arraylist id's to the database.
contactsList = contactDBHandler.GetAllValues(contacts);
// Simple log statement to loop and display results
for (int i = 0; i < contactsList.size(); i++){
Log.i("Numbers", contactsList.get(i));
}
ContactDBHandler
查詢
// I'm telling it to get the contact number from the contact_list
// when the groupcode matches the code recieved.
public ArrayList<String> GetAllValues(ArrayList groupCode)
{
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = null;
String alarmName = "";
ArrayList<String> list = new ArrayList<>();
cursor = db.rawQuery("SELECT contact_number FROM contact_list WHERE grp_code=?", new String[]{groupCode+ ""});
if (cursor.moveToFirst())
{
do
{
list.add(cursor.getString(0));
}
while (cursor.moveToNext());
}
if (cursor != null && !cursor.isClosed())
{
cursor.close();
}
return list;
}
感謝 你能看到我在哪裏呢?
您是否獲得一個異常或不輸出預期的結果不匹配? –
目前我的日誌只是空的,也只是測試我的聯繫人數組列表,我可以確認它有ID等待使用。 – user3403733