Dec 2, 2011

Apply List to Spinner

Apply List to Spinner

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

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Spinner
android:id="@+id/myspinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />

</LinearLayout>


package com.AndroidListSpinner;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;

public class AndroidListSpinnerActivity extends Activity {

Spinner MySpinner;
List<String> myList;
private ArrayAdapter<String> myAdapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MySpinner = (Spinner)findViewById(R.id.myspinner);

initList();
myAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, myList);
myAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
MySpinner.setAdapter(myAdapter);
}

void initList(){
myList = new ArrayList<String>();
myList.add("Sunday");
myList.add("Monday");
myList.add("Tuesday");
myList.add("Wednesday");
myList.add("Thursday");
myList.add("Friday");
myList.add("Saturday");
}
}


Related Post:
- Apply array to Spinner
- Dynamic change the content of Spinner

No comments:

Post a Comment

Infolinks In Text Ads