public class DataBWT1 extends Activity {
private Spinner sp; //View
private AutoCompleteTextView auto;
private Button b;
//Model Case 1
private static final String ITEMS[] =
{
"동백꽃", "개나리", "진달래", "철쭉", "백합", "튜울립",
"장미", "봉선화", "무궁화", "벗꽃", "살구꽃", "복숭아꽃",
"할미꽃", "에델바이스", "안개꽃", "카네이션", "들국화",
"가나다", "가나라", "가다라", "가지마", "가나다라",
"cat", "catch", "cast", "case", "castring", "cacaotok"
};
List<String> list; // Model Case 2
ArrayAdapter<String> aa; //Controller
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sp = (Spinner)findViewById(R.id.spinner01);
b = (Button)findViewById(R.id.Button01);
sp.setPrompt("좋아하는 아이템 선택");
auto = (AutoCompleteTextView)findViewById(R.id.AutoCompleteTextView01);
//m1(); //Model - Array
//m2(); //Model - List
//m3(); //Model - Xml
//m1_1(); //Model - Array
//m2_1(); //Model - List
//m3_1(); //Model - Xml
}
void m1_1(){
aa = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, ITEMS);
auto.setAdapter(aa);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String item = auto.getText().toString();
showMsg("선택아이템 : " + item);
}
});
}
void m2_1(){
list = new Vector<String>();
for(String item : ITEMS) list.add(item);
aa = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, list);
auto.setAdapter(aa);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String item = auto.getText().toString();
showMsg("선택아이템 : " + item);
}
});
}
void m3_1(){
ArrayAdapter aa =
ArrayAdapter.createFromResource(this, R.array.animals,
android.R.layout.simple_spinner_item);
auto.setAdapter(aa);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String item = auto.getText().toString();
showMsg("선택아이템 : " + item);
}
});
}
void m3(){
/*final ArrayAdapter aa =
ArrayAdapter.createFromResource(this, R.array.animals,
android.R.layout.simple_spinner_item);*/
ArrayAdapter aa =
ArrayAdapter.createFromResource(this, R.array.animals,
android.R.layout.simple_spinner_item);
sp.setAdapter(aa);
sp.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
public void onItemSelected (AdapterView<?> parent, View view, int position, long id){
ArrayAdapter aa = (ArrayAdapter)parent.getAdapter();
String item = (String)aa.getItem(position);
showMsg("선택 아이템 : "+ aa.getItem(position));
}
public void onNothingSelected (AdapterView<?> parent){
}
});
}
void m1(){
aa = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, ITEMS);
aa.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(aa);
sp.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
public void onItemSelected (AdapterView<?> parent, View view, int position, long id){
showMsg("선택 아이템 : "+ ITEMS[position]);
}
public void onNothingSelected (AdapterView<?> parent){
}
});
}
void m2(){
list = new Vector<String>();
for(String item : ITEMS) list.add(item);
aa = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
aa.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(aa);
sp.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
public void onItemSelected (AdapterView<?> parent, View view, int position, long id){
showMsg("선택 아이템(list) : "+ list.get(position));
}
public void onNothingSelected (AdapterView<?> parent){
}
});
}
void showMsg(String msg){
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
}