1

早安,伙计们。这又是我,大卫·迪马兰塔。我是来问问题的。我需要创建一个程序,如果所有三个玩家的名字都不同,确认将在 toast 上显示“过程完成”。但相反,甚至所有人都没有相同的名字。

脚步:

  1. 每个玩家从旋转器中选择一个名字。
  2. 该值被写入字符串。
  3. 单击按钮时,该过程会评估相似性别名(名称)。
  4. 如果相似,至少有两个,toast 消息将显示“请指定不同的用户名”。
  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();
        }

    }

}
4

1 回答 1

1

我认为您的应用程序逻辑没有任何问题。

但是,您忘记设置 Spinner 的 onItemSelected:

spinner1.setOnItemSelectedListener( new response_1());
spinner2.setOnItemSelectedListener( new response_2());
spinner3.setOnItemSelectedListener( new response_3());
于 2012-07-31T03:38:32.460 回答