Android-多个Spinners

我有这个代码:

package lijap.app.starcraft2counters; import java.io.File; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Spinner; import android.widget.Toast; public class Settings extends Activity implements OnItemSelectedListener { Spinner answertime; Spinner gametime; Spinner missesallowed; String[] answerseconds = { "Unlimited", "1 second", "2 seconds", "3 seconds", "4 seconds", " 5 seconds" }; String[] gameminutes = { "Unlimited", "1 minute", "2 minutes", "3 minutes", "4 minutes", " 5 minutes" }; String[] numberofmisses = { "Unlimited", "5", "10", "15", "20", "25" }; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.settings); ArrayAdapter<String> adapter = new ArrayAdapter<String>(Settings.this, android.R.layout.simple_spinner_item, gameminutes); answertime = (Spinner) findViewById(R.id.s_answertime); answertime.setAdapter(adapter); answertime.setOnItemSelectedListener(this); } @Override public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } } 

我正在混淆试图投入超过1个微调。 我是否共享arrays适配器,还是为每个适配器创build一个新的? 怎么样的onItemSelected? 正如你从顶部的琴弦中看到的那样,我需要三个旋转器。 我将如何做到这一点?

我是新来的android,你可以告诉。 所有的帮助表示赞赏 – Lijap

如果适配器使用相同的信息,您可以在不同的Spinner器之间共享适配器。 显然,每个适配器都需要调整一组不同的String ,所以您需要为每个Spinner创build一个ArrayAdapter

一个OnItemSelectedListener将为3个Spinners (只要你设置它们)。 您可以调用作为parameter passing的AdapterView<?>上的getId() ,以知道哪个Spinner引发了该事件。

 public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) { switch(arg0.getId()){ case R.id.s_answertime: break; case ... } } 

检查这一个

 yearDropdown.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) { selectedyear = parentView.getSelectedItemPosition(); drpyear = yearDropdown.getSelectedItem().toString(); //your code here } public void onNothingSelected(AdapterView<?> parentView) { selectedyear = 0; //return; } }); monthDropdown.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) { //your code here } public void onNothingSelected(AdapterView<?> parentView) { selectedmonth = 0; //return; } }); dayDropdown.setOnItemSelectedListener(new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) { //your code here } public void onNothingSelected(AdapterView<?> parentView) { selectedday = 0; } }); 

您需要为每个微调器创build每个适配器。

  adapterYear = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item); yearDropdown.setAdapter(adapterYear); adapterMonth = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item); monthDropdown.setAdapter(adapterMonth); adapterDays = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item); dayDropdown.setAdapter(adapterDays);