简体   繁体   English

在不同的 Activity 中显示列表视图

[英]Show a list view in a different Activity

Hey I was wondering if this is possible.嘿,我想知道这是否可能。 I'm working on a app that takes user input and creates flashcards.我正在开发一个接受用户输入并创建抽认卡的应用程序。 When I click the red button I want it to open a new activity displaying the input as a list.当我单击红色按钮时,我希望它打开一个将输入显示为列表的新活动。 I'm not sure how to get the second activity to read the Edittext and put it into a list display.我不确定如何让第二个活动读取 Edittext 并将其放入列表显示中。

When you press the green button, a prompt takes user input and post it to the page.当您按下绿色按钮时,提示会接受用户输入并将其发布到页面。 When you click the red button it will take you to a list view of what the user entered.当您单击红色按钮时,它将带您进入用户输入内容的列表视图。 I'm a bit stumped on how to pass the information to the next page to display a list.我对如何将信息传递到下一页以显示列表感到有点困惑。

enter code here package Main Code ;

TextView QuestionTV;
TextView AnswerTV;

//Buttons of Main Flashcard xml
Button addcardbtn;
Button Answerbtn;
Button nextbtn;

//Input and Buttons from dialog_Flashcard xml
EditText QuestionET;
EditText AnswerET;
Button Submitcardbtn;
Button cancelbtn;
Button deletecard;



//counter for array
int count = 0;

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

    addcardbtn = findViewById(R.id.addcardbtn);
    Answerbtn = findViewById(R.id.Answerbtn);
    nextbtn = findViewById(R.id.nextbtn);
    deletecard = findViewById(R.id.deletecard);

    //new array
    QuestionArray = new ArrayList<>();
    adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, QuestionArray);
    adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, QuestionArray);

    AnswerArray = new ArrayList<>();
    adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_list_item_1, AnswerArray);
    adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, AnswerArray);

    //Text views
    QuestionTV = findViewById(R.id.QuestionTV);
    AnswerTV = findViewById(R.id.answerTV);

    deletecard.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            startActivity(new Intent(getApplication(), FlashCard_ListView.class));
        }
    });

    addcardbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final AlertDialog.Builder flashbuilder = new AlertDialog.Builder(FlashCards.this);
            //get layout for the alert box
            View fView = getLayoutInflater().inflate(R.layout.dialog_flashcard, null);

            //get EditText from the alertbox layout
            QuestionET = fView.findViewById(R.id.QuestionET);
            AnswerET = fView.findViewById(R.id.AnswerET);

            //get buttons from the alertbox layout
            Submitcardbtn = (Button) fView.findViewById(R.id.Submitcardbtn);
            cancelbtn = fView.findViewById(R.id.cancelbtn);


            //make dialog box pop up
            flashbuilder.setView(fView);
            final AlertDialog dialog = flashbuilder.create();
            dialog.show();

            //code for the submit button
            Submitcardbtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   if(!QuestionET.getText().toString().isEmpty() && !AnswerET.getText().toString().isEmpty()){
                       QuestionArray.add(QuestionET.getText().toString());
                       AnswerArray.add(AnswerET.getText().toString());
                       Toast.makeText(FlashCards.this, "Card Created", Toast.LENGTH_SHORT).show();
                       dialog.dismiss();
                   }
                }
            });

            //code for cancel button
            cancelbtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                }
            });
            nextbtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   QuestionTV.setText(QuestionArray.get(count));
                   AnswerTV.setText(AnswerArray.get(count));
                    if (count<QuestionArray.size()-1 || count<AnswerArray.size()-1){
                        count++;
                        AnswerTV.setVisibility(View.INVISIBLE);
                   }else if(count == QuestionArray.size()-1){
                        count = 0;
                        AnswerTV.setVisibility(View.INVISIBLE);
                    }
                }
            });
            Answerbtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    AnswerTV.setVisibility(View.VISIBLE);
                }
            });
        }


    });
}

} }

First Activity code
ArrayList<String> QuestionArray = new ArrayList<>();
                    QuestionArray.add(QuestionET.getText().toString());

                    ArrayList<String> AnswerArray = new ArrayList<>();
                    AnswerArray.add(AnswerET.getText().toString());

                    Intent i = new Intent(FlashCards.this, FlashCard_ListView.class);

                    i.putExtra("Questions", QuestionArray);
                    i.putExtra("Answers", AnswerArray);

                    startActivity(i);

Second Activity Code

Intent i = getIntent();

    ArrayList<String> QuestionArray = i.getStringArrayListExtra("Questions");
    adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, QuestionArray);
    QuestionListView.setAdapter(adapter);

    ArrayList<String> AnswerArray = i.getStringArrayListExtra("Answers");
    adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, AnswerArray);
    AnswersListView.setAdapter(adapter);

I Figured It out thanks!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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