简体   繁体   中英

I have a textview that gets its text from a selection of options within a dialogbox. How can I get the selections to also show in another layout?

I am trying to get the textviews from Screen1 to display as the textviews in Screen2. The textViews in Screen1 are set based on the user's selection of checkbox items presented to them in the form of a dialog box. The dialog box displays when a button is clicked (in this case the buttons are called List1, and List2). Within the dialog box the user is presented with three items, they choose the items they want, select "Add Choices", and their selections are displayed under the button.

When the user clicks the "Next" button, I would like for their selections to be displayed in the next layout (Screen2).

The choices presented to the user are described within two ArrayLists (one array of choices for each button) contained with the res-Strings file.

To be honest it is hard to recap on the various things I have tried, as there have been many. I have been working on it for several days trying various methodologies described for sending text between layouts however, none of them have worked for me in this particular situation.

I am new to android development, so any and all suggestions are helpful here.

I did not include Screen2 in the code, Screen2 is only comprised of two empty text views and a back button to get back to Screen1.

//Screen1

    import android.content.DialogInterface;
    import android.content.Intent;
    import android.support.v7.app.AlertDialog;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    import java.util.ArrayList;

    public class Screen1 extends AppCompatActivity {
    //Fields for List 1
    Button List1;
    TextView Text_View_List1;
    String[] Item_Choices;
    boolean[] Selected_Choices;
    ArrayList<Integer> List1_Items = new ArrayList<>();

    //Fields for List 2
    Button List2;
    TextView Text_View_List2;
    String[] Item_Choices2;
    boolean[] Selected_Choices2;
    ArrayList<Integer> List2_Items = new ArrayList<>();

    Button Next;

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

        List1 = findViewById(R.id.Button1);
        Text_View_List1 = findViewById(R.id.Text_View_List1);
        Item_Choices = getResources().getStringArray(R.array.List1_Items);
        Selected_Choices = new boolean[Item_Choices.length];


        //Show a dialog box when List1 button is clicked containing items      from ArrayList defined in res-strings

        List1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(Screen1.this);
                builder.setTitle("Options to choose from");
                builder.setMultiChoiceItems(Item_Choices, Selected_Choices, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int position, boolean isChecked) {
                        if (isChecked){
                            if (!List1_Items.contains(position)){
                                List1_Items.add(position);
                            }
                        }
                        if (!isChecked){
                            if (List1_Items.contains(position)){
                                List1_Items.remove((Integer) position);
                            }
                        }
                    }
                });
                builder.setCancelable(false);

                //Selected choices are added to List2's Text view
                builder.setPositiveButton("Add Choices", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String item1 = "";
                        for (int i = 0; i < List1_Items.size(); i ++){
                            item1 = item1 + Item_Choices[List1_Items.get(i)];
                            if ( i != List1_Items.size() -1){//if the selected choice is NOT last, add a comma
                                item1 = item1 + "," + System.lineSeparator();//Displays chosen items stacked on top of each other rather than side by side (to save screen space)
                            }
                        }
                        Text_View_List1.setText(item1);
                    }

                });
                //Closes out the dialog box
                builder.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                //clear selections from List2's TextView
                builder.setNeutralButton("Clear Selections", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        for (int i = 0; i < Selected_Choices.length; i ++){
                            Selected_Choices[i] = false;
                            List1_Items.clear();
                            Text_View_List1.setText("");
                        }
                    }
                });
                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });

        //Same set up as List1, but uses data from the second ArrayList described in res-strings folder


        List2 = findViewById(R.id.Button2);
        Text_View_List2 = findViewById(R.id.Text_View_List2);

        //Set up the arrays
        Item_Choices2 = getResources().getStringArray(R.array.List2_Items);
        Selected_Choices2 = new boolean[Item_Choices2.length];

        //Show a dialog box when List2 button is clicked containing items from ArrayList defined in res-strings

        List2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(Screen1.this);
                builder.setTitle("Options to Choose from");
                builder.setMultiChoiceItems(Item_Choices2, Selected_Choices2, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int position, boolean isChecked) {
                        if (isChecked) {
                            if (!List2_Items.contains(position)) {
                                List2_Items.add(position);
                            }
                            if (!isChecked) {
                                if (List2_Items.contains(position)) {
                                    List2_Items.remove((Integer) position);
                                }
                            }
                        }

                    }
                });
                builder.setCancelable(false);
                //Selected choices are added to List2's Text view

                builder.setPositiveButton("Add Choices", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String item2 = "";
                        for (int i = 0; i < List2_Items.size(); i++) {
                            item2 = item2 + Item_Choices2[List2_Items.get(i)];
                            if (i != List2_Items.size() - 1) { //if the selected choice is NOT last, add a comma
                                item2 = item2 + "," + System.lineSeparator(); //Displays chosen items stacked on top of each other rather than side by side (to save screen space)

                            }
                        }
                        Text_View_List2.setText(item2);
                    }

                });
                //Closes out the dialog box

                builder.setNeutralButton("Dismiss", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                //clear selections from List2's TextView

                builder.setNegativeButton("Clear Selections", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        for (int i = 0; i < Selected_Choices2.length; i++) {
                            Selected_Choices2[i] = false;
                            List2_Items.clear();
                            Text_View_List2.setText("");
                        }
                    }
                });

                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });
        Next = findViewById(R.id.Next);
        //goes to screen2
        Next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent nextLayout = new Intent(Screen1.this, Screen2.class);
                startActivity(nextLayout);
            }
        });
}
    }

    //XML for Screen1
    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    tools:context=".Screen1">

    <Button
        android:id="@+id/Button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:text="List1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.188"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.343" />

    <Button
        android:id="@+id/Button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:text="List2"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.726"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.343" />

    <TextView
        android:id="@+id/Text_View_List1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.193"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.445" />

    <TextView
        android:id="@+id/Text_View_List2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.66"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.446" />

    <Button
        android:id="@+id/Next"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="Next"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />
</android.support.constraint.ConstraintLayout>


    //Res-Layout(the arrays)
    <resources>
    <string name="app_name">TestApp</string>

    <string-array name="List1_Items">
        <item>Choice 1</item>
        <item>Choice 2</item>
        <item>Choice 3</item>
    </string-array>

    <string-array name="List2_Items">
        <item>Choice x</item>
        <item>Choice y</item>
        <item>Choice z</item>
    </string-array>



</resources>

Create a String variable to store the selected option from Dialog. Then pass the variable in SecondActivity

Next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent nextLayout = new Intent(Screen1.this, Screen2.class);
                nextLayout.putExtra("position", value);
                startActivity(nextLayout);
            }
        });

And get the value in second activity

String value = getIntent().getExtras().getString("position");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM