Рейтинговая панель возвращает значение по умолчанию

У меня проблема с моим заявлением. После того, как пользователь нажимает кнопку "Готово", появляется "AlertDialog", и пользователь имеет возможность что-то оценить.

Моя проблема в том, что когда пользователь нажимает кнопку "ОК" из AlertDialog (positveButton), строка рейтинга возвращает значение по умолчанию (0.0). Я попытался использовать OnRatingBarChangeListener и сохранил рейтинг в переменной, но это тоже не сработало.


Java-код:

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

Спасибо за вашу помощь

1 ответ

Решение

Хорошо, я исправил это. For those who have the same problem: I changed final View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.dialog_rating, null); в final View view = this.getLayoutInflater().inflate(R.layout.dialog_rating, null);

Теперь работает нормально

Другие вопросы по тегам