未显示选定的Spinner数据

时间:2017-03-30 11:55:27

标签: java android android-spinner

我已经实现了Spinner,从下拉列表中选择值时,从服务器获取微调器的数据它没有被选中

    timeSpinner=(Spinner)findViewById(R.id.snipper2);
        timeSpinner.setAdapter(new ArrayAdapter<String>(singleDay.this,android.R.layout.simple_spinner_dropdown_item,dateArrayList));
        timeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(singleDay.this, "i am working...", Toast.LENGTH_SHORT).show();
                Toast.makeText(singleDay.this, ""+adapterView.getSelectedItem(), Toast.LENGTH_SHORT).show();
String s=timeSpinner.getSelectedItem().toString();
                textView.setText(s);

            }


    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {
        Toast.makeText(singleDay.this, "i am working...", Toast.LENGTH_SHORT).show();
    }

Xml代码:

<Spinner
                android:id="@+id/snipper2"
                android:layout_row="1"
                android:background="@drawable/layout_bg"
                android:layout_column="1"
                android:layout_columnSpan="2"
                android:textColorHint="#05ab9a"
                android:prompt="@string/spinnertitle"
                android:layout_width="129dp"
                android:layout_height="39dp"

                />

下面的截图:

After select nothing sleeted

1 个答案:

答案 0 :(得分:1)

更改spinner heightwidth。它似乎太小而无法显示数据。

将您的代码更改为此

ArrayAdapter<String> dataAdapter  = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,dateArrayList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

timeSpinner.setAdapter(dataAdapter);

您可以使用getItemAtPosition()方法获取所选项目:

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
final String itemSelected = parent.getItemAtPosition(position).toString();
Log.i("Item Selected: ", itemSelected);
}