Я создал фрагмент диалога с флажками.Он может показать общее количество выбранных элементов в текстовом представлении в основной деятельности.Но когда я снова открываю это диалоговое окно, в нем не отображаются флажки, которые я выбрал ранее.Я хочу показать ранее выбранные флажки при повторном открытии диалога.Я не могу понять, как это сделать.Я пытался так много раз, но я застрял.Пожалуйста, помогите мне решить эту проблему.
Файл XML Layout
<?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 ();
}
}