2012-07-31 34 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