如何依赖于彼此的阵列列表

时间:2021-05-10 13:43:11

I'm trying to create a login form where you select an item from a spinner and then you need to put in the right code in a text field to be able to proceed. This is what I got.

我正在尝试创建一个登录表单,您可以在其中从微调器中选择一个项目,然后您需要在文本字段中输入正确的代码才能继续。这就是我得到的。

Spinner schoolList = findViewById(R.id.schoolList);

    ArrayAdapter<String> schoolListAdapter = new ArrayAdapter<String>(CreateAccount.this, android.R.layout.simple_spinner_dropdown_item, schools);

    schoolList.setAdapter(schoolListAdapter);
private String[] schools = new String[]{
        "School 1",
        "School 2,
        "School 3"
};


private String[] schoolCodeArray = new String[] {
        "password1",
        "password2",
        "password3"
};

I know that you could check to see if School 1 = password1 and so on but this list will be very long and I'm therefore looking for a better way. Something like taking the index of the selected school and checking if that index is equal to the index from the schoolCodeArray and matches what is put in the text field. I hope that you understand what I'm looking for. I appreciate all help!

我知道你可以查看一下学校1 =密码1等等,但是这个列表会很长,因此我正在寻找更好的方法。类似于获取所选学校的索引并检查该索引是否等于来自schoolCodeArray的索引并匹配文本字段中的索引。我希望你明白我在寻找什么。我感谢所有的帮助!

2 个解决方案

#1


0  

I suppose you have initialized the EditText by findViewById(), so try this and put your code inside onItemSelected:

我想你已经通过findViewById()初始化了EditText,所以试试这个并把你的代码放在onItemSelected中:

    String[] schools = new String[]{
            "School 1",
            "School 2",
            "School 3"
    };


    final String[] schoolCodeArray = new String[] {
            "password1",
            "password2",
            "password3"
    };

    Spinner schoolList = findViewById(R.id.schoolList);
    ArrayAdapter<String> schoolListAdapter = new ArrayAdapter<String>(CreateAccount.this, android.R.layout.simple_spinner_dropdown_item, schools);
    schoolList.setAdapter(schoolListAdapter);

    schoolList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            if (schoolCodeArray[position].equals(editText.getText().toString().trim())) {
                // your code here
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

#2


1  

If you supposed to have two spinner one for school and other one for password, then consider ordering of items may different as well. Make your logic flexible. To consider above create a HashMap where consider school as a key and password as a value.

如果您应该有两个微调器用于学校而另一个用于密码,那么考虑项目的顺序也可能不同。使您的逻辑灵活。要考虑上述情况,请创建一个HashMap,将school视为密钥,将密码视为值。

HashMap<String, String> credentialMap = new HashMap<String, String>();
credentialMap.put("School 1","Password 1")
credentialMap.put("School 2","Password 2")

Once user selects item from both of the spinner then on some button action you need to validate it. To do that

一旦用户从两个微调器中选择项目,然后在某个按钮操作上,您需要验证它。要做到这一点

    loginButton.setOnClickListener(new View.OnClickListener(){
         public void onClick(View v){
            if(credentialMap.containsKey(schoolSpinnerItem)){
              if(credentialMap.get(schoolSpinnerItem).equals(passwordSpinnerItem)){
                // Login success
              }
              else{
                 // Login failed
              }
            }
         }
     });

#1


0  

I suppose you have initialized the EditText by findViewById(), so try this and put your code inside onItemSelected:

我想你已经通过findViewById()初始化了EditText,所以试试这个并把你的代码放在onItemSelected中:

    String[] schools = new String[]{
            "School 1",
            "School 2",
            "School 3"
    };


    final String[] schoolCodeArray = new String[] {
            "password1",
            "password2",
            "password3"
    };

    Spinner schoolList = findViewById(R.id.schoolList);
    ArrayAdapter<String> schoolListAdapter = new ArrayAdapter<String>(CreateAccount.this, android.R.layout.simple_spinner_dropdown_item, schools);
    schoolList.setAdapter(schoolListAdapter);

    schoolList.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            if (schoolCodeArray[position].equals(editText.getText().toString().trim())) {
                // your code here
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

#2


1  

If you supposed to have two spinner one for school and other one for password, then consider ordering of items may different as well. Make your logic flexible. To consider above create a HashMap where consider school as a key and password as a value.

如果您应该有两个微调器用于学校而另一个用于密码,那么考虑项目的顺序也可能不同。使您的逻辑灵活。要考虑上述情况,请创建一个HashMap,将school视为密钥,将密码视为值。

HashMap<String, String> credentialMap = new HashMap<String, String>();
credentialMap.put("School 1","Password 1")
credentialMap.put("School 2","Password 2")

Once user selects item from both of the spinner then on some button action you need to validate it. To do that

一旦用户从两个微调器中选择项目,然后在某个按钮操作上,您需要验证它。要做到这一点

    loginButton.setOnClickListener(new View.OnClickListener(){
         public void onClick(View v){
            if(credentialMap.containsKey(schoolSpinnerItem)){
              if(credentialMap.get(schoolSpinnerItem).equals(passwordSpinnerItem)){
                // Login success
              }
              else{
                 // Login failed
              }
            }
         }
     });