2017-02-17 2 views
0

編集:私のonCickListenerに評価値を送信してからShow My Dialogのコードを添付しました。 私は、数値形式(4.5)の評価を示すTextViewを持っています。このTextViewを押すと、ダイアログボックスがポップアップしてRatingBarの評価を変更できます。 Ratingbarの評価は、ポップアップするときのTextView Ratingに等しくなるように設定されています。これは期待通りに機能し、OKを押すとTextViewが新しいレーティングに更新されます。しかしTextViewをもう一度押すと、最初の最初の値が表示され、それを更新した値ではありません。私はonCreateDialog()内に自分のコードをすべて持っているので、これだけ分かっています。私はこれをOnStart()とonResume()を使って動作させようとしましたが、その後私のアプリケーションはクラッシュします。このコードを正しく書くにはどうしたらいいですか?添付初期ビルド後にDialogFramgmentへの入力をリセットするには?

はonCreadeDialog(内に設定されているすべてのコードと私の機能コードである)

public class RatingDialog extends DialogFragment{ 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState) { 

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 

    // Get the layout inflater 
    LayoutInflater inflater = getActivity().getLayoutInflater(); 

    // Inflate and set the layout for the dialog 
    // Pass null as the parent view because its going in the dialog layout 
    final View DialogView = inflater.inflate(R.layout.dialog_rating, null); 

    /** 
    * Retrieve the argument "num" (Previously rating) and set ratingbar´s rating equal to this. 
    */ 
    getArguments().getFloat("num"); 
    RatingBar ValueView = (RatingBar) DialogView.findViewById(R.id.Ratingbar); 
    ValueView.setRating(getArguments().getFloat("num")); 
    builder.setView(DialogView) 

      // Add action buttons 
      .setPositiveButton("OK", new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int id) { 
        /** 
        * Get the new value from the Ratingbar and send this back to the AddRating TextView 
        */ 
        RatingBar ValueView = (RatingBar) DialogView.findViewById(R.id.Ratingbar); 
        float Value = ValueView.getRating(); 
        TextView Text = (TextView) getActivity().findViewById(R.id.AddRating); 
        Text.setText(String.valueOf(Value)); 
        RatingDialog.this.getDialog().cancel(); 
       } 
      }) 
      .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        RatingDialog.this.getDialog().cancel(); 
       } 
      }); 

    return builder.create(); 
} 

}

以下

に評価値を送信し、その後、私のダイアログを表示onCickListenerするためのコードである:

/** 
    * Set the On Click Listener and send the Rating value to the Dialog 
    */ 
    final TextView Rating = (TextView) findViewById(R.id.AddRating); 
    String S = (String) Rating.getText(); 
    final Float F; 

    if (S==""){ 
     F=0.0f;} 
    else 
     F = Float.valueOf(S); 

    Rating.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      RatingDialog newFragment = new RatingDialog(); 
      newFragment.show(getSupportFragmentManager(), "Rating"); 

      /** 
      * Send Verdien av rating til dialogvinduet 
      */ 
      Bundle args = new Bundle(); 
      args.putFloat("num", F); 
      newFragment.setArguments(args); 
     } 
    }); 
+0

DialogFragment(より正確には、DialogFragmentに引数として渡す値を設定/計算するコード)を表示するコードを投稿します。 – Luksprog

答えて

0

常に同じFの値を渡しています。 それは次のようになります。

final TextView rating = (TextView) findViewById(R.id.AddRating); 

    rating.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      RatingDialog newFragment = new RatingDialog(); 
      newFragment.show(getSupportFragmentManager(), "Rating"); 

      /** 
      * Send Verdien av rating til dialogvinduet 
      */ 
      Bundle args = new Bundle(); 
      args.putFloat("num", TextUtils.isEmpty(rating.getText()) ? 
        0.0f : Float.valueOf(rating.getText().toString())); 
      newFragment.setArguments(args); 
     } 
    }); 

その場合、あなたはratingのTextViewの実際の値を渡します。

はい、java code conventionに従ってください。あなたのコードを読むのは難しいからです。

関連する問題