티스토리 뷰

[Android] ContentProvider(컨텐트프로바이더)로 ExpandableList(확장 가능한 목록) 구현 예제 (CursorTreeAdapter (커서 트리 아답터) 사용) 튜토리얼



ContentProvider를 구현했으면 사용을 해야할 것이다. Activity안에서 query로 데이터를 가져올 수 있지만 목록에는 CursorAdapter나 CursorTreeAdapter로 해당하는 목록에 적용 시켜야할 것이다.



지금부터 ExpandableList에 CursorTreeAdapter를 사용해서 ContentProvider를 적용하는 방법을 알아볼 것이고 BaseExpandableListActivity를 확장하는 Activity안에서 작업을 할 것이다. 조금거 기본적인 ExpandableList를 사용하는 방법은 조만간 정리할 예정이다.






0. ContentProvider를 구현해서 준비

: 아래 참고

[안드로이드] 컨텐트프로바이더(ContentProvider) 예제



1. BaseExpandableListActivity를 확장하는 Activity와 ExpandableList가 들어있는 Layout을 준비

:ProjectCampusActivity.java

public class ProjectCampusActivity extends BaseExpandableListActivity 

{

 Context mContext;


    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        mContext = this;

        setContentView(R.layout.main);

 }

}



:layout/main.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" >


    <ExpandableListView

        android:id="@+id/android:list"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" >

    </ExpandableListView>


</LinearLayout>





2.  CursorTreeAdapter를 확장하는 Adapter 생성


: ProjectCampusActivity의 sub class로 생성

: StateSchoolExpandableListAdapter 는 CursorTreeAdapter를 확장하고 필요한 함수들을 구현하면 된다.

public class ProjectCampusActivity extends BaseExpandableListActivity 

{

....


public class StateSchoolExpandableListAdapter extends CursorTreeAdapter{


LayoutInflater mLayoutInflater;

public StateSchoolExpandableListAdapter(Cursor cursor, Context context) {

super(cursor, context);

this.mLayoutInflater = LayoutInflater.from(context);

}

public StateSchoolExpandableListAdapter(Context context) {

super(context.getContentResolver().query(State.getListUri(), State.PROJECTION, null, null, null) , context);

this.mLayoutInflater = LayoutInflater.from(context);

}


@Override

protected void bindChildView(View view, Context context, Cursor cursor,

boolean isLastChild) {

TextView textView = (TextView)view.findViewById(R.id.textViewSchoolName);

            textView.setText(School.getName(cursor));

            

            textView = (TextView)view.findViewById(R.id.textViewEventNumber);

            textView.setText("EventNum");

            

            textView = (TextView)view.findViewById(R.id.textViewTodayEvent);

            textView.setText("TodayEvent");

}


@Override

protected void bindGroupView(View view, Context context, Cursor cursor,

boolean isExpanded) {

TextView textView = (TextView)view.findViewById(R.id.textViewStateName);

            textView.setText(State.getName(cursor));

            

            textView = (TextView)view.findViewById(R.id.textViewEventCount);

            textView.setText("T");

}


@Override

protected Cursor getChildrenCursor(Cursor groupCursor) {

Long id = State.getId(groupCursor);

return getContentResolver().query(School.getListUri(id), School.PROJECTION, null, null, null);

}


@Override

protected View newChildView(Context context, Cursor cursor,

boolean isLastChild, ViewGroup parent) {

View childView = mLayoutInflater.inflate(R.layout.school_item_view , null , false);

return childView;

}


@Override

protected View newGroupView(Context context, Cursor cursor,

boolean isExpanded, ViewGroup parent) {

View groupView = mLayoutInflater.inflate(R.layout.state_item_view , null , false);

            

return groupView;

}

}

....

}


: newGroupView = 그룹 뷰를 새로 생성할때 호출

: bindGroupView = 그룹 뷰에 데이터를 적용시킬때 호출

: newChildView = 자식 뷰를 새로 생성할때 호출

: bindGroupView = 자식 뷰에 데이터를 적용시킬때 호출

: getChildrenCursor = 자식 커서를 가져올때 호출 : 다시 ContentProvider의 Uri를 통해서 Cursor 생성/반환





3. Activity에 Adapter를 적용


: ProjectCampusActivity의 onCreate에서 적용

public class ProjectCampusActivity extends BaseExpandableListActivity {

    private static final int SCHOOL_VIEW = 0;

/** Called when the activity is first created. */

StateSchoolExpandableListAdapter adapter;

Context mContext;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        mContext = this;

        setContentView(R.layout.main);

        

        adapter = new StateSchoolExpandableListAdapter(this);

        this.setListAdapter(adapter);

        

    }
}



구현 끝.

실행한 결과 아래와 같이 잘된다!


* 주의 : CursorTreeAdapter를 사용하는 경우 테이블에 아이디는 _id 로 명명해줘야한다.



끝.

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/03   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31
글 보관함