【发布时间】:2017-04-22 16:27:43
【问题描述】:
我已经设置了一个应用程序,该应用程序当前可以在数据库中查找一个输入 id,然后给出一个结果。例如。用户输入 id = 1 ,数据库包含 id 为 1 的记录,然后返回名称或编号等...
现在我想通过使用包含一系列 id 的数组列表查询我的数据库来稍微改进系统,例如3、456、731 等...我希望我的数据库搜索。我还将多个值分组到某些 id,例如数据库可能会搜索 3 的 id,然后会找到 5 个结果我希望它将每个结果的电话号码返回到另一个数组列表中,我可以打印到日志.
我希望我已经对此进行了充分的解释,但如果您需要更多信息,请提出问题。
下面的代码演示了用于获得单个结果的查询的修改版本,但我看不出我做错了什么以获得多个结果。
活动....
// 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。
标签: android sql arrays database arraylist