自SimpleCursorAdapter错误 [英] Custom SimpleCursorAdapter error

查看:173
本文介绍了自SimpleCursorAdapter错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的ListView在我的活动,并定制SimpleCursorAdapter。
自SimpleCursorAdapter看起来是这样的。

I have ListView in my Activity, and custom SimpleCursorAdapter. Custom SimpleCursorAdapter looks like this.

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适配器设置好的为列表适配器。

CustomSimpleCursor adapter is setted as list adapter.

code的伟大工程,酒店名称是确定的,星星是好的,但问题是,滚动列表一段时间后,每家酒店都会获得5星,我知道它`真的。

Code works great, hotel name is ok, stars are ok, but problem is that after scrolling list some time, every hotel gets 5 stars, and I know that it`s true.

任何帮助如何管理从数据库中的ListView数星星。
谢谢你。

Any help how to manage number of stars in that listView from database. Thanks.

推荐答案

我觉得你只需要确保你清理你的状态。
适配器使得恒星可见的数据显示它。然而,它没有以往任何时候都让他们GONE /不可见时,他们没有。

I think you just need to make sure you clean your state. Your adapter makes stars visible as your data shows it. However it doesn't ever make them GONE/INVISIBLE when they aren't.

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阵列才能避免遍历每个绑定一个单元时间树的查找成本。这样的:

Also if you want you could cache that ImageView array to avoid the look up cost of traversing the tree each time you bind a cell. Like:

@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;
 }

这篇关于自SimpleCursorAdapter错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆