问题
I have a problem with my application. After the user hits the "finish button" an "AlertDialog" comes up and the user has the ability to rate something.
My problem is when the user hits the ok button from the AlertDialog (positveButton) the rating bar returns the default value (0.0). I tried to use the OnRatingBarChangeListener and saved the rating in a variable but this didn't work either.
Java Code:
public RatingBar ratingBar;
AlertDialog
AlertDialog.Builder build = new AlertDialog.Builder(MainActivity.this);
final View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.dialog_rating, null); // This a have from another Stackoverflow question
build.setView(R.layout.dialog_rating);
ratingBar = (RatingBar) view.findViewById(R.id.dialog_ratingBar);
build.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dia, int which) {
Log.i("[Rate]", String.valueOf(ratingBar.getRating())); //This returns 0.0
Log.i("[Rate]", "" + ratingBar); // Is Valid check
}
});
AlertDialog dialog = build.create();
dialog.setTitle(R.string.rating_dialog_title);
ialog.show();
XML Code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RatingBar
style="@android:style/Widget.DeviceDefault.RatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/dialog_ratingBar"
android:numStars="5"
android:stepSize="0.5"
android:layout_gravity="center_horizontal" />
</LinearLayout>
Thanks for your help
回答1:
Ok, I fixed it.
For those who have the same problem:
I changed final View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.dialog_rating, null);
to final View view = this.getLayoutInflater().inflate(R.layout.dialog_rating, null);
Now it works fine
来源:https://stackoverflow.com/questions/38975895/rating-bar-returns-the-default-value