Android:How 要设置 Spinner 的默认值?

Android:How To set set deafult value of Spinner?

我正在创建一个下拉列表(微调器),其中必须显示所有日期,但我想设置不同的默认值 value.Presently 默认值和微调器的第一个值相同..请帮助我如何做到这一点...赞赏代码片段.... 我的是 =>

public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener {
Spinner spinner;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    spinner =(Spinner) findViewById(R.id.spinner1);
    ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
            R.array.days, android.R.layout.simple_spinner_item);
   spinner.setAdapter(adapter);
   spinner.setOnItemSelectedListener(this);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {
    TextView tv =(TextView)arg1;
    Toast.makeText(this, "YOu have selected"+tv.getText(), Toast.LENGTH_SHORT).show();

}

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

}

 }

Activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" />

</RelativeLayout>

simple_spinner_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

</LinearLayout>

String.xml

'

<string name="app_name">Spinner</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string-array name="days">
    <item>Choose A day</item>
    <item>Sunday</item>
    <item>Monday</item>
    <item>Tuesday</item>
    <item>Wednesday</item>
    <item>Thrusday</item>
    <item>Friday</item>
    <item>Saturday</item>
</string-array>

'

您可以使用以下函数:setSelection(Integer)

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    spinner =(Spinner) findViewById(R.id.spinner1);
    ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
        R.array.days, android.R.layout.simple_spinner_item);
    spinner.setAdapter(adapter);
    spinner.setOnItemSelectedListener(this);
    spinner.setSelection(positionSpinner);
}

编辑: 我认为唯一的解决方案是创建自己的微调器适配器,但将文本作为最后一项并使用 getCount 以便不显示它。

class MySpinnerAdapter extends ArrayAdapter<String> {
    public MySpinnerAdapter(Context context, int resource, List<String> objects) {
        super(context, resource, objects);
    }

    @Override
    public int getCount() {
        return super.getCount() - 1; // This makes not show the last item
    }

    @Override
    public SpinnerItem getItem(int position) {
        return super.getItem(position);
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

}

最后:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    spinner =(Spinner) findViewById(R.id.spinner1);
    ArrayList itemsSpinner = new ArrayList(); 
    itemsSpinner.add("Day 1"); 
    itemsSpinner.add("Day 2"); 
    itemsSpinner.add("Choose a day"); // Last item

    MySpinnerAdapter adapterSpinner = new MySpinnerAdapter(this, android.R.layout.simple_spinner_item, itemsSpinner );
    adapterSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapterSpinner);
    spinner.setSelection(items.size() - 1);
}

提示 定义你的 R.array.days 作为最后一个元素。

<string-array name="days">
  <item>Sunday</item>
  <item>Monday</item>
  <item>Tuesday</item>
  <item>Wednesday</item>
  <item>Thursday</item>
  <item>Friday</item>
  <item>Saturday</item>
  <item>Choose a Day</item> <!-- Hint -->
</string-array>

现在,实例化您的 ArrayAdapter 并将其设置为

spinner = (Spinner) findViewById(R.id.spinner1);
String[] dayValues = getResources().getStringArray(R.array.days);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
   android.R.layout.simple_spinner_item, dayValues) {

    @Override
    public int getCount() {
         int c = super.getCount();
         if (spinner.getSelectedItemPosition() < c - 1) return c;
         return c > 0 ? c - 1 : c;
    }
};
spinner.setAdapter(adapter);

最后,将您的提示设置为默认微调器值

spinner.setSelection(dayValues.length - 1);

我开发了一个小组件来实现它。只需在 AutoCompleteTexview 中添加提示文本并使用它构建 CompleteSpinner。

https://github.com/FernandoGaliay/CompleteSpinner