为什么Spinner不包含我的物品?

时间:2011-04-24 10:43:25

标签: android

您好 我现在看几个如何使用微调器的例子。但无论如何,无论我在模拟器中尝试微调器,都不会显示其内容...... 这是我使用的代码:

public class NewBooking extends Activity {

private static List<String> l = new ArrayList<String>();

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.newbooking);
    String[] items = new String[] {"One", "Two", "Three"};
    Spinner spinner = (Spinner) findViewById(R.id.cmbNewBookingType);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_spinner_item, items);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
}
}

我认为这段代码应该在微调控件中添加第一,第二和第三项,但控件在模拟器中保持为空......

任何想法我可能做错了什么?

谢谢!

2 个答案:

答案 0 :(得分:0)

不确定你做错了什么,因为我对这个平台并不是很有经验。

BUT

我刚刚实现了一个Spinner,这是我使用的代码 - 可能有用。

    final Spinner spnrCategory = (Spinner) this.findViewById(R.id.spinnerCategory);
    // The Spinner does not generate an interrupt for OnClick ...
    // We use an adapter to set and access data in the Spinner
    // The data to populate the Spinner is in the Strings resource file
    // We have to define what the spinner looks like when closed and also when open!!!
    ArrayAdapter<?> spinnerAdapter = ArrayAdapter.createFromResource(this,R.array.Categories, android.R.layout.simple_spinner_item);
    spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spnrCategory.setAdapter(spinnerAdapter);
    spnrCategory.setSelection(0);  // Default to the first category
    strCategoryType = spnrCategory.getSelectedItem().toString();

这就是我在strings.xml文件中添加的内容(我更喜欢在代码之外保留这样的列表)。

<string-array name="Categories">        
    <item>LIVE: Gardening</item>
    <item>LIVE: Allotments</item>
    <item>Other</item>
</string-array>

然后,使用微调器中的数据,例如当点击“下一个”按钮时

    Button btnNext = (Button) this.findViewById(R.id.btnNext);
    btnNext.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            strCategoryType = spnrCategory.getSelectedItem().toString();
            Intent intentNextActivity;
            intentNextActivity = new Intent(SelCategoryActivity.this, ShowVideoActivity.class);             
            startActivity(intentNextActivity);
            SelCategoryActivity.this.finish();
        }
    });

它对我有用,希望它对你有用

干杯,

奥利弗

答案 1 :(得分:0)

解决我的问题。我误解了新活动的加载。在我改变方法以显示新视图

之后,一切正常
Intent intent = new Intent(this, NewBooking.class);
startActivity(intent);
相关问题