在我的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;
}
}
答案 0 :(得分:1)
boolean isMarked = cursor.getInt(cursor.getColumnIndex("marked")) == 1;
if (isMarked) {
imgMark.setVisibility(View.VISIBLE);
}else{
imgMark.setVisibility(View.GONE);
}