如何从微调器中检索值

时间:2017-12-20 12:32:23

标签: java android android-spinner

我想从spinner中检索一个值。 这是我的代码

AddCustomerAcitivity.java

public class AddCustomerActivity extends AppCompatActivity{
Spinner spinnerCustomer;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_customer);

    Toolbar toolbar = (Toolbar) findViewById(R.id.add_customer_toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setTitle("Add New Customer");

    fetchId();
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.spinner_category,
            android.R.layout.simple_spinner_item);
  adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerCustomer.setAdapter(adapter); 
}

private void fetchId() {
    spinnerCustomer = findViewById(R.id.spinner_customer);
}
}

String.xml

<string-array name="spinner_category">
    <item>Male</item>
    <item>Female</item>
</string-array>

acitivty_add_customer.xml

 <Spinner
    android:id="@+id/spinner_customer"
    android:layout_width="match_parent"
    android:layout_height="30dp"
    android:layout_margin="10dp"/>

我没有使用数据库从spinner获取值。我使用字符串数组获取值。我希望它在String中的值。

4 个答案:

答案 0 :(得分:2)

试试这个

String text = spinnerCustomer.getSelectedItem().toString();

修改

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.spinner_category,
            android.R.layout.simple_spinner_item);
  adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerCustomer.setAdapter(adapter);



spinnerCustomer.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String selectedItemText = (String) parent.getItemAtPosition(position);
                // Notify the selected item text
                Toast.makeText
                        (getApplicationContext(), "Selected : " + selectedItemText, Toast.LENGTH_SHORT)
                        .show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

答案 1 :(得分:1)

您需要实现AdapterView.OnItemSelectedListener接口 之后,您可以使用onItemSelected()方法打印您的值。

示例代码:

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
    Spinner spinner;
    String[] sampleData = {
            "first",
            "second",
            "third",
            "fourth",
            "fifth"
    };

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

        spinner.setOnItemSelectedListener(this);
        ArrayAdapter aa = new ArrayAdapter(this, android.R.layout.simple_spinner_item, sampleData);
        aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(aa);
    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        Toast.makeText(getApplicationContext(), sampleData[i], Toast.LENGTH_LONG).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }
}

答案 2 :(得分:0)

你也可以这样做。

spinnerCustomer=(Spinner)findViewById(R.id.spinner_customer);
String[] arrayOfStrings=getResources().getStringArray(R.array.spinner_category);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item,arrayOfStrings);

spinnerCustomer.setAdapter(adapter);
final Spinner finalSpinnerCustomer = spinnerCustomer;
finalspinnerCustomer.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
            String item = ((TextView) view).getText().toString();  
        Toast.makeText(this,"Clicked"+ item.toString(), Toast.LENGTH_SHORT).show();
        }
});

答案 3 :(得分:0)

您必须setOnItemSelectedListener向您的微调器获取微调器的select event。它将为您提供所选位置,您可以使用相同的位置或索引从数据源获取值。

String[] categories = getResources().getStringArray(R.array.spinner_category);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, categories);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerCustomer.setAdapter(adapter);
    spinnerCustomer.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            String selectedValue = categories[i];
            Toast.makeText(AddCustomerActivity.this, "selected value " + selectedValue, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });