初始构建后如何重置DialogFramgment的输入?

时间:2017-02-17 12:26:32

标签: android dialogfragment

编辑:附上我的onCickListener的代码,它发送评级值,然后显示我的对话框。 我有一个TextView,它以数字格式(4.5)显示我的评级。当我按下这个TextView时会弹出一个对话框让我通过RatingBar更改评级。当弹出窗口时,Ratingbar的评级设置为等于TextView评级。这按预期运行,当我按OK时,TextView会更新为新的评级。但是当我再次按下TextView时,会显示初始的第一个值,而不是我刚刚更新的值。我已经弄清楚这是因为我在onCreateDialog()中有我的所有代码。我试图通过使用OnStart()和onResume()来实现这一点,但随后我的应用程序崩溃了。如何正确编写此代码?

附件是我的功能代码,其中所有代码都在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);
        }
    });

1 个答案:

答案 0 :(得分: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。因为你的代码很难阅读。