Android: two Spinner onItemSelected()

后端 未结 6 1070
死守一世寂寞
死守一世寂寞 2020-12-01 07:23

I have two spinners (day_spin and time_spin) in one Activity. I want to save the selected day_spin value into a variable. Is it possible to differenciate betwee

6条回答
  •  借酒劲吻你
    2020-12-01 08:09

    Two Spinner in same class

    I try a lot of this but at the last, I got a working Code.

        state = findViewById(R.id.spinnerState);
        country = findViewById(R.id.spinnerCountry);
    
        List categories = new ArrayList();
        categories.add("Select State");
        categories.add("Andhra Pradesh");
        categories.add("Arunachal Pradesh");
        categories.add("Assam");
        categories.add("Bihar");
        categories.add("Chandigarh");
        categories.add("Delhi");
        categories.add("Goa");
        categories.add("Gujarat");
        categories.add("Haryana");
        categories.add("Himachal Pradesh");
        categories.add("Jammu and Kashmir union territory");
        ArrayAdapter dataAdapter = new ArrayAdapter(SaveUser.this, android.R.layout.simple_spinner_item, categories);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        state.setAdapter(dataAdapter);
        state.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView parent, View view, int position, long id) {
                String item = parent.getItemAtPosition(position).toString();
                Toast.makeText(getApplicationContext(), item, Toast.LENGTH_LONG).show();
            }
    
            @Override
            public void onNothingSelected(AdapterView parent) {
                // can leave this empty
            }
        });
        List categories2 = new ArrayList();
        categories2.add("Select Country");
        categories2.add("Afghanistan");
        categories2.add("Albania");
        categories2.add("Algeria");
        categories2.add("India");
        categories2.add("Andorra");
        categories2.add("Angola");
        categories2.add("Antigua and Barbuda");
        categories2.add("Argentina");
        categories2.add("Armenia");
        // Div Spinner implementing onItemSelectedListener
        ArrayAdapter dataAdapter2 = new ArrayAdapter(SaveUser.this, android.R.layout.simple_spinner_item, categories2);
        dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        country.setAdapter(dataAdapter2);
        country.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView parent, View view, int position, long id) {
    
                String item = parent.getItemAtPosition(position).toString();
                Toast.makeText(getApplicationContext(), item, Toast.LENGTH_LONG).show();
            }
    
            @Override
            public void onNothingSelected(AdapterView parent) {
                // can leave this empty
            }
    
        });
    
    
    }
    

提交回复
热议问题