I have an activity that contains two spinners and implements OnItemSelectedListener. To find out what item was selected I use a switch. When an items gets selected I have a simple toast come up to tell the user what was selected. The problem is when the activity is created a toast comes up and says what item was selected. I only want the toast to show when a user selects something besides the default item, the one that is shown when the activity is created. Here is my code below

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    SpinnerAdapterTexts sat = new SpinnerAdapterTexts();

    final Spinner spin = (Spinner)findViewById(R.id.spinner);
    ArrayAdapter<String> arrayAd = new ArrayAdapter<String>(this,R.layout.row,R.id.type,sat.getType());
    spin.setAdapter(arrayAd);
    spin.setOnItemSelectedListener(this);

    final Spinner spinService = (Spinner)findViewById(R.id.spinnerService);
    ArrayAdapter<String> arrayAd2 = new ArrayAdapter<String>(this,R.layout.row,R.id.type, sat.getServices());
    spinService.setAdapter(arrayAd2);
    spinService.setOnItemSelectedListener(this);

}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos ,
        long id) {
    // TODO Auto-generated method stub@

    switch(parent.getId()) {
        case R.id.spinner:
            selectedType = parent.getItemAtPosition(pos).toString();

            Toast.makeText(getApplicationContext(),  selectedType + " was selected", Toast.LENGTH_LONG).show();

            break;
        case R.id.spinnerService:
            selectedType = parent.getItemAtPosition(pos).toString();

            Toast.makeText(getApplicationContext(),  selectedType + " was selected", Toast.LENGTH_LONG).show();
            break;
    }

    selectedType = parent.getItemAtPosition(pos).toString();
    Log.e("TAG",  selectedType);

}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}
有帮助吗?

解决方案

then do this

int count;
protected void onCreate(){
    count=0;
    //your codes
}
//then before toast
if(count==0){
    count=1;
    Toast(the user selection);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top