评分栏 returns 默认值

Rating Bar returns the default Value

我的申请有问题。用户点击 "finish button" 后,会出现 "AlertDialog" 并且用户可以对某些内容进行评分。

我的问题是,当用户从 AlertDialog (positveButton) 中点击确定按钮时,评级栏 returns 默认值 (0.0)。我尝试使用 OnRatingBarChangeListener 并将评级保存在变量中,但这也不起作用。


Java代码:

public RatingBar ratingBar;

警报对话框

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 Whosebug 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代码

<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>

感谢您的帮助

好的,我修好了。 对于那些有同样问题的人: 我将 final View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.dialog_rating, null); 更改为 final View view = this.getLayoutInflater().inflate(R.layout.dialog_rating, null);

现在可以正常使用了