286
No
grid.xml
<?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/grid"
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="com.example.choi.ex02.GridActivity">
<!--android:numColumns="3" 한 라인에 출력할 컬럼의 숫자-->
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/grid1"
android:layout_below="@+id/txtResult"
android:numColumns="3"
/>
<TextView
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/txtResult" />
</RelativeLayout>
GridActivity
package com.example.choi.ex02;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;
public class GridActivity extends AppCompatActivity {
//1.변수 선언
TextView txtResult;
GridView grid1;
String[] items={"사과", "포도", "바나나", "자두", "귤", "자몽",
"사과", "포도", "바나나", "자두", "귤", "자몽",
"사과", "포도", "바나나", "자두", "귤", "자몽"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid);
//2.객체 생성
txtResult=(TextView)findViewById(R.id.txtResult);
grid1 =(GridView)findViewById(R.id.grid1);
//3. 아답터 생성
ArrayAdapter<String> adapter=new ArrayAdapter<String>(
this, android.R.layout.simple_list_item_1, items
);
//4. 그리드뷰에 아답터를 연결 시킴
grid1.setAdapter(adapter);
//5.이벤트 처리
grid1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//poistion : 아이템의 인덱스 값(0부터)
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
txtResult.setText(items[position]);
}
});
}
}
댓글 ( 4)
댓글 남기기