我有一個ListView
與一些元素,我想改變行的背景取決於他們在數據庫中的類型。我得到的是一個SimpleCursorAdapter
實例,它的功能是adapter.setViewBinder(...)
。但看起來,我無法訪問連續的LinearLayout
。下面的代碼:我可以從ViewBinder中的ListView行更改LinearLayout的背景嗎?
final SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.day, c, new String[] { "name_con", "start", "end",
"type", "prof", "room" }, new int[] { R.id.subjectName,
R.id.subjectStart, R.id.subjectEnd, R.id.subjectType,
R.id.subjectProf, R.id.subjectRoom });
adapter.setViewBinder(new ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int column) {
switch (view.getId()) {
case R.id.subjectName:
final String colNameName = cursor.getString(1);
((TextView) view).setText(colNameName);
return true;
case R.id.subjectStart:
final int colNameStart = cursor.getInt(2);
Date dStart = new Date(colNameStart * 1000);
SimpleDateFormat sdfStart = new SimpleDateFormat(
"HH:mm", Locale.getDefault());
String startString = sdfStart.format(dStart);
((TextView) view).setText(startString);
return true;
case R.id.subjectEnd:
final int colNameEnd = cursor.getInt(3);
Date dEnd = new Date(colNameEnd * 1000);
SimpleDateFormat sdfEnd = new SimpleDateFormat(
"HH:mm", Locale.getDefault());
String EndString = sdfEnd.format(dEnd);
((TextView) view).setText(EndString);
return true;
case R.id.subjectType:
final int colNameType = cursor.getInt(4);
switch(colNameType){
case 0:
((TextView) view).setText(R.string.practice);
break;
case 1:
((TextView) view).setText(R.string.course);
break;
case 2:
((TextView) view).setText(R.string.practica);
break;
case 3:
((TextView) view).setText(R.string.seminar);
}
return true;
case R.id.subjectProf:
final String colNameProf = cursor.getString(5);
((TextView) view).setText(colNameProf);
return true;
case R.id.subjectRoom:
final String colNameRoom = cursor.getString(6);
((TextView) view).setText(colNameRoom);
return true;
case R.id.dayCell:
((LinearLayout)view).setBackgroundColor(Color.WHITE);
default:
return false;
}
}
});
this.setListAdapter(adapter);
完美的作品!謝謝。 – 2011-05-06 13:39:43
很高興聽到它的工作。 – Femi 2011-05-06 13:42:57