Adapter´Â µ¥ÀÌÅÍ Å×À̺íÀ» ¸ñ·Ï ÇüÅ·Πº¸¿©ÁÖ±â À§ÇØ »ç¿ëµÇ´Â °ÍÀ¸·Î µ¥ÀÌÅ͸¦ ´Ù¾çÇÑ Çü½ÄÀÇ ¸®½ºÆ®
Çü½ÄÀ¸·Î º¸¿©ÁÖ±â À§Çؼ µ¥ÀÌÅÍ¿Í ¸®½ºÆ® ºä »çÀÌ¿¡ Á¸ÀçÇÏ´Â °´Ã¼ÀÔ´Ï´Ù.
Áï, °£´ÜÈ÷ ¸»ÇØ µ¥ÀÌÅÍ¿Í ¸®½ºÆ® ºä »çÀÌÀÇ Åë½ÅÀ» À§ÇÑ ´Ù¸® ¿ªÈ°À» ÇÕ´Ï´Ù.
¾Èµå·ÎÀ̵带 »ìÆ캸¸é¼.. µ¥ÀÌÅ͸¦ »ç¿ëÀÚ Á¤ÀÇ ¸®½ºÆ®ºä Çü½ÄÀ¸·Î Ç¥ÇöÇϴµ¥ ÀÖ¾î¼ ¸Å¿ì À¯¿¬ÇÑ ¹æ½ÄÀ»
Á¦°øÇÑ´Ù´Â °ÍÀ» ¾Ë°Ô µÇ¾ú°í... ÀÌ·± À¯¿¬ÇÔÀ» À§ÇØ ´Ù¼Ò º¹ÀâÇÑ ±¸Á¶¿¡ Àͼ÷ÇØÁ® º¼ »ý°¢À¸·Î ÀÌ ±ÛÀ» ÀÛ¼ºÇÏ°Ô µÇ¾ú½À´Ï´Ù.
¾Èµå·ÎÀ̵带 óÀ½ Á¢ÇϽô ºÐµé¿¡°Ô »ó´çÈ÷ ºÒÄ£ÀýÇÑ ³»¿ëÀ̶ó°í »ý°¢µË´Ï´Ù.
Á¦ °³ÀÎÀûÀ¸·Î´Â Á¤¸® Â÷¿øÀÇ ±ÛÀ̶ó´Â Á¡À» ´Ù½Ã±Ý ¾ð±ÞÇØ µå¸³´Ï´Ù.
¸ÕÀú ¾Æ·¡¿Í °°Àº °á°ú¸¦ ¸ñÇ¥·Î Çؼ »ç¿ëÀÚ Á¤ÀÇ Adapter ¸¸µé±â¿¡ ´ëÇÑ ÇÙ½É ³»¿ëÀ» Á¤¸®ÇØ º¸°Ú½À´Ï´Ù.
Àι°¿¡ ´ëÇÑ »çÁø°ú À̸§ ±×¸®°í »ýÀÏ Á¤º¸¸¦ ¸®½ºÆ® Çü½ÄÀ¸·Î º¸¿©ÁÖ°í Àִ ȸéÀÔ´Ï´Ù.
À§ÀÇ È¸é¿¡ ´ëÇÑ ·¹À̾ƿôÀº ´ÙÀ½°ú °°½À´Ï´Ù.
Áï.. ActivityÀÇ View Content°¡ profilelistview.xml¿¡ ÇØ´çµÇ¸ç ¸®½ºÆ®ºä¸¦ ä¿ì´Â
°¢ Ç׸ñÀº profileview.xml·Î Á¤Àǵȴٴ ³»¿ëÀÔ´Ï´Ù. ¸ÕÀú Å« ·¹À̾ƿôÀÎ profilelistview.xml¿¡ ´ëÇÑ ÄÚµå´Â ´ÙÀ½°ú °°½À´Ï´Ù.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:a="http://schemas.android.com/apk/res/android"
a:orientation="vertical"
a:layout_width="fill_parent"
a:layout_height="fill_parent"
a:gravity="center">
<ListView
a:id="@+id/list"
a:layout_width="fill_parent"
a:layout_height="fill_parent" />
</LinearLayout>
´Ü¼øÈ÷ ·¹À̾ƿô ¾È¿¡ list¶ó´Â idÀÇ ListView À§Á¬¸¸À» °¡Áö°í ÀÖ½À´Ï´Ù.
±×¸®°í ´ÙÀ½ ÄÚµå´Â ÀÌ ListView À§Á¬ ¾È¿¡ ´ãÀ» Ç׸ñ¿¡ ´ëÇÑ ºä¿¡ ÇØ´çÇÏ´Â profileview.xmlÀÔ´Ï´Ù.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:a="http://schemas.android.com/apk/res/android"
a:orientation="horizontal"
a:layout_width="fill_parent"
a:layout_height="wrap_content">
<ImageView
a:id="@+id/photo"
a:layout_width="50dp"
a:layout_height="60dp"
a:focusable="false" />
<LinearLayout
a:orientation="vertical"
a:layout_width="fill_parent"
a:layout_height="fill_parent"
a:gravity="center">
<TextView
a:id="@+id/name"
a:layout_width="match_parent"
a:layout_height="wrap_content"
a:textColor="#ffff00"
a:textSize="13pt" />
<TextView
a:id="@+id/telephone"
a:layout_width="match_parent"
a:layout_height="23dp"
a:textColor="#888888"
a:textSize="13pt" />
</LinearLayout>
</LinearLayout>
À§ÀÇ Äڵ带 µµ½ÄÈ ÇÏ¸é ¾Æ·¡¿Í °°½À´Ï´Ù.
¼öÆò Á¤·Ä·Î ÁöÁ¤µÈ ·¹À̾ƿô ¾È¿¡ ImageView À§Á¬°ú TextView À§Á¬ 2°³¸¦ °¡Áö°í ÀÖ´Â ¼öÁ÷ Á¤·Ä·Î
ÁöÁ¤µÈ ·¹À̾ƿô¿¡ ´ëÇÑ ³»¿ëÀÔ´Ï´Ù. ÀÌÁ¦ ÀÌ·¸°Ô UI°¡ Á¤ÇØÁ³À¸´Ï Äڵ忡 ´ëÇؼ Á¤¸®ÇØ º¸°Ú½À´Ï´Ù.
¸ÕÀú ¸®½ºÆ® ºä¸¦ ä¿ï µ¥ÀÌÅ͸¦ ³ªÅ¸³¾ Ŭ·¡½ºÀÎ Profile¿¡ ´ëÇÑ ÄÚµåÀÔ´Ï´Ù.
Profile.java
class Profile {
private int _photo;
private String _name;
private String _telephone;
public int getPhoto() {
return _photo;
}
public String getName() {
return _name;
}
public String getTelephone() {
return _telephone;
}
public Profile(int photo, String name, String telephone) {
_photo = photo;
_name = name;
_telephone = telephone;
}
}
»çÁø¿¡ ´ëÇÑ ¸®¼Ò½º ID°ª°ú À̸§ ±×¸®°í ÀüȹøÈ£¿¡ ´ëÇÑ °ªÀ» ÀúÀåÇÏ°í ¾ò±â À§ÇÑ ´Ü¼øÇÑ Å¬·¡½ºÀÔ´Ï´Ù.
´ÙÀ½À¸·Î ºä¸¦ ȸ鿡 Ç¥½ÃÇϱâ À§ÇÑ Activity Ŭ·¡½º¸¦ »ó¼Ó¹ÞÀº MainActivity Ŭ·¡½º ÀÔ´Ï´Ù.
MainActivity.java
public class MainActivity extends Activity implements OnItemClickListener {
private ArrayList _profiles = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profilelistview);
_profiles = new ArrayList();
Profile p1 = new Profile(R.drawable.boa, "º¸¾Æ", "1986³â 11¿ù 5ÀÏ");
Profile p2 = new Profile(R.drawable.kaj, "±è¾ÆÁß", "1982³â 10¿ù 16ÀÏ");
Profile p3 = new Profile(R.drawable.nrs, "³ª¸£»þ", "1981³â 12¿ù 28ÀÏ");
Profile p4 = new Profile(R.drawable.lsy, "À̼ö¿µ", "1979³â 4¿ù 12ÀÏ");
Profile p5 = new Profile(R.drawable.jyj, "ÀåÀ±Á¤", "1980³â 2¿ù 16");
Profile p6 = new Profile(R.drawable.nhh, "³ëÇöÈñ", "1972³â 8¿ù 23ÀÏ");
Profile p7 = new Profile(R.drawable.nhh, "³ëÇöÈñ", "1972³â 8¿ù 23ÀÏ");
Profile p8 = new Profile(R.drawable.nhh, "³ëÇöÈñ", "1972³â 8¿ù 23ÀÏ");
_profiles.add(p1);
_profiles.add(p2);
_profiles.add(p3);
_profiles.add(p4);
_profiles.add(p5);
_profiles.add(p6);
_profiles.add(p7);
_profiles.add(p8);
ProfileListAdapter adapter = new ProfileListAdapter(this, R.layout.profileview, _profiles);
ListView listView = (ListView)findViewById(R.id.list);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
public void onItemClick(AdapterView parent, View v, int position, long id)
{
String name1 = _profiles.get(position).getName();
Toast.makeText(getApplicationContext(),"position:"+position+",getName:"+name1,500).show();
}
}
2¹ø ÄÚµåÀÇ _profiles´Â µ¥ÀÌÅÍ ¸ñ·ÏÀ» ´ã°í ÀÖ´Â ÄÁÅ×À̳ÊÀÔ´Ï´Ù.
´ÙÀ½À¸·Î, ÀÌ ÄÁÅ×À̳ʿ¡ µ¥ÀÌÅ͸¦ Ãß°¡ÇÏ´Â ÄÚµå Á¸ÀçÇÕ´Ï´Ù.
ÀÌ ÄÁÅ×À̳ÊÀÇ µ¥ÀÌÅÍ°¡ ¹Ù·Î ¸®½ºÆ® ºä¿¡ Ç¥½ÃµÉ Á¤º¸ÀÔ´Ï´Ù. ÀÌ µ¥ÀÌÅÍ¿Í ¸®½ºÆ® ºä¸¦ ¿¬°áÇØÁÖ±â À§ÇÑ °ÍÀÌ ¹Ù·Î
ÀÌ ±ÛÀÇ ÁÖÁ¦ÀÎ »ç¿ëÀÚ Á¤ÀÇ AdapterÀÎ ProfileListAdapter ÀÔ´Ï´Ù.
ÀÌ »ç¿ëÀÚ Á¤ÀÇ Adapter Ŭ·¡½º´Â BaseAdapter¸¦ »ó¼Ó¹Þ½À´Ï´Ù. ÄÚµå´Â ¾Æ·¡¿Í °°½À´Ï´Ù.
ProfileListAdapter.java
class ProfileListAdapter extends BaseAdapter {
private LayoutInflater _inflater;
private ArrayList _profiles;
Context context;
private int _layout;
public ProfileListAdapter(Context context, int layout, ArrayList profiles) {
_inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
_profiles = profiles;
_layout = layout;
}
@Override
public int getCount() {
return _profiles.size();
}
@Override
public String getItem(int pos) {
return _profiles.get(pos).getName();
}
@Override
public long getItemId(int pos) {
return pos;
}
@Override
public View getView(final int pos, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = _inflater.inflate(_layout, parent, false);
}
Profile profile = _profiles.get(pos);
ImageView photo = (ImageView)convertView.findViewById(R.id.photo);
photo.setImageResource(profile.getPhoto());
TextView name = (TextView)convertView.findViewById(R.id.name);
name.setText(profile.getName());
TextView telephone = (TextView)convertView.findViewById(R.id.telephone);
telephone.setText(profile.getTelephone());
return convertView;
}
}
Áß¿äÇÑ ºÎºÐ¸¸ ¾ð±ÞÇϸé... OverrideÇؾßÇÒ ¸Å¼µå´Â ¸ðµÎ 4°³·Î½á getCount, getItem, getItemId, getViewÀÔ´Ï´Ù.
±×¸®°í ¸®½ºÆ® ºäÀÇ Ç׸ñ¿¡ ´ëÇÑ ºä¸¦ »ý¼ºÇÏ´Â getView´Â óÀ½ È£ÃâµÉ¶§ µÎ¹ø° ÀÎÀÚÀÎ convertView°¡ nullÀ̸ç
ÀÌÈÄ °³¹ßÀÚ°¡ Á÷Á¢ ÀνºÅϽº¸¦ »ý¼ºÇØÁÖ¸é ÀÌÈÄ È£ÃâµÉ¶§´Â óÀ½ È£ÃâµÉ¶§ »ý¼ºµÈ ÀνºÅϽº°¡ Àü´ÞµÇ´Â ±¸Á¶·Î½á
ÀνºÅϽºÀÇ »ý¼º¿¡ ´ëÇÑ ºÎ´ãÀ» ÃÖ¼ÒÈÇϱâ À§ÇÑ ¹æ¾ÈÀÔ´Ï´Ù.
ÀÌ¹Ì ¾Èµå·ÎÀ̵åÀÇ »ç¿ëÀÚ Á¤ÀÇ Adapter ¸¸µé±â¿¡ ´ëÇÑ ³»¿ëÀº ¿©Å¸ÀÇ ´Ù¾çÇÑ °³¹ß Ç÷§Æû¿¡¼µµ »ç¿ëµÇ°í ÀÖ´Â ±¸Á¶ÀÌÁö¸¸...
¾Èµå·ÎÀ̵带 ÅëÇØ ´Ù½Ã±Ý Á¢ÇØ º½À¸·Î½á.. ÀÌ·¯ÇÑ ±¸Á¶¿¡ Àͼ÷ÇØÁ®¼ ÀÚ½ÅÀÌ °³¹ßÇÏ°í ÀÖ´Â ¼ÒÇÁÆ®¿þ¾î¿¡
±× ¼³°è ±¸Á¶ ÀÚü¸¦ Àû¿ëÇØ º¼ ¼ö ÀÖ´Â ¹ßÀüÀ¸·Î±îÁö À̾±æ ½º½º·Î¿¡°Ô ´ÙÁüÇØ º¾´Ï´Ù.