我可以从 ViewBinder 中的 ListView 行更改 LinearLayout 的背景吗?
我有一个包含一些元素的 ListView,我想根据它们在数据库中的类型更改行的背景。我得到的是一个 SimpleCursorAdapter
实例及其函数 adapter.setViewBinder(...)
。但似乎我无法访问一行的 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);
I have a ListView
with some elements and I want to change the background of the row depending on their type in the database. What I got is a SimpleCursorAdapter
instance and it's function adapter.setViewBinder(...)
. But it seems, that I can't access the LinearLayout
of a row. Here's the code:
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该能够执行以下操作:
编辑:为了提高效率,您可能只想在其中一个 ViewBinder 传递中执行此操作,并且您需要确保它是创建的 LinearLayout在
R.layout.day
中,否则最终会出现 ClassCast 异常。You should be able to do something like:
EDIT: for efficiency, you'll probably only want to do it in one of the
ViewBinder
passes, and you'd need to be sure its a LinearLayout that's created inR.layout.day
or you'll end up with a ClassCast exception.您可以调用 setBackgroundColor(int color)、setBackgroundResource(int) 或 setBackgroundResource(int resid) 来更改视图的背景。
BR,
克里斯托弗
You could call the setBackgroundColor(int color), setBackgroundResource(int) or setBackgroundResource(int resid) to change the background of your view.
BR,
Christoffer
覆盖 bindView 在你的
CursorAdapter
中Override bindView in your
CursorAdapter