ListView에 커스텀 어댑터 추가하는법

조회수 3549회

리스트뷰에 커스텀 어댑터를 넣고싶은데 관련 자료를 얻을 수 없을까 싶어서 질문올립니다. 예제를 보여주시면 정말 감사하겠습니다.

1 답변

  • 좋아요

    0

    싫어요
    채택 취소하기
    public class ListAdapter extends ArrayAdapter<Item> {
    
        public ListAdapter(Context context, int textViewResourceId) {
            super(context, textViewResourceId);
        }
    
        public ListAdapter(Context context, int resource, List<Item> items) {
            super(context, resource, items);
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
    
            View v = convertView;
    
            if (v == null) {
                LayoutInflater vi;
                vi = LayoutInflater.from(getContext());
                v = vi.inflate(R.layout.itemlistrow, null);
            }
    
            Item p = getItem(position);
    
            if (p != null) {
                TextView tt1 = (TextView) v.findViewById(R.id.id);
                TextView tt2 = (TextView) v.findViewById(R.id.categoryId);
                TextView tt3 = (TextView) v.findViewById(R.id.description);
    
                if (tt1 != null) {
                    tt1.setText(p.getId());
                }
    
                if (tt2 != null) {
                    tt2.setText(p.getCategory().getId());
                }
    
                if (tt3 != null) {
                    tt3.setText(p.getDescription());
                }
            }
    
            return v;
        }
    
    }
    

    이건 제가 만들던 프로젝트에서 사용한 클래스인데요. 리스트에 보이고 싶은 항목들을 위 예제에서는 List items로 썼는데 원하시는걸로 바꿔서 쓰시면 됩니다. 그리고 getView메소드는 위에걸 참고하셔서 바꾸시면됩니다.

    아래는 R.layout.itemlistrow.xml 으로 각 항목을 어떻게 보여줄지를 정의한겁니다.

    <?xml version="1.0" encoding="utf-8"?>
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="wrap_content" android:orientation="vertical"
        android:layout_width="fill_parent">
    
        <TableRow android:layout_width="fill_parent"
                  android:id="@+id/TableRow01"
                  android:layout_height="wrap_content">
    
            <TextView android:textColor="#FFFFFF"
                      android:id="@+id/id"
                      android:layout_width="fill_parent"
                      android:layout_height="wrap_content"
                      android:text="id" android:textStyle="bold" 
                      android:gravity="left"
                      android:layout_weight="1" 
                      android:typeface="monospace"
                      android:height="40sp" />
        </TableRow>
    
        <TableRow android:layout_height="wrap_content"
                  android:layout_width="fill_parent">
    
            <TextView android:textColor="#FFFFFF" 
                      android:id="@+id/categoryId"
                      android:layout_width="fill_parent"
                      android:layout_height="wrap_content"
                      android:text="categoryId" 
                      android:layout_weight="1" 
                      android:height="20sp" />
    
            <TextView android:layout_height="wrap_content"
                      android:layout_width="fill_parent" 
                      android:layout_weight="1"
                      android:textColor="#FFFFFF"
                      android:gravity="right"
                      android:id="@+id/description"
                      android:text="description" 
                      android:height="20sp" />
        </TableRow>
    
    </TableLayout>
    

    이것도 리스트의 각행을 어떻게 보여줄지 질문자님이 원하시는데로 레이아웃을 만드셔서 수정하시면 됩니다. 사용은

    ListView yourListView = (ListView) findViewById(R.id.itemListView);
    ListAdapter customAdapter = new ListAdapter(this, R.layout.itemlistrow, List<yourItem>);
    yourListView .setAdapter(customAdapter);
    

    이런식으로 하시면됩니다. 도움이 됐으면 좋겠네요.

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)