IT/Android

android - Adapter, GridView, AlertDialog

노마드오브 2018. 11. 10. 22:42
package com.example.it.myapplication9_5;

import android.app.AlertDialog;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setTitle("그리드뷰 영화 포스터");

final GridView gv = (GridView) findViewById(R.id.gridView1);
//3요소 있어야함
//1.위치 (액티비티)
//2.한줄 디자인
MyGridAdapter gAdapter = new MyGridAdapter(this);
//3.데이터
gv.setAdapter(gAdapter);
}

public class MyGridAdapter extends BaseAdapter {
Context context;

int [] posterID = {R.drawable.mov01, R.drawable.mov02, R.drawable.mov03,
R.drawable.mov04, R.drawable.mov05, R.drawable.mov06,
R.drawable.mov07, R.drawable.mov08, R.drawable.mov09, R.drawable.mov10,
R.drawable.mov01, R.drawable.mov02, R.drawable.mov03,
R.drawable.mov04, R.drawable.mov05, R.drawable.mov06,
R.drawable.mov07, R.drawable.mov08, R.drawable.mov09, R.drawable.mov10,
R.drawable.mov01, R.drawable.mov02, R.drawable.mov03,
R.drawable.mov04, R.drawable.mov05, R.drawable.mov06,
R.drawable.mov07, R.drawable.mov08, R.drawable.mov09, R.drawable.mov10};

String [] posterName = {"써니", "완득이", "괴물",
"라디오스타", "비열한거리", "왕의남자",
"아일랜드", "웰컴투동막골", "헬보이", "백투더퓨처",
"써니", "완득이", "괴물",
"라디오스타", "비열한거리", "왕의남자",
"아일랜드", "웰컴투동막골", "헬보이", "백투더퓨처",
"써니", "완득이", "괴물",
"라디오스타", "비열한거리", "왕의남자",
"아일랜드", "웰컴투동막골", "헬보이", "백투더퓨처"};


public MyGridAdapter(Context context) {
this.context = context;
}

@Override
public int getCount() { //중요1//데이터의 갯수 지정
return posterID.length;
}

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

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

@Override
public View getView(int position, View convertView, ViewGroup parent) { //중요2 : 디자인 담당
ImageView imageView = new ImageView(context);
imageView.setLayoutParams(new ViewGroup.LayoutParams(100, 150));
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setPadding(5,5,5,5);

imageView.setImageResource(posterID[position]);

final int pos = position;
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View dialogView = (View) View.inflate(MainActivity.this, R.layout.dialog, null);
AlertDialog.Builder dlg = new AlertDialog.Builder(MainActivity.this);
ImageView ivPoster = (ImageView) dialogView.findViewById(R.id.ivPoster);
ivPoster.setImageResource(posterID[pos]);
dlg.setTitle(posterName[pos]);
dlg.setIcon(R.drawable.ic_launcher);
dlg.setView(dialogView);
dlg.setNegativeButton("닫기", null);
dlg.show();
}
});

return imageView;
}

}

}


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

<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:numColumns="4" >
</GridView>

</LinearLayout>


<?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:gravity="center"
android:orientation="vertical" >

<ImageView
android:id="@+id/ivPoster"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</LinearLayout>