I have in my MainActivity a ListView which shows a list of movies that i'm populating with a SimpleCursorAdapter. In another activity the user is able to set the rating with a RatingBar and the float value is saved in the Database. I would like to show on the MainActivity next to the name of the movie, the rating set in the other activity.
I tried to use another RatingBar only as an indicator and to set it but it didn't work. I tried to use five insivible imageviews ( stars) which became visible depending on the float value but it didn't work. Now i'm trying to use the viewbinder, with if statements : get the float value of the rating bar from the database, parse it to Int and set the right drawable into an Imageview.
There are no errors in the LogCat but the imageview doesn't change at all... Maybe i'm wrong in refreshing the list? I'm a principiant please help me :) What am I doing wrong??????
//This is my main activity:
public class MainActivity extends ListActivity implements OnItemClickListener {
public static final int ADD_MANUAL = 1;
private Cursor cursor;
private MoviesHandler db;
private String [] cols = new String [] {MoviesDbConstants.MOVIE_TITLE , MoviesDbConstants.MOVIE_URI};
private int [] views = new int [] { R.id.title , R.id.listImgmovie};
private SimpleCursorAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new MoviesHandler(this);
cursor = db.getAllMovies();
adapter = new SimpleCursorAdapter(this,
R.layout.list, cursor, cols, views, 0);
adapter.setViewBinder(new CustomViewBinder());
ListView lv = getListView();
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);
registerForContextMenu(lv);
}
@Override
protected void onResume() {
super.onResume();
refreshList();
}
private void refreshList() {
cursor = db.getAllMovies();
adapter.changeCursor(cursor);
adapter.notifyDataSetChanged();
}
// and this is the ViewBinder:
private class CustomViewBinder implements ViewBinder {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
if (columnIndex == cursor.getColumnIndex(MoviesDbConstants.MOVIE_RATING)) {
int num = (int) cursor.getFloat(columnIndex);
switch (num){
case 1:
((ImageView) view).setBackgroundResource(R.drawable.one_star);
break;
case 2:
((ImageView) view).setBackgroundResource(R.drawable.two_star);
break;
case 3:
((ImageView) view).setBackgroundResource(R.drawable.three_star);
break;
case 4:
((ImageView) view).setBackgroundResource(R.drawable.four_star);
break;
case 5:
((ImageView) view).setBackgroundResource(R.drawable.five_star);
break;
}
return true;
}
return false;
}
}