android:自定义 CursorAdapter 中的 ListView 项目样式
我有一个自定义 CursorAdapter,用于相应地扩展自定义项布局和样式,如下面的代码所示。我的问题是,有时即使来自数据库的数据是正确的,也会提供错误的样式信息(因此对于下面的示例,我将得到 isEvent == true
但它会继续样式就好像 isEvent == false
一样。
这是一个已知的错误吗?或者我可以做些什么来解决这个问题?
private class EventAdapter extends CursorAdapter {
public EventAdapter(Context context) {
super(context, null);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return getLayoutInflater().inflate(R.layout.event_view_list_item, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
final boolean isEvent = cursor.getInt(EventQuery.IS_EVENT) == Event.EVENT;
final String eventName = cursor.getString(EventQuery.NAME);
final TextView eventNameView = (TextView) view.findViewById(R.id.event_name);
if (isEvent) {
eventNameView.setText(eventName);
view.findViewById(R.id.arrow).setVisibility(View.VISIBLE);
view.findViewById(R.id.in_play_icon).setVisibility(View.GONE);
} else {
eventNameView.setText(cursor.getString(EventQuery.NAME));
}
}
}
I have a custom CursorAdapter which I'm using to inflate a custom item layout and style accordingly, as shown in the code below. My issue is that sometimes the wrong style information is supplied even though the data coming from the DB is correct (so for the below example I'll get isEvent == true
but it'll go on to style as if isEvent == false
.
Is this a known bug? Am I doing something wrong or is there something I can do which will fix this?
private class EventAdapter extends CursorAdapter {
public EventAdapter(Context context) {
super(context, null);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return getLayoutInflater().inflate(R.layout.event_view_list_item, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
final boolean isEvent = cursor.getInt(EventQuery.IS_EVENT) == Event.EVENT;
final String eventName = cursor.getString(EventQuery.NAME);
final TextView eventNameView = (TextView) view.findViewById(R.id.event_name);
if (isEvent) {
eventNameView.setText(eventName);
view.findViewById(R.id.arrow).setVisibility(View.VISIBLE);
view.findViewById(R.id.in_play_icon).setVisibility(View.GONE);
} else {
eventNameView.setText(cursor.getString(EventQuery.NAME));
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这只是 Android 重用视图的老问题的一个例子。通过重置视图并在适当的情况下进行替换,可以修复此问题
I think this is just a case of the old issue of Android reusing views. By resetting the view and replacing where appropriate this can be fixed