从ratingBar.getRating()得到0.0值; (默认值)即使我更改了评级栏值?

时间:2017-04-09 09:23:58

标签: android ratingbar

我调查了this,但似乎对我不起作用。我在警告对话框中显示评级栏。按下按钮时,我只获得 0.0 的值。以下是代码:

rateme.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numStars="5"
android:id="@+id/ratingBar"
android:stepSize="1.0"/>
</LinearLayout>

Java.Code

 //Typecase
 final View view = this.getLayoutInflater().inflate(R.layout.rateme, null);
 ratingBar=(RatingBar)view.findViewById(R.id.ratingBar);

 AlertDialog.Builder alertDialog = null;
        alertDialog = new AlertDialog.Builder(this);
                alertDialog.setTitle("Rate and Review");
    if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
        alertDialog.setView(R.layout.rateme);
    }else{
        alertDialog.setMessage("Please  Rate and Review");
    }
                alertDialog.setPositiveButton("SUBMIT", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        final String myPackage = getPackageName();
                        String value=" "+ratingBar.getRating();
                        Log.d(TAG," "+value);   **//Getting value 0.0 although I change  ratingbar and submit.**
                    }
                }).setNegativeButton("QUIT", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                }).setCancelable(true).setIcon(android.R.drawable.star_big_on).create();
    ratingBar.setOnRatingBarChangeListener(new RatingBar.OnRatingBarChangeListener() {
        @Override
        public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
           //The toast below is not executed even when I chaned the ratingbar stars.
            Toast.makeText(Front.this, "Blank", Toast.LENGTH_SHORT).show();
            //Log.d(TAG,"Reached on Listener");
        }
    });
    alertDialog.show();
 }

似乎我已经完成了所有事情,但我得到了值0.0(常数)。可能有什么问题我错了?

2 个答案:

答案 0 :(得分:0)

问题出在这里

setView

你不需要这个if / else,因为alertDialog.setView(R.layout.rateme);是自Api版本1以来Android的一部分。另一方面,你的评级栏与你在对话框中看到的不同,因为{{1} }再次膨胀评级栏。使用alertDialog.setView(view);可以解决您的问题

答案 1 :(得分:0)

出了什么问题

您将布局膨胀一次,保持对RatingBar的引用,然后忘记布局。

final View view = this.getLayoutInflater().inflate(R.layout.rateme, null);
ratingBar = (RatingBar)view.findViewById(R.id.ratingBar);
// You never work with view again.

然后,AlertDialog.Builder会扩展实际显示的不同布局(基于相同的资源ID)。

alertDialog.setView(R.layout.rateme);

如何解决

使用您夸大的布局。

final View view = this.getLayoutInflater().inflate(R.layout.rateme, null);
ratingBar = (RatingBar)view.findViewById(R.id.ratingBar);
alertDialog.setView(view);

如何进一步解决

使用对话框上下文(非活动上下文),以便在您有黑暗活动并想要轻量级对话框时正确绘制对话框。

final AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
final LayoutInflater layoutInflater = LayoutInflater.from(alertDialog.getContext());
final View view = layoutInflater.inflate(R.layout.rateme, null);
ratingBar = (RatingBar)view.findViewById(R.id.ratingBar);
alertDialog.setView(view);

始终使用在浏览视图时可以获得的最接近,最符合逻辑的上下文。