491
No
class MainActivity
package org.androidtown.myspinner;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
Spinner spinner, spinner2;
String[] names={"소녀시대", "걸스데이", "씨스타", "티아라"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner=(Spinner)findViewById(R.id.spinner);
//1. 안드로이드 에서 제공되는 어댑터 설정
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,
names
);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext()
, "선택된 아이템" + names[position],
Toast.LENGTH_SHORT
).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
// 2. 스피너 커스텀 어댑터 설정 구조화
/* ArrayList<SingerVO> strings=new ArrayList<>();
strings.add(new SingerVO("소녀시대", R.drawable.a1));
strings.add(new SingerVO("걸스데이", R.drawable.a2));
strings.add(new SingerVO("씨스타", R.drawable.a3));
strings.add(new SingerVO("티아라", R.drawable.a4));
SpinnerAdapter spinnerAdapter=new SpinnerAdapter(getApplicationContext(), strings);
spinner2.setAdapter(spinnerAdapter);*/
}
}
R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="org.androidtown.myspinner.MainActivity">
<Button
android:text="스피너"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="167dp"
android:id="@+id/button"
android:background="@android:color/holo_green_dark" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/spinner"
android:layout_centerHorizontal="true"
android:layout_marginTop="66dp"
android:id="@+id/spinner2" />
</RelativeLayout>
댓글 ( 4)
댓글 남기기