SQlite
我已经成功地在 android 中创建了一个表。数据库中有一个性别(男/女)列。我想做的是检查性别类型并根据类型显示男性/女性图标。我使用setViewBinder()
which 通过列索引(我不知道究竟是哪一列),但我相信我们可以使用它来检查性别列。有一个更好的方法吗?
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);