<?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" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout>
package com.gusfree.listview;
import java.util.ArrayList;
import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
public class ListViewActivity extends Activity {
ListView listView;//서울에 사는 뽀삐
Spinner spinner;
ImageView imageView;
//자원
String[] datas={"티파니","써니","태연","제시카","효연","유리","윤아","서현","수영"};
ArrayList arrayList =new ArrayList();//자원 사용3번째 예
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*listView=null;*/
/*ListView listView;//우리동네 뽀삐*/
arrayList.add("봄");
arrayList.add("햇살");
arrayList.add("시원한 물");
arrayList.add("따뜻한 차");
setContentView(R.layout.main);//이후에 findView
listView=(ListView)findViewById(R.id.listView);
spinner=(Spinner)this.findViewById(R.id.spinner1);
imageView=(ImageView)findViewById(R.id.imageView1);
/* Context 뷰를 올릴 수 있는 뒤쪽의 ....
* Android-Context
* Web-'2d'Graphics
* Java - frame*/
ArrayAdapter sAdapter=
ArrayAdapter.createFromResource(
ListViewActivity.this, R.array.girls,
android.R.layout.simple_spinner_item);
//spinner 클릭 했을때 펼처지는 뷰에 사용할 layout
sAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(sAdapter);
//spinner리스너 구현하기
spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Toast.makeText(ListViewActivity.this,
arg2+" ,"+arg3, 0).show();
/* class Toast{
* static String LENGTH_SHORT=0;
* static String LENGTH_LONG=1;
* */
switch(arg2){
case 0:
imageView.setImageResource(R.drawable.tipa);
break;
case 1:
imageView.setImageResource(R.drawable.sun);
break;
case 2:
imageView.setImageResource(R.drawable.tae);
break;
case 3:
imageView.setImageResource(R.drawable.je);
break;
case 4:
imageView.setImageResource(R.drawable.hyu);
break;
case 5:
imageView.setImageResource(R.drawable.uri);
break;
case 6:
imageView.setImageResource(R.drawable.unna);
break;
case 7:
imageView.setImageResource(R.drawable.seo);
break;
case 8:
imageView.setImageResource(R.drawable.su);
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
//라디오 그룹에서는 호출될 일이 없다.
}});
//res폴더안에 있는 거 전부 가져오기
Resources res = getResources();
//어뎁터
/* 110v 가전제품 - 어댑터 - 220v
일본 가전제품 110v <- 어댑터 <- 220v 전원
ListView <- Adapter <- 화면에 표시할 데이터
스트링 배열*/
//어뎁터 생성
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
R.array.girls);
//arrayList);
//res.getStringArray(R.array.girls));
//datas);
//set adapter
listView.setAdapter(adapter);
//set item Click
listView.setOnItemClickListener(new OnItemClickListener(){
@Override//리스너를 호출한 뷰의 정보가 넘어옵니다.
public void onItemClick(AdapterView<?>
arg0,View arg1, int arg2,long arg3) {
//arg0 = listView
//arg1 = TextView
//arg2 = 포지션값
//리스튜 뷰의 x번째 아이템 값을 가져와라
String msg=(String)listView.getItemAtPosition(arg2);
//간단하게 메시지를 표시해주는 용도로 사용
Toast toast =null;//튀어나오는 뷰
toast = toast.makeText(getApplicationContext(),
arg2+" 번째 "+msg, Toast.LENGTH_SHORT);
toast.show();//화면에 나와라
}
});
}
}