我在android中使用SQlite
成功创建了一个表。数据库中有一个性别栏(男/女)。我想做的是检查性别类型并根据类型显示男/女图标。我使用setViewBinder()
传递列索引(我确切知道哪一列),但我相信我们可以用它来检查性别列。有更好的方法吗?
ListView lv=(ListView)findViewById(R.id.mylist);
dbh = new DatabaseHelper(this);
c = dbh.getReadableDatabase().rawQuery("SELECT _id, " +
DatabaseHelper.NAME +
", " + DatabaseHelper.LASTNAME +
", " + DatabaseHelper.ans2 + ", " + DatabaseHelper.SEX +
" FROM " +
DatabaseHelper.TABLE_NAME, null); // initializing
String[] dataFrom ={DatabaseHelper.NAME, DatabaseHelper.LASTNAME, DatabaseHelper.ans2, DatabaseHelper.SEX};
int[] dataTo = {R.id.name, R.id.value1, R.id.value2, R.id.icon};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.row, c, dataFrom, dataTo);
adapter.setViewBinder(new ViewBinder(){
public boolean setViewValue(View view, Cursor cursor,
int columnIndex) {
if (view.getId() == R.id.icon) {
int sex = cursor.getInt(columnIndex);
String s = String.valueOf(sex);
if (s == "male") {
((ImageView) view).setImageResource(R.drawable.male);
} else if (s == "female"){
((ImageView) view).setImageResource(R.drawable.female);
}
return true;
} else {
return false;
}
}
});
lv.setAdapter(adapter);
答案 0 :(得分:2)
方法bindView允许您轻松设置/检查数据,因为您收到:
答案 1 :(得分:2)
if (s == "male")
更改
if (s.equals("male"))