2016-08-16 73 views
1

我的應用程序出現問題。在用戶點擊「完成按鈕」後,出現「AlertDialog」,用戶可以對某些事物進行評分。評級欄返回默認值

我的問題是,當用戶點擊AlertDialog(positveButton)的ok按鈕時,評級欄返回默認值(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 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代碼

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

感謝您的幫助

+0

嘗試此鏈接:如何從的RatingBar獲得評分](http://stackoverflow.com/a/7332660/6541084) –

+0

不,問題仍然存在,但感謝您的幫助。 –

回答

0

好吧,我固定它。 對於那些誰都有同樣的問題: 我改變final View view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.dialog_rating, null);final View view = this.getLayoutInflater().inflate(R.layout.dialog_rating, null);

現在它工作正常

+0

獲取保存值。你可以幫我嗎? – Queendevelopers