작업 절차
1. OneItem class 정의
2. OneItem에 대한 xml정의(item_layout.xml) : 배치 디자인
3. ItemListAdapter 에서 getView 함수 수정
4. main_activity.xml 디자인 : ListView 삽입
5. MainActivity에서 List 데이터 생성하여 삽입
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
//ListView에 넣을 데이터의 리스트 변수 생성
List<OneItem> items = new ArrayList<OneItem>();
{
OneItem item= new OneItem();
item.imagefile = R.drawable.ic_launcher;
item.title = "Someone Like You";
item.singer = "Adele";
item.length = "4:47";
items.add(item);//리스트에 삽입
}
{
OneItem item= new OneItem();
item.imagefile = R.drawable.ic_launcher;
item.title = "Stranger In Moscos";
item.singer = "Michael Jackson";
item.length = "5:44";
items.add(item);//리스트에 삽입
}
ListView listView = (ListView)findViewById(R.id.list);
ItemListAdapter adapter = new ItemListAdapter(this, R.layout.item_layout, items);
listView.setAdapter(adapter);
}
//클래스 정의
public class OneItem {
public String title;
public String singer;
public String length;
public Integer imagefile;
}
class ItemListAdapter extends ArrayAdapter<OneItem> {
private List<OneItem> items;
private Context context;
private int layoutResource;
public void setContext(Context c) {
this.context = c;
}
public ItemListAdapter(Context context, int layoutResource, List<OneItem> items) {
super(context, layoutResource, items);
this.context = context;
this.items = items;
this.layoutResource = layoutResource;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(layoutResource, null);
}
final OneItem oneItem = items.get(position);
if (oneItem != null) {
//이하의 부분을 구성
TextView title = (TextView) convertView.findViewById(R.id.title);
TextView singer = (TextView) convertView.findViewById(R.id.singer);
TextView length = (TextView) convertView.findViewById(R.id.length);
ImageView imgView = (ImageView) convertView.findViewById(R.id.imageView1);
if (title != null){
title.setText(oneItem.title);
}
if (singer != null){
singer.setText(oneItem.singer);
}
if (length != null){
length.setText(oneItem.length);
}
if(imgView != null){
imgView.setBackgroundResource(oneItem.imagefile);
}
}
return convertView;
}
}
}
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</RelativeLayout>
item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="128dp"
android:layout_height="128dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/imageView1"
android:layout_toRightOf="@+id/imageView1"
android:text="Title" />
<TextView
android:id="@+id/singer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/title"
android:layout_below="@+id/title"
android:text="Singer" />
<TextView
android:id="@+id/length"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/title"
android:layout_alignBottom="@+id/title"
android:layout_alignParentRight="true"
android:text="Length" />
</RelativeLayout>