2014-12-07 4 views
0

다음 요소를 사용하여 완료해야하는 매우 구체적인 작업이 있습니다. 로그인 페이지를 개발해야하는데, 그 페이지에서 내가 한 드롭 다운 상자 (회 전자)를 만들어야합니다. 스피너에는 4 개의 이름이 포함되어 있습니다. 그러나 드롭 박스에서 이름을 선택하면 사용자는 패스 코드 (4 자리 숫자)를 입력해야합니다. 이것은 까다로워지기 때문에 도움이 필요합니다. 배열을 사용하여 스피너의 이름을 각 사용자와 관련된 4 개의 하드 코딩 된 패스 코드 세트와 연결해야합니다. user1 패스 코드는 1234이며 다른 패스 코드는 유효하지 않습니다. 이 특정 주제와 관련하여 도움을 요청했으며 일부 사람들은 다른 방법을 제안했지만이 페이지에는 병렬 배열을 사용해야한다는 점을 강조해야합니다. 사용하려고 시도한 일부 코드가 포함됩니다. 내 질문은 배열을 스피너에 연결하여 선택한 사용자가 특정 패스 코드를 입력해야하는 방법입니다. 현재 사용자가 특정 패스 코드를 입력 할 때 연결할 수 없습니다.배열 및 스피너

 my spinner contains four names// spinner=(Spinner) findViewById(R.id.names); 
    ArrayAdapter adapter=ArrayAdapter.createFromResource(this, 
    R.array.names,android.R.layout.simple_spinner_item); 
    spinner.setAdapter(adapter); 
    spinner.setOnItemSelectedListener(this); 

my passcode login //public void onTextChanged(CharSequence s, int start, int before, 
    int count) 
    { 
    if (passcodeEntered.getText().toString().length() == 4) 
    { 
    if ((passcodeEntered.getText().toString().equalsIgnoreCase("1234"))) 
    { 
     Intent myIntent = new Intent(MainActivity.this, Summary.class); 
     startActivity(myIntent); 

    } else 
    { 
     Toast.makeText(getBaseContext(), "Invalid Passcode",   

     Toast.LENGTH_SHORT).show(); 

    finally here is some code I was trying to use in my arrays// 

    public void passCodes(){ 

    String[] names = {"user1", "user2", "user3", "user4"}; 
    String [] passcodes = {"1234", "4321", "5678", "8765"}; 
    int passcode = keyboard.nextInt(); 

    for (int i = 0; i < names.length; i++) 
    { 



    if (passcodes[i]==passcode){ 
     System.out.println("Go to next page"); 
     System.out.println(names[i]); 
    } 

    } 
+0

스택 오버플로는 프로그래밍 관련 질문입니다. 귀하의 질문은 무엇인가? – CommonsWare

+0

무엇이 문제입니까? –

+0

나는 그것을 어떻게 작동시키는 지 모른다. 나는 패스 코드와 일치하는 이름을 알 수 없다. – ExarchTemplar

답변

0

당신은 setOnItemSelectedListener

spinner.setOnItemSelectedListener를 오버라이드 (override) 할 필요 (새 OnItemSelectedListener() {

@Override 
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 
{ 
    //here check for the position, this will tell you which item in the dropdown was selected by the user. You can use this position as index to get the passcode from the array and match it with the passcode entered by user here and accordingly either call another activity or throw Toast message to user. 


} 

});

스피너 처리에 대한 예제는 this blog을 참조하십시오.