main.xml
<?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" 
    android:id="@+id/layout">

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

    <GridView
        android:id="@+id/gridView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:numColumns="3" >
        <!-- android:numColumns="auto_fit"-->
        
    </GridView>

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

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="100px"
        android:layout_height="100px"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>
GridViewActivity.java
package com.gusfree.gridview;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class GridViewActivity extends Activity {
    GridView gridView;
    ArrayList<Data> list=new ArrayList<Data>();  //collection
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); //inflate        
        gridView=(GridView) this.findViewById(R.id.gridView1);        

        list.add(new Data(R.drawable.black,"0번"));
		list.add(new Data(R.drawable.blue,"1번"));
		list.add(new Data(R.drawable.green,"2번"));
		list.add(new Data(R.drawable.red,"3번"));
		list.add(new Data(R.drawable.yell,"4번"));
        
        // 2+ (3*5)
        gridView.setAdapter(new MyAdapter());
        
        //내가 click한 item의 이미지를 배경화면으로 셋팅하기
        gridView.setOnItemClickListener(
        	     new OnItemClickListener(){

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int position, long arg3) {
					Data theData=list.get(position);
					int imageId=theData.getImageId();
					
					GridViewActivity.this
					.findViewById(R.id.layout)
					.setBackgroundResource(imageId);
						
					}});
        
    }
    
    class MyAdapter extends BaseAdapter{

		@Override
		public int getCount() {			
			return list.size();
		}

		@Override
		public Object getItem(int arg0) {			
			return null;
		}

		@Override
		public long getItemId(int arg0) {			
			return 0;
		}

		@Override
		public View getView(int position, View arg1, ViewGroup arg2) {
			//item.xml을 불러오자
			LinearLayout layout=(LinearLayout)
			View.inflate(GridViewActivity.this,  //getApplicationContext();
					R.layout.item, null);
			
			//자원 list     .. list.add(new Data());
			Data oneData=list.get(position);
			
			int imageId=oneData.getImageId();
			//불러온 xml안의 ImageViwe에 이미지를 셋팅합니다
			//   ( 2*(3+5) ) -1 
			((ImageView)layout.
					findViewById(R.id.imageView1)).
					setImageResource(imageId);
			
			//불러온 xml안의 TextView에 글자를 셋팅합니다
			TextView tv=(TextView) layout.findViewById(R.id.textView1);
			tv.setText(oneData.getTitle());
			
			return layout;
		}
    	
    }
}
Data.java
package com.gusfree.gridview;

public class Data {
	private int imageId; // 이미지 주소
	private String title; //은닉화 
	
	public Data(){ //Default 생성자 Constructor
		//new Data();
	}
	
	public Data(int id, String title){//생성자 오버로딩
		//new Data(3, "dd");
		imageId=id;
		this.title=title;
	}
	
	public int getImageId(){
		return imageId;
	}
	
	public String getTitle(){ 
		// 조건 로그인했냐 .. 권한이 있냐.. 추후 ..
		return title;
	}
}




'Android > 2012.04월 강좌' 카테고리의 다른 글

5일차 LogCat을 사용한 error 찾기  (0) 2012.05.02
5일차 Intent  (0) 2012.05.02
5일차 계산기  (0) 2012.05.02
5일차 계산기(내가 한것)  (0) 2012.05.02
5일차 복습  (0) 2012.05.02

+ Recent posts