안드로이드 개발 질문/답변
(글 수 45,052)
package se.ks;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.SimpleExpandableListAdapter;
public class ExpandListViewTest extends Activity {
private static final String KEY1 = "GROUP";
private static final String KEY2 = "CHILD";
private String[] GROUPS = {"Group1", "Group2", "Group3"};
private String[][][] CHILDREN = {
{{"Child11", "Text11"}},
{{"Child21", "Text21"}, {"Child22", "Text22"}},
{{"Child31", "Text31"}, {"Child32", "Text32"}, {"Child33", "Text33"}},
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
List<Map<String, String>> groupData =
new ArrayList<Map<String, String>>();
List<List<Map<String, String>>> childData =
new ArrayList<List<Map<String, String>>>();
for (int i = 0; i < GROUPS.length; i++) {
Map<String, String> curGroupMap =
new HashMap<String, String>();
groupData.add(curGroupMap);
curGroupMap.put(KEY1, GROUPS[i]);
curGroupMap.put(KEY2, "");
List<Map<String, String>> children =
new ArrayList<Map<String, String>>();
if (CHILDREN.length > i) {
for (int j = 0; j < CHILDREN[i].length; j++) {
Map<String, String> curChildMap =
new HashMap<String, String>();
children.add(curChildMap);
curChildMap.put(KEY1, CHILDREN[i][j][0]);
curChildMap.put(KEY2, CHILDREN[i][j][1]);
}
}
childData.add(children);
}
ExpandableListAdapter adapter =
new SimpleExpandableListAdapter(
this,
groupData,
android.R.layout.simple_expandable_list_item_1,
new String[] { KEY1, KEY2 },
new int[] { android.R.id.text1, android.R.id.text2 },
childData,
android.R.layout.simple_expandable_list_item_2,
new String[] { KEY1, KEY2 },
new int[] { android.R.id.text1, android.R.id.text2 }
);
ExpandableListView listView =
(ExpandableListView) findViewById(R.id.ExpandableListView);
listView.setAdapter(adapter);
final int groupCount = listView.getCount();
for(int i=0; i <groupCount; i++){
listView.collapseGroup(i);
listView.expandGroup(i);
}
listView.setOnGroupClickListener(new OnGroupClickListener() {
public boolean onGroupClick(ExpandableListView parent,
View v, int groupPosition, long id) {
return false;
}
});
listView.setOnChildClickListener(new OnChildClickListener() {
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
return false;
}
});
}
}
여기서 각 child 들 마다 클릭시 다른 페이지로 가게 intent를 줄려고 하는데
Toast로 값을 좀 알아볼려고 하니 makeText에서 에러가납니다.
listView.setOnChildClickListener(new OnChildClickListener() {
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Toast.makeText(this, childPostion, Toast.LENGTH_SHORT).show();
return false;
}
});
왜그런지 문제랑 각각 다른 intent 를 줄려면 어떻게 해야하는지 궁금합니다.
많은 댓글 부탁드릴께요




Toast.makeText(this, childPostion, Toast.LENGTH_SHORT).show();
여기서 this <- 이거 (ExpandListViewTest.this) 이렇게 변경해야함.
Context 는 내부 class 꺼 말고 젤 외각 (원래 class) 써야함