在android中再次打开对话框片段时,不显示选中的复选框

时间:2018-12-13 09:20:03

标签: android android-fragments

我创建了一个带有复选框的对话框片段。它可以在主活动的文本视图中显示所选项目的总数。但是,当我再次打开该对话框时,它不会显示我之前选择的复选框。我想在再次打开对话框时显示先前选择的复选框。我不明白该怎么做。我尝试了很多次,但被卡住了。请帮我解决这个问题。

XML布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="18dp"
    android:orientation="vertical"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center_vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1"
                android:orientation="horizontal"
                android:layout_marginTop="@dimen/default_gap">

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="@string/_01"
                        android:textAlignment="center"
                        android:textSize="20sp"
                        />

                    <CheckBox
                        android:id="@+id/checkBox01"
                        android:layout_width="wrap_content"
                        android:layout_height="70dp"
                        android:button="@drawable/custom_check_box"
                        android:layout_gravity="center_horizontal"
                        android:onClick="selectItem"/>

                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:orientation="vertical">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="@string/_02"
                        android:textAlignment="center"
                        android:textSize="20sp"
                        />

                    <CheckBox
                        android:id="@+id/checkBox02"
                        android:layout_width="wrap_content"
                        android:layout_height="70dp"
                        android:layout_gravity="center_horizontal"
                        android:button="@drawable/custom_check_box"
                        android:onClick="selectItem"/>

                </LinearLayout>

            </LinearLayout>

        </LinearLayout>


----------


    </ScrollView>

</LinearLayout>

seatBooking.java文件

   
public class seatBooking extends AppCompatDialogFragment implements View.OnClickListener {

    ArrayList<String> selection = new ArrayList<String>();
    private TextView textViewTotalSeats;
    private TextView textViewSelectedSeat;
    private seatBooking.seatBookingListener listener;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater layoutInflater = getActivity().getLayoutInflater();
        final View view = layoutInflater.inflate(R.layout.activity_seat_booking,null);
        textViewTotalSeats = (TextView) view.findViewById(R.id.textViewTotalSeats);
        textViewSelectedSeat = (TextView) view.findViewById(R.id.textViewSelectedSeat);

        view.findViewById(R.id.checkBox01).setOnClickListener (this);
        view.findViewById(R.id.checkBox02).setOnClickListener (this);
        view.findViewById(R.id.checkBox03).setOnClickListener (this);
        view.findViewById(R.id.checkBox04).setOnClickListener (this);
        view.findViewById(R.id.checkBox05).setOnClickListener (this);
        view.findViewById(R.id.checkBox06).setOnClickListener (this);
        view.findViewById(R.id.checkBox07).setOnClickListener (this);
        view.findViewById(R.id.checkBox08).setOnClickListener (this);

        builder.setView(view )
                .setTitle("Seat Reservation")
                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                    }
                })

                .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {

                        String numberOfSeats = "";
                        int totalSeats = 0;

                        for (String selections : selection){

                            numberOfSeats = numberOfSeats  + selections ;
                            if(!(selection.indexOf(selections)==selection.size()-1)){
                                numberOfSeats+=",";
                            }

                        }
                        textViewSelectedSeat.setText(numberOfSeats);

                        textViewTotalSeats.setText(Integer.toString(selection.size()));
                        String seatBooking = textViewTotalSeats.getText().toString();
                        listener.applyTextSeatBooking(seatBooking);
                    }
                });

        return builder.create();
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);

        try {
            listener = (seatBooking.seatBookingListener)context;
        }catch (ClassCastException e){
            throw new ClassCastException(context.toString() + "Must implement seatBookingListener");
        }


    }

    @Override
    public void onClick(View view) {
        boolean checked = ((CheckBox) view).isChecked();

        switch (view.getId()){

            case R.id.checkBox01:
                if(checked) {
                    selection.add("01");
                }else {
                    selection.remove("01");
                }
                break;

            case R.id.checkBox02:
                if(checked) {
                    selection.add("02");
                }else {
                    selection.remove("02");
                }
                break;

            case R.id.checkBox03:
                if(checked) {
                    selection.add("03");
                }else {
                    selection.remove("03");
                }
                break;

            case R.id.checkBox04:
                if(checked) {
                    selection.add("04");
                }else {
                    selection.remove("04");
                }
                break;

            case R.id.checkBox05:
                if(checked) {
                    selection.add("05");
                }else {
                    selection.remove("05");
                }
                break;

            case R.id.checkBox06:
                if(checked) {
                    selection.add("06");
                }else {
                    selection.remove("06");
                }
                break;

            case R.id.checkBox07:
                if(checked) {
                    selection.add("07");
                }else {
                    selection.remove("07");
                }
                break;

            case R.id.checkBox08:
                if(checked) {
                    selection.add("08");
                }else {
                    selection.remove("08");
                }
                break;
        }
    }

    public interface seatBookingListener{

        void applyTextSeatBooking(String seatBooking);
    }
}

ticketBooking.java

public class ticketBooking extends AppCompatActivity implements arrive.arriveListener,seatBooking.seatBookingListener,depart.departListener,cash.cashListener {

    private TextView textViewTotalSeats,

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ticket_booking);

        textViewTotalSeats =(TextView) findViewById(R.id.textViewTotalSeats);
      
        cardViewSeats = (CardView)findViewById(R.id.cardViewSeats);

        try {
            textViewTotalSeats.setText(Integer.toString(selection.size()));
        }catch (Exception e){

        }

        cardViewSeats.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                openDialogSeatBooking();
                Total();
            }
        });

    public void openDialogSeatBooking(){
        seatBooking s = new seatBooking ();
        s.show(getSupportFragmentManager(),"Seat Reservation");
    }
    
    @Override
    public void applyTextSeatBooking(String seatBooking) {

        textViewTotalSeats.setText(seatBooking);
        Total ();
    }
}

0 个答案:

没有答案
相关问题