Example usage for android.widget RatingBar setRating

List of usage examples for android.widget RatingBar setRating

Introduction

In this page you can find the example usage for android.widget RatingBar setRating.

Prototype

public void setRating(float rating) 

Source Link

Document

Sets the rating (the number of stars filled).

Usage

From source file:com.androidquery.AbstractAQuery.java

/**
 * Set the rating of a RatingBar./*  w  w w .j  ava  2 s  . c  o m*/
 *
 * @param rating the rating
 * @return self
 */
public T rating(float rating) {

    if (view instanceof RatingBar) {
        RatingBar rb = (RatingBar) view;
        rb.setRating(rating);
    }
    return self();
}

From source file:com.example.android.ennis.barrett.popularmovies.DetailFragment.java

/**
 * Sets all the views to related id./*  ww w  .  jav  a 2s  . c  om*/
 * @param _id The id of the movie
 */
private void setDetails(long _id) {
    /*
     * get references
     */
    TextView title = (TextView) mRootView.findViewById(R.id.original_title);
    TextView overview = (TextView) mRootView.findViewById(R.id.overview);
    TextView date = (TextView) mRootView.findViewById(R.id.date);
    TextView voteAverage2 = (TextView) mRootView.findViewById(R.id.vote_average2);
    ImageView poster = (ImageView) mRootView.findViewById(R.id.poster);
    RatingBar voteAverage = (RatingBar) mRootView.findViewById(R.id.vote_average);
    LinearLayout videos = (LinearLayout) mRootView.findViewById(R.id.videos);
    LinearLayout reviews = (LinearLayout) mRootView.findViewById(R.id.reviews);
    CompoundButton isFavorite = (CompoundButton) mRootView.findViewById(R.id.favorite);

    ContentResolver contentResolver = getActivity().getContentResolver();

    /*
     * Queries the movies table
     */
    Cursor cursor = contentResolver.query(TMDbContract.Movies.URI, null, TMDbContract.Movies.ID + " = ?",
            new String[] { mID + "" }, null);
    cursor.moveToFirst();

    /*
     * sets most views to the movie
     */
    title.setText(cursor.getString(cursor.getColumnIndex(TMDbContract.Movies.ORIGINAL_TITLE)));

    overview.setText(cursor.getString(cursor.getColumnIndex(TMDbContract.Movies.OVERVIEW)));

    date.setText(cursor.getString(cursor.getColumnIndex(TMDbContract.Movies.RELEASE_DATE)));

    // Setups up the poster
    String posterURLString = "http://image.tmdb.org/t/p/w185/"
            + cursor.getString(cursor.getColumnIndex(TMDbContract.Movies.POSTER));
    Log.v(TAG, posterURLString);
    Picasso.with(getActivity()).load(posterURLString).into(poster);

    String bool = cursor.getString(cursor.getColumnIndex(TMDbContract.Movies.IS_FAVORITE));

    isFavorite.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            ContentValues value = new ContentValues();
            String isFavorite = "0";
            if (((CompoundButton) v).isChecked()) {
                isFavorite = "1";
            }

            value.put(TMDbContract.Movies.IS_FAVORITE, isFavorite);
            int num = getActivity().getContentResolver().update(TMDbContract.Movies.URI, value,
                    TMDbContract.Movies._ID + " = ?", new String[] { Long.toString(mID) });
        }
    });

    //short circuit logic stops app from crashing..So don't reverse the expression
    if (bool != null && bool.equals("1")) {
        isFavorite.setChecked(true);
    } else {
        isFavorite.setChecked(false);
    }

    //Set up the RatingBar and the TextView with the rating
    float vote = cursor.getFloat(cursor.getColumnIndex(TMDbContract.Movies.VOTE_AVERAGE));
    voteAverage2.setText(vote + " / 10 ");
    vote /= 2;
    Log.v(TAG, vote + "");
    voteAverage.setRating(vote);
    Log.v(TAG, voteAverage.getRating() + "");

    /*
     * Set up the videos LinearLayout.
     * Queries the table and then creates TextViews to display the results
     */
    Cursor cursorVideos = contentResolver.query(TMDbContract.Videos.URI, null,
            TMDbContract.Videos.MOVIE_IDS + " = ?",
            new String[] { cursor.getString(cursor.getColumnIndex(TMDbContract.Movies.MOVIE_ID)) }, null);
    VideoCursorAdapter adapter = new VideoCursorAdapter(getActivity(), R.layout.video_card, cursorVideos);

    //loop to create TextViews to display the results
    for (int i = 0; i < adapter.getCount(); i++) {
        View view = adapter.getView(i, null, null);
        view.setOnClickListener(this);
        videos.addView(view);
    }

    /*
     * Set up the reviews LinearLayout.
     * Queries the table and then creates TextViews to display the results
     */
    Cursor cursorReviews = contentResolver.query(TMDbContract.Reviews.URI, null,
            TMDbContract.Reviews.MOVIE_IDS + " = ?",
            new String[] { cursor.getString(cursor.getColumnIndex(TMDbContract.Movies.MOVIE_ID)) }, null);

    if (cursorReviews.getCount() == 0) {
        mRootView.findViewById(R.id.reviews_header).setVisibility(View.GONE);
    } else {
        ReviewCursorAdapter adapter2 = new ReviewCursorAdapter(getActivity(), R.layout.review_card,
                cursorReviews);
        for (int i = 0; i < adapter2.getCount(); i++) {
            View view = adapter2.getView(i, null, null);
            reviews.addView(view);
        }
    }
}