2012-07-31 32 views
1

早上好夥計。 David Dimalanta,這是我。我在這裏提出一個問題。我需要創建一個程序,如果所有三個玩家都有不同的名字,那麼確認將在「完成過程」上說。但相反,即使所有人都沒有相同的名字。下面的行 - 使三個紡紗廠和玩家添加或選擇不使用相同的名稱

步驟:

  1. 每個玩家從旋轉選擇的名稱。
  2. 將值寫入字符串。
  3. 點擊按鈕後,該過程將評估相似度別名(名稱)。
  4. 如果相似,至少兩個,吐司消息將會說「請指定一個不同的用戶名」。
  5. 如果不是,那就完成了。

而且,我的活動名稱是「Player_3_at_Spinner_Menu.java」。這是我對這個類下的第一部分代碼:

//Spinners for Players 
private Spinner spinner_1; 
private Spinner spinner_2; 
private Spinner spinner_3; 

//Button to Start 
private Button play_it; 

//For Displaying Text 
private String SUMMON_PICK_UP_1, SUMMON_PICK_UP_2, SUMMON_PICK_UP_3; 
private String cplayer_1, cplayer_2, cplayer_3; 

//Text Response from a Spinner 
public final static String EXTRA_MESSAGE_1 = "com.example.databasetestvertwo.MESSAGE1"; 
public final static String EXTRA_MESSAGE_2 = "com.example.databasetestvertwo.MESSAGE2"; 
public final static String EXTRA_MESSAGE_3 = "com.example.databasetestvertwo.MESSAGE3"; 

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.players_3); 

    //Searching for ID... (Button) 
    play_it = (Button) findViewById(R.id.START_GAME); 

    //Adding listener to the button(s). 
    play_it.setOnClickListener(new trigger_happy_start()); 

    //Call from the Database_Handler.class to call the database. 
    Database_Handler db = new Database_Handler(getApplicationContext()); 

    //Then, load the content and... 
    loadSpinnerData(); 
} 




//Insert the value from the database into each of the spinners. 
private void loadSpinnerData() 
{ 

    //Initialize the spinners. 
    spinner_1 = (Spinner) findViewById(R.id.player_1_spinner); 
    spinner_2 = (Spinner) findViewById(R.id.player_2_spinner); 
    spinner_3 = (Spinner) findViewById(R.id.player_3_spinner); 

    Database_Handler db = new Database_Handler(getApplicationContext()); 

    List<String> lables = db.getAllLabels(); 

    //Creating an adapter for the spinner... 
    ArrayAdapter<String> data_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, lables); 

    data_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

    //Inserts the spinners by a database. 
    spinner_1.setAdapter(data_adapter); 
    spinner_2.setAdapter(data_adapter); 
    spinner_3.setAdapter(data_adapter); 

} 




    //Action applied if a user chose this item. (Player 1) 
public class response_1 implements OnItemSelectedListener 
{ 

    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) 
    { 

     SUMMON_PICK_UP_1 = parent.getItemAtPosition(position).toString(); 

     Toast.makeText(parent.getContext(), "You selected: " + SUMMON_PICK_UP_1, Toast.LENGTH_SHORT).show(); 

    } 

    public void onNothingSelected(AdapterView<?> arg0) 
    { 

     //Do nothing. I guess... 

    } 

} 




//Action applied if a user chose this item. (Player 2) 
public class response_2 implements OnItemSelectedListener 
{ 

    public void onItemSelected(AdapterView<?> parent_2, View view, int position, long id) 
    { 

     SUMMON_PICK_UP_2 = parent_2.getItemAtPosition(position).toString(); 

     Toast.makeText(parent_2.getContext(), "You selected: " + SUMMON_PICK_UP_2, Toast.LENGTH_SHORT).show(); 


    } 

    public void onNothingSelected(AdapterView<?> arg0) 
    { 

     // TODO Auto-generated method stub 

    } 

} 




//Action applied if a user chose this item. (Player 3) 
public class response_3 implements OnItemSelectedListener 
{ 

    public void onItemSelected(AdapterView<?> parent_3, View view, int position, long id) 
    { 

     SUMMON_PICK_UP_3 = parent_3.getItemAtPosition(position).toString(); 

     Toast.makeText(parent_3.getContext(), "You selected: " + SUMMON_PICK_UP_2, Toast.LENGTH_SHORT).show(); 


    } 

    public void onNothingSelected(AdapterView<?> arg0) 
    { 

     // TODO Auto-generated method stub 

    } 

} 

而且,這裏的檢查,如果所有的代碼,或至少兩個,球員得到了相同的名稱,過程警告他們不要使用相同的名稱下這個活動也。這裏是我的代碼:

private class trigger_happy_start implements OnClickListener 
{ 

    public void onClick(View v) 
    { 

     //Checks if the names assigned on each spinner have a the same name. 
     if 
     (
       SUMMON_PICK_UP_1 == SUMMON_PICK_UP_2 || 
       SUMMON_PICK_UP_1 == SUMMON_PICK_UP_3 || 
       SUMMON_PICK_UP_2 == SUMMON_PICK_UP_3 
     ) 
     { 
      Toast.makeText(getApplicationContext(), "Please specify a different username.", Toast.LENGTH_SHORT).show(); 
     } 
     else 
     { 
      Toast.makeText(getApplicationContext(), "Process complete, idiot.", Toast.LENGTH_SHORT).show(); 
     } 

    } 

} 

回答

1

我沒有看到您的應用程序邏輯的任何問題。

但是,你忘了設置微調的onItemSelected:

spinner1.setOnItemSelectedListener(new response_1()); 
spinner2.setOnItemSelectedListener(new response_2()); 
spinner3.setOnItemSelectedListener(new response_3()); 
+0

哦!我明白了,ariefbayu。並且,感謝您的幫助。我幾乎感到困惑,因爲我專注於** private類trigger_happy_start **下的** onClick()**方法。我從來沒有意識到我忘了爲每個微調者設置聽衆。再次感謝您的幫助。 – 2012-07-31 03:48:30