简体   繁体   中英

Rating Bar Android

Hi I am busy making a program where you get to rate people I've been playing around with the Rating Bar widget however I am struggling to make it show a rating after selecting the amount of stars. What I'm I doing wrong. I would also like to to know if the android sdk have anything on up and down votes and would it be wise to use images for a rating system.

The reason why some of the code is commented out is because my app won't run when its uncommented but I know its needed to do what I want to do.

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
public class rating_system_fragment extends Fragment implements OnRatingBarChangeListener{

RatingBar ratingBar;
TextView ratingResult;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    inflater.inflate(R.layout.rating_system_fragment,container,false);
    //setContentView(R.layout.activity_main);
    return inflater.inflate(R.layout.rating_system_fragment, container, false);
    //ratingResult = (TextView) ratingResult.findViewById(R.id.textViewRating);
    //((RatingBar) ratingBar.findViewById(R.id.ratingBar))
      //      .setOnRatingBarChangeListener(this);
}

@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
                            boolean fromTouch) {
    final int numStars = ratingBar.getNumStars();
    ratingResult.setText(rating + "/" + numStars);
}

}

You have several syntax erros in your commented code. It should look like this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View rootView = inflater.inflate(R.layout.rating_system_fragment,container,false);
    ratingResult = (TextView) rootView.findViewById(R.id.textViewRating);
    ratingBar = (RatingBar) rootView.findViewById(R.id.ratingBar);
    ratingBar.setOnRatingBarChangeListener(this);

    return rootView;
}

The key here is that ratingBar and textViewRating are childs of your root view, and should only be accessed when you have aready inflated them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM