我是Android新手。而且我有一个问题(菜鸟问题),您能帮忙吗?
我有一个数据库方法getAllQuests()将表中的所有记录提取到ArrayList中。代码如下。我认为它有效。
下一步是从ArrayList中获取特定数据。问题是:该怎么做? 我假设我可以使用indexOf()或get()。但是我仍然不知道如何获取适当的行。输出如下。
所以我需要将表中的1行放入ArrayList中?还是我需要使用另一种方法?
// Getting All Quests
public List<Quest> getAllQuests() {
List<Quest> questList = new ArrayList<Quest>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_QUEST;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Quest quest = new Quest();
quest.setID(Integer.parseInt(cursor.getString(0)));
quest.setQuestName(cursor.getString(1));
quest.setQuestPlayer(cursor.getString(2));
quest.setStatus(cursor.getInt(3));
// Adding quest to list
questList.add(quest);
} while (cursor.moveToNext());
}
// return quest list
return questList;
}
// I try to figure out what to do
List<Quest> questList = db.getAllQuests();
int index = questList.indexOf(1);
Log.d(LOG_TAG, "--- index of Quest= " + index);
Log.d(LOG_TAG, "--- element of " + index + " is " + questList.get(1));
该代码的输出为:
10-20 08:39:30.210 D/myLogs: --- index of Quest= -1
10-20 08:39:30.210 D/myLogs: --- element of -1 is com.homemade.saga.Quest@41b10288
答案 0 :(得分:0)
您可以使用每个结构
遍历对象列表。for (Quest q: questlist) {
Log.d(LOG_TAG,"--- index of Quest= " + String.valueOf(q.getIndex));
...... etc
}