Reputation: 61
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 adapter is setted as list adapter.
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.
Any help how to manage number of stars in that listView from database. Thanks.
Upvotes: 0
Views: 362
Reputation: 10184
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);
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;
}
Upvotes: 1
Reputation: 1007296
You are never making the stars invisible. It should be something like:
for (int i = 0; i < numberOfStars; i++)
images[i].setVisibility(View.VISIBLE);
for (int i = numberOfStars; i < 5; i++)
images[i].setVisibility(View.INVISIBLE);
All that being said:
RatingBar
instead of individual starsfindViewById()
calls on every rowgetInt()
instead of getString()
and then converting it to an int
yourselfCursorAdapter
, ideally you should be overriding newView()
and bindView()
, not getView()
Upvotes: 0