【发布时间】:2011-03-14 20:19:12
【问题描述】:
我使用 SimpleCursorAdapter 和一个 xml 文件,其中定义了一些视图:
<LinearLayout ...>
<ImageView android:id="@+id/listIcon" />
<TextView android:id="@+id/listText" />
</LinearLayout>
我的目标是以编程方式设置TextView的文本颜色,以及LinearLayout的背景颜色(即ListView中的每一行);颜色是从数据库返回的。
例如,我在尝试操纵 TextView 时遇到了 NPE,但在它没有任何抱怨的情况下发现它:
TextView tv = (TextView) findViewById(R.id.listText);
tv.setTextColor(color); // NPE on this line
这是公平的;如果列表中有多个条目,则可以合理地假设“R.id.listText”将不起作用。所以我扩展了 SimpleCursor Adapter:
public View getView(int position, View convertView, ViewGroup parent) {
View row = super.getView(position, convertView, parent);
TextView text = (TextView) row.findViewById(R.id.listText);
// ImageView icon = (ImageView) row.findViewById(R.id.listIcon);
// If there's an icon defined
if (mIcon_id != 0) {
// icon.setImageResource(mIcon_id);
}
// If text color defined
if (mTextColor != 0) {
text.setTextColor(mTextColor);
}
// If background color set
if (mBackgroundColor != 0) {
row.setBackgroundColor(mBackgroundColor);
}
return(row);
}
我得到两个不同的错误:
- 类似的 NPE 被抛出 "text.setTextColor(mTextColor)"
- 如果带有 ImageView 的行是 未注释,我得到一个 "ClassCastException: android.widget.TextView" 我在哪里 打电话 “row.findViewById(R.id.listIcon)”
作为参考,我尝试使用 Commonsware 的示例代码,并将其应用于我的情况。 link (pdf)
改成这样:
public View getView(int position, View convertView, ViewGroup parent) {
convertView = super.getView(position, convertView, parent);
if (convertView == null) convertView = View.inflate(mContext, R.layout.theme_item, null);
TextView text = (TextView) convertView.findViewById(R.id.listText_tv);
ImageView icon = (ImageView) convertView.findViewById(R.id.listIcon_iv);
// If there's an icon defined
if (mIcon_id != 0) {
icon.setImageResource(mIcon_id);
}
// If text color defined
if (mTextColor != 0) {
text.setTextColor(mTextColor);
}
// If background color set
if (mBackgroundColor != 0) {
convertView.setBackgroundColor(mBackgroundColor);
}
bindView(convertView, mContext, mCursor);
return(convertView);
}
现在我在下一个活动中得到一个 ClassCastException(单击列表项)。在下一个活动中没有任何修改;它在对具有条目的列表使用 SimpleListAdapter 时起作用(点击会导致 Activity2),所以我认为在这个扩展类中我仍然做错了。
【问题讨论】:
-
除了 ClassCastException,如果您的光标只返回一个条目,这段代码是否有效?
-
@Quintin 我无法检查这一点,我恢复到旧版本,并尝试使用以下建议的新版本。你知道为什么在这种情况下会抛出 ClassCastException 吗?单击现在工作(颜色已更改)列表中的条目后,它应该打开一个新活动 - 我现在在这个(常规活动,没有列表)中得到了一个 ClassCastException,而我以前没有。
-
*没有尝试过Project > Clean,所以我会在我开始工作时尝试一下;在 SO 上搜索显示生成的 R 文件可能会不同步导致此问题。
-
Project > Clean 解决了 ClassClassExceptions :)
标签: android android-layout listactivity simplecursoradapter