【问题标题】:How to have to array list depending on each other如何必须根据彼此排列列表
【发布时间】:2018-08-04 16:40:04
【问题描述】:

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

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"
};

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

【问题讨论】:

  • 创建一个将学校和代码结合在一起的类;然后创建该类的单个数组。
  • 为什么不使用 Array of Array? String[][] = new String[]{ new String[] {"School1", "Password1"}, new String[] {"School 2", "Password2" } };

标签: java android indexing spinner


【解决方案1】:

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

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

一旦用户从两个微调器中选择项目,然后在某些按钮操作上,您需要对其进行验证。这样做

    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
              }
            }
         }
     });

【讨论】:

    【解决方案2】:

    我想你已经用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) {
    
            }
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-19
      相关资源
      最近更新 更多