开发者

Why does the Spinner not contain my items?

Hi i now looked at several examples of how to use the spinner. But it does not matter, whatever i try the spinner in the emulator does not show its conent... This is the code I use:

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.onCre开发者_StackOverflow中文版ate(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);
}
}

I tought this code should add the items one, two and three in the spinner control but the control remains empty in the emulator...

Any ideas what I may did wrong?

Thanks!


Not sure what you did wrong, as I am not all that experienced with this platform.

BUT

I just implemented a Spinner, and this is the code that I used - might be useful.

    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();

And this is what I put into the strings.xml file (I prefer keeping lists like this outside the code).

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

Then, to use the data in the spinner, for example when the 'Next' buton is clicked

    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();
        }
    });

It works for me, hope it is useful for you

Cheers,

Oliver


Solved my problem. I missunderstood the loading of a new activity. Everything works after i changed the method to display the new view to

Intent intent = new Intent(this, NewBooking.class);
startActivity(intent);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜