2011년 6월 3일 금요일

Android - Custom ListView

use LayoutInflater


LayoutInflater


This class is used to instantiate layout XML file into its corresponding View objects. It is never be used directly -- usegetLayoutInflater() or getSystemService(String) to retrieve a standard LayoutInflater instance that is already hooked up to the current context and correctly configured for the device you are running on.
(http://developer.android.com/reference/android/view/LayoutInflater.html)



아래 구성 요소들이 필요하다
1. LIST를 구성하는 ROW의 LAYOUT FILE(layout 폴더에 위치, XML)
2. LIST의 내용이 되는 데이터 클래스
3. LIST에 VIEW를 제공하는 ArrayAdapter 상속 클래스



각각 파일 예제


1. ROW의 LAYOUT(layout/add_row.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="wrap_content"
android:minHeight="60dp"
android:orientation="horizontal"
>
    <LinearLayout android:layout_marginRight="10dp"
          android:layout_height="wrap_content"
          android:id="@+id/linearLayout2"
          android:layout_marginBottom="10dp"
          android:layout_marginTop="10dp"
          android:layout_width="fill_parent"
          android:orientation="horizontal">
        <ImageView android:src="@drawable/icon"
                  android:id="@+id/list_view_row_add_row_icon"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_gravity="center_vertical"></ImageView>
        <TextView android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:id="@+id/list_view_row_add_row_name"
                  android:text="name"
                  android:layout_gravity="center_vertical"
                  android:textSize="25dp"
                  android:paddingLeft="5dp"></TextView>
    </LinearLayout>

</LinearLayout>

2. DATA 클래스(ActionDTO.java)

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.view.View.OnClickListener;

public class ActionDTO {

private Drawable icon;
private String title;

public Drawable getIcon() {
return icon;
}
public void setIcon(Drawable icon) {
this.icon = icon;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}


3. ArrayAdapter(AddListViewAdapter.java)


import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class AddListViewAdapter extends ArrayAdapter<ActionDTO>{
private Context context;
private int layout;
private ArrayList<ActionDTO> actionList;
private LayoutInflater layoutInflater;


public AddListViewAdapter(Context context, int textViewResourceId, ArrayList<ActionDTO> objects) {
super(context, textViewResourceId, objects);
// TODO Auto-generated constructor stub
this.context = context;
this.layout = textViewResourceId;
this.actionList = objects;
                //LayoutInflater 생성
this.layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ActionDTO action = actionList.get(position);

if(convertView == null){
                        //xml 파일로 정의된 layout 을 View 클래스로 변환
convertView = layoutInflater.inflate(layout, null);
}

if(action != null){
                        //convertView 라는 View 오브젝트를 이용해서 각각의 콤포넌트를 컨트롤
TextView tvName = (TextView) convertView.findViewById(R.id.list_view_row_add_row_name);
ImageView imgIcon = (ImageView) convertView.findViewById(R.id.list_view_row_add_row_icon);

tvName.setText(action.getTitle());
imgIcon.setImageDrawable(action.getIcon());
}

return convertView;
}
}



4. 실제 사용 예제


//listview, arraylist, adapter 초기화
ListView lstActionList = (ListView)findViewById(R.id.list_add);
ArrayList<ActionDTO> arrActionList = new ArrayList<ActionDTO>();
AddListViewAdapter adptActionList = new AddListViewAdapter(this, R.layout.add_row, arrActionList);

//adapter 세팅
lstActionList.setAdapter(adptActionList);

//list의 내용을 채울 ArrayList 작성

res = this.getResources();
ActionDTO action = new ActionDTO();
action.setTitle("ADD");
action.setIcon(res.getDrawable(android.R.drawable.ic_input_add));
arrActionList.add(action);

//listview refresh(?)
adptActionList.notifyDataSetChanged();