使用自定义CursorAdapter的ListView无法正常工作

时间:2012-02-16 18:18:54

标签: android listview android-cursoradapter

在我的Android应用程序的一部分中,我有一个ListView,显示表中的条目列表。当用户点击ListView项目时,会显示此项目的新Intent

在新意图中,用户可以对此条目执行某些操作,如阅读编辑收藏(以及不喜欢 em>当项目已被收藏时)。在详细意图中,当收藏时,我将其表中条目的“标记”列更改为 1 ,当 0 更改为 0 > unfavorited 即可。

工作正常。但问题在于我的主人ListView。我为CursorAdapter设置了自定义ListView。我想添加ImageView,表示条目收藏的天气与否。在我的ListView项目的布局文件中,我为此添加了ImageView,并将其visibility设置为GONE

我想检测收藏的商品并将其明星ImageView visibility设置为VISIBLE。然后我在我的设备中运行了应用程序。像往常一样,没有任何条目被收藏。然后点击ListView中的第一项,打开此项目的详细信息页面。我收藏它并返回列表。

好的,现在第一个项目上有一个星形图标,但不仅仅是这个,还包括其他一些项目。这些错误加星标的项目的详细信息页面表示它不是收藏。所以问题不在于我的数据库操作。此外,我检查了显示标记项目的光标,其.getCount()也表示只有 1 被收藏。我找不到有问题的地方。我为自定义CursorAdapter编写了简化的源代码:

public class HereIsMyAdapter extends CursorAdapter {

    private final LayoutInflater mInflater;

    public HereIsMyAdapter(Context context, Cursor cursor) {
        super(context, cursor, true);
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        TextView txtTestText = (TextView) view.findViewById(R.id.txtTestText);
        ImageView imgMark = (ImageView) view.findViewById(R.id.imgMark);

        txtSureAz.setText(cursor.getString(cursor.getColumnIndex("azname")));

        boolean isMarked = cursor.getInt(cursor.getColumnIndex("marked")) == 1 ? true : false;

        if (isMarked) {
            imgMark.setVisibility(0);
        }

    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        View view = mInflater.inflate(R.layout.my_list_item, parent, false);
        bindView(view, context, cursor);
        return view;
    }

}

1 个答案:

答案 0 :(得分:1)

你试过类似的事吗?

boolean isMarked = cursor.getInt(cursor.getColumnIndex("marked")) == 1;
if (isMarked) {
    imgMark.setVisibility(View.VISIBLE);
}else{
    imgMark.setVisibility(View.GONE);
}