我的Activity中有ListView,自定义SimpleCursorAdapter。 自定义SimpleCursorAdapter看起来像这样。
public class MySimpleCursorAdapter extends SimpleCursorAdapter {
private Activity activity;
private Cursor cursor;
public MySimpleCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to, Activity activity) {
super(context, layout, c, from, to);
this.activity = activity;
this.cursor = c;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null)
view = View.inflate(this.activity,
com.tour.R.layout.third_level_list_item, null);
cursor.moveToPosition(position);
String hotelName=cursor.getString(cursor.getColumnIndexOrThrow(TodoDbAdapter.KEY_HOTEL));
String stars = cursor.getString(cursor.getColumnIndexOrThrow(TodoDbAdapter.KEY_ROW_STARS));
int numberOfStars = Integer.parseInt(stars);
TextView hotel = (TextView) view.findViewById(com.tour.R.id.hotel);
if (hotel != null)
hotel.setText(hotelName);
ImageView[] images = new ImageView[5];
images[0] = (ImageView) view.findViewById(com.tour.R.id.slika1);
images[1] = (ImageView) view.findViewById(com.tour.R.id.slika2);
images[2] = (ImageView) view.findViewById(com.tour.R.id.slika3);
images[3] = (ImageView) view.findViewById(com.tour.R.id.slika4);
images[4] = (ImageView) view.findViewById(com.tour.R.id.slika5);
for (int i = 0; i < numberOfStars; i++)
images[i].setVisibility(View.VISIBLE);
return view;
}
}
CustomSimpleCursor适配器被设置为列表适配器。
代码效果很好,酒店名称还可以,星级还可以,但问题是滚动列表一段时间后,每家酒店都有5颗星,我知道这是真的。
如何管理数据库中listView中的星数。 感谢。
答案 0 :(得分:1)
我认为你只需要确保清理你的状态。 您的适配器会在数据显示时显示星标。然而,当它们不是时,它们并没有使它们成为GONE / INVISIBLE。
ImageView[] images = new ImageView[5];
images[0] = (ImageView) view.findViewById(com.tour.R.id.slika1);
images[1] = (ImageView) view.findViewById(com.tour.R.id.slika2);
images[2] = (ImageView) view.findViewById(com.tour.R.id.slika3);
images[3] = (ImageView) view.findViewById(com.tour.R.id.slika4);
images[4] = (ImageView) view.findViewById(com.tour.R.id.slika5);
for (ImageView image : images) {
image.setVisibility(View.INVISIBLE);
}
for (int i = 0; i < numberOfStars; i++)
images[i].setVisibility(View.VISIBLE);
此外,如果您希望可以缓存该ImageView数组,以避免每次绑定单元格时遍历树的查找成本。像:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = View.inflate(this.activity, com.tour.R.layout.third_level_list_item, null);
ImageView[] images = new ImageView[5];
images[0] = (ImageView) view.findViewById(com.tour.R.id.slika1);
images[1] = (ImageView) view.findViewById(com.tour.R.id.slika2);
images[2] = (ImageView) view.findViewById(com.tour.R.id.slika3);
images[3] = (ImageView) view.findViewById(com.tour.R.id.slika4);
images[4] = (ImageView) view.findViewById(com.tour.R.id.slika5);
view.setTag(images);
}
ImageView[] images = (ImageView[]) view.getTag();
for (ImageView image : images) {
image.setVisibility(View.INVISIBLE);
}
for (int i = 0; i < numberOfStars; i++)
images[i].setVisibility(View.VISIBLE);
return view;
}
答案 1 :(得分:0)
你永远不会让星星变得隐形。它应该是这样的:
for (int i = 0; i < numberOfStars; i++)
images[i].setVisibility(View.VISIBLE);
for (int i = numberOfStars; i < 5; i++)
images[i].setVisibility(View.INVISIBLE);
所有这一切:
RatingBar
代替个别明星findViewById()
次调用getInt()
而不是getString()
,然后自己将其转换为int
CursorAdapter
,理想情况下,您应该覆盖newView()
和bindView()
,而不是getView()