2012-01-02 12 views
0

私は自分のアクティビティにリストビューを持ち、カスタムSimpleCursorAdapterを持っています。 カスタム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アダプタがリストアダプタとしてsettedされます。

コードはうまく動作しますが、ホテル名は大丈夫ですが、星は大丈夫ですが、問題はリストがスクロールした後、すべてのホテルが5つの星を獲得したことです。

このリストのスター数をデータベースからどのように管理するかについてのヘルプ。おかげさまで

答えて

1

私はあなたの状態をきれいにすることを確認する必要があると思います。 あなたのデータが示すように、あなたのアダプターは星を目に見えるようにします。しかし、彼らがそうでないときには、それらが消滅することはありません。

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配列をキャッシュして、セルをバインドするたびにツリーを走査するルックアップコストを避けることができます。 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; 
} 
+0

おかげで男...それは動作します... :) – Markonato

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); 

  • は、すべての行のすべてのそれらのfindViewById()の呼び出しを行うことを避けるために、ホルダーパターンを使用することを検討してRatingBarの代わりに、個々の星
  • を使用することを検討してくださいそれはのようなものでなければなりません
  • の代わりにgetInt()を呼び出してからintに変換することをご検討ください。
  • これはCursorAdapterあり、理想的にあなたがnewView()bindView()をオーバーライドしなければならないので、ないgetView()
関連する問題