android:自定义 CursorAdapter 中的 ListView 项目样式

发布于 2024-09-12 16:12:56 字数 1201 浏览 8 评论 0原文

我有一个自定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

镜花水月 2024-09-19 16:12:56

我认为这只是 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文