안녕하세요 ExpandableList 내에 체크박스를 둠을 통해
체크리스트를 만드려고 하는데요.
아래의 소스를 이용해 만들고 있는데 체크박스의 체크가 자기 멋대로 움직이네요..
SimpleExpandableListAdapter 아답터가 View 를 재활용 하면서 생기는,
SimpleExpandableListAdapter가 TextView 를 가져와 TextView 의 내용만을 바인딩 해서 생기는 문제라는데
어떻게 해야 이 문제를 해결할 수 있을까요?
소스는 아래와 같습니다.
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.CheckBox;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
import android.util.Log;
public class ElistCBox extends ExpandableListActivity
{
private static final String LOG_TAG = "ElistCBox";
static final String colors[] = {
"grey",
"blue",
"yellow",
"red"
};
static final String shades[][] = {
// Shades of grey
{
"lightgrey","#D3D3D3",
"dimgray","#696969",
"sgi gray 92","#EAEAEA"
},
// Shades of blue
{
"dodgerblue 2","#1C86EE",
"steelblue 2","#5CACEE",
"powderblue","#B0E0E6"
},
// Shades of yellow
{
"yellow 1","#FFFF00",
"gold 1","#FFD700",
"darkgoldenrod 1"," #FFB90F"
},
// Shades of red
{
"indianred 1","#FF6A6A",
"firebrick 1","#FF3030",
"maroon","#800000"
}
};
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
setContentView(R.layout.main);
SimpleExpandableListAdapter expListAdapter =
new SimpleExpandableListAdapter(
this,
createGroupList(), // groupData describes the first-level entries
R.layout.group_row, // Layout for the first-level entries
new String[] { "colorName" }, // Key in the groupData maps to display
new int[] { R.id.childname }, // Data under "colorName" key goes into this TextView
createChildList(), // childData describes second-level entries
R.layout.child_row, // Layout for second-level entries
new String[] { "shadeName", "rgb" }, // Keys in childData maps to display
new int[] { R.id.childname, R.id.rgb } // Data under the keys above go into these TextViews
);
setListAdapter( expListAdapter );
}
public void onContentChanged () {
super.onContentChanged();
Log.d( LOG_TAG, "onContentChanged" );
}
@Override
public boolean onChildClick(
ExpandableListView parent,
View v,
int groupPosition,
int childPosition,
long id) {
Log.d( LOG_TAG, "onChildClick: "+childPosition );
CheckBox cb = (CheckBox)v.findViewById( R.id.check1 );
// if( cb != null )
// cb.toggle();
return false;
}
public void onGroupExpand (int groupPosition) {
Log.d( LOG_TAG,"onGroupExpand: "+groupPosition );
}
/**
* Creates the group list out of the colors[] array according to
* the structure required by SimpleExpandableListAdapter. The resulting
* List contains Maps. Each Map contains one entry with key "colorName" and
* value of an entry in the colors[] array.
*/
private List createGroupList() {
ArrayList result = new ArrayList();
for( int i = 0 ; i < colors.length ; ++i ) {
HashMap m = new HashMap();
m.put( "colorName",colors[i] );
result.add( m );
}
return (List)result;
}
/**
* Creates the child list out of the shades[] array according to the
* structure required by SimpleExpandableListAdapter. The resulting List
* contains one list for each group. Each such second-level group contains
* Maps. Each such Map contains two keys: "shadeName" is the name of the
* shade and "rgb" is the RGB value for the shade.
*/
private List createChildList() {
ArrayList result = new ArrayList();
for( int i = 0 ; i < shades.length ; ++i ) {
// Second-level lists
ArrayList secList = new ArrayList();
for( int n = 0 ; n < shades[i].length ; n += 2 ) {
HashMap child = new HashMap();
child.put( "shadeName", shades[i][n] );
child.put( "rgb", shades[i][n+1] );
secList.add( child );
}
result.add( secList );
}
return result;
}
}
//group_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/childname"
android:paddingLeft="50px"
android:textSize="14px"
android:textStyle="italic"
android:layout_width="150px"
android:layout_height="wrap_content"/>
</LinearLayout>
//child_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="@+id/childname"
android:paddingLeft="50px"
android:focusable="false"
android:textSize="14px"
android:textStyle="italic"
android:layout_width="150px"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/rgb"
android:focusable="false"
android:textSize="14px"
android:textStyle="italic"
android:layout_width="100px"
android:layout_height="wrap_content"/>
<CheckBox android:id="@+id/check1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:focusable="false"/>
setTag(), getTag()를 이용해 보세요~!!
http://arabiannight.tistory.com/entry/%EC%95%88%EB%93%9C%EB%A1%9C%EC%9D%B4%EB%93%9CAndroid-ArrayAdapter-BaseAdapter%EB%A5%BC-%EC%82%AC%EC%9A%A9%ED%95%9C-ListView-%EA%B5%AC%ED%98%84