'BaseAdapter'에 해당되는 글 1건

  1. 2012.01.10 안드로이드 AdapterView(BaseAdapter) 예제 따라하기
JAVA/Android2012.01.10 14:03
- BaseAdapter를 이용해서 adapter 생성하고,사진 gridview로 뿌리고 imageview 아이템뷰를 만들어서 intent넘기는 프로그래밍을 해보자
- Adapter패턴 적용이 되어 좀더 효율 적인 프로그래밍인된다.

-Java 소스
public class Ex08_AdapterViewActivity extends Activity implements
OnClickListener, OnItemClickListener {

ArrayList<Integer> arrayItem;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview);

arrayItem = new ArrayList<Integer>();// 데이터 생성
arrayItem.add(R.drawable.icon);
arrayItem.add(R.drawable.icon);
arrayItem.add(R.drawable.icon);
arrayItem.add(R.drawable.icon);
arrayItem.add(R.drawable.icon);
arrayItem.add(R.drawable.icon);
arrayItem.add(R.drawable.icon);
arrayItem.add(R.drawable.icon);
arrayItem.add(R.drawable.icon);

ImageAdapter adapter = new ImageAdapter(this, arrayItem);// Adapter 생성

GridView grid = (GridView) this.findViewById(R.id.grid1);// 연결
grid.setAdapter(adapter);

// grid.setOnClickListener(this);
grid.setOnItemClickListener(this);
}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// TODO Auto-generated method stub
Intent intent = new Intent(this, MyPrint.class);
intent.putExtra("param1", arrayItem.get(arg2));
startActivity(intent);
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

}

}
------------------------------------------------------------------------------------------------------- 
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private ArrayList<Integer> mArrayItem;

public ImageAdapter(Context c, ArrayList<Integer> arrayItem) {
mContext = c;
mArrayItem = arrayItem;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return mArrayItem.size(); // 갯수 return
}

@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return mArrayItem.get(arg0); // 해당되는 position의 내용
}

@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0; // 해당되는 position의 id
}

@Override
public View getView(int arg0, View arg1/* item에 대한뷰 하나하나 */, ViewGroup arg2
/* * 뷰* 전체*/) {
// TODO Auto-generated method stub
ImageView imageview;

if (arg1 == null) {

imageview = new ImageView(mContext);
imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageview.setPadding(8, 8, 8, 8);
} else {
imageview = (ImageView) arg1;
}
imageview.setImageResource(mArrayItem.get(arg0));
return imageview;

}

}
--------------------------------------------------------------------------------------------------------
public class MyPrint extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.myprint);

Intent intent = getIntent();

int val = intent.getIntExtra("param1", 0);

TextView text = (TextView) this.findViewById(R.id.text1print);
String s = Integer.toString(val);
text.setText(s);

ImageView view = (ImageView) this.findViewById(R.id.image1print);
view.setImageResource(val);

}
}
 
- 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" >

    <GridView
        android:id="@+id/grid1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:columnWidth="70dp"
        android:gravity="center"
        android:numColumns="auto_fit" />

</LinearLayout>
-------------------------------------------------------------------------------------------------------
 <?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" >

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

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

</LinearLayout>

-실행 화면

[##_http://seungngil.tistory.com/script/powerEditor/pages/1N%7Ccfile9.uf@186DAF3F4F0BC60C1023BF.png%7Cwidth=%22200%22%20height=%22332%22%20alt=%22%22%20filename=%22adapterview2.png%22%20filemime=%22image/jpeg%22%7C_##]

신고
Posted by Namseungil