所以我在这里有点困惑......
我的代码从我的 sqlite 数据库中获取信息并填充一个列表,然后使用标准数组适配器显示该列表。我想要做的是让它在这个列表中,如果“完成”表行值为“是”,则行颜色为绿色
这是我正在使用的表的数据库结构:
String CREATE_ACHIEVEMENTS_TABLE = "CREATE TABLE achievements ("
+ "id INTEGER PRIMARY KEY,"
+ "name VARCHAR,"
+ "type VARCHAR,"
+ "value VARCHAR,"
+ "completed VARCHAR"
+ ")";
这是我从数据库中获取列表的代码:
public ArrayList<String> getAchievements(Context context) {
ArrayList<String> achievementList = new ArrayList<String>();
String selectQuery = "SELECT * FROM achievements ORDER BY id asc";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
if (cursor.getString(4).equals("yes")) {
achievementList.add(cursor.getString(1)+" (completed)");
}
else {
achievementList.add(cursor.getString(1));
}
} while (cursor.moveToNext());
}
}
else {
achievementList.add(context.getResources().getString(R.string.na));
}
cursor.close();
db.close();
return achievementList;
}
这是我的自定义阵列适配器:
public class AchievementAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public AchievementAdapter(Context context, String[] values) {
super(context, R.layout.achievements, values);
this.context = context;
this.values = values;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater)this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.achievement_item, parent, false);
}
return row;
}
}
我真的不知道从这里去哪里。这是我的第一个 android 应用程序,我学到了很多东西,但我似乎无法弄清楚如何在自定义数组适配器方面实现这个简单的事情......我找到的所有教程都包含一堆我不想要的功能。如果列表项的“已完成”表值为“是”,我要做的就是将列表项的文本颜色设为绿色...