如何从“编辑文本”字段中将值添加到预先存在的字符串列表中?

问题描述 投票:0回答:2

我有一个应用程序,我试图让用户在注册帐户时回答一系列问题。然后,他们将被问到这些问题,作为记忆测试的一种形式。我已经实现了一个开放式问题功能,但问题的答案是硬编码的。我想在“answers”字符串中获取用户在注册期间输入的值。

问答数据库代码:

package com.example.memory;

public class QuestionDatabase {

    public static String[] questions = {
            "What time did you wake up?",
            "What do you do first when you wake up?",
            "What did you have for breakfast?",
            "What time did you go to bed last night?",
            "What is your favorite colour?",
            "What is your favorite movie?",
            "What is your favorite food?",
    };

    public static String[] answers = {

            "9",
            "Bathroom",
            "Coffee",
            "11",
            "Blue",
            "Avengers",
            "Pizza",
    };

}

以下是问题所在的代码:

package com.example.memory;

public class Q1 extends AppCompatActivity {

    Button save1;
    EditText ans1;
    String answer1;

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

        save1 = (Button) findViewById(R.id.save1);
        ans1 = (EditText) findViewById(R.id.ans1);
        answer1 = ans1.getText().toString();

        save1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                save1();
            }
        });


    }

    public void save1() {
        Intent intent = new Intent(this, LoginActivity.class);
        intent.putExtra("", ans1.getText().toString());
        startActivity(intent);
    }
}

如何将“ans1”中输入的值输入QuestionDatabase的String [] answers部分。

android android-intent android-edittext
2个回答
2
投票

最好使用字符串的ArrayList而不是String Array

ArrayList<String> stringList = new ArrayList<>();
stringList.add("you String");

0
投票

使用HashSet for Strings而不是String数组。这样,您就可以轻松添加字符串,并且答案数据结构中不会出现重复的字符串。

HashSet<String> answers = new HashSet<String>();  
set.add("input string); 
热门问题
推荐问题
最新问题