您好我希望我的可扩展列表视图中的孩子开始一项新活动,有人可以告诉我我哪里出错了。我尝试过这样做,但是我点击任意一个子视图后,我使用的代码会杀死应用程序。我根据请求添加了适配器代码。感谢。
public class MainActivity extends Activity implements NumberPicker.OnValueChangeListener, View.OnClickLis``tener {
String[] searches = {"Height", "Weight", "Hair Colour"};
List<String> childList;
List<String> groupList;
Map<String, List<String>> advSearchOption;
ExpandableListView expListView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createGroupList();
createCollection();
expListView = (ExpandableListView) findViewById(R.id.search_list);
final ExpandableListAdapter expListAdapter = new ExpandableListAdapter(
this, groupList, advSearchOption);
expListView.setAdapter(expListAdapter);
expListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
Intent k;
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
switch (childPosition){
case 0:
k = new Intent(getApplicationContext(), ChildActivity.class);
break;
case 1:
k = new Intent(getApplicationContext(), ChildActivity.class);
break;
}
startActivity(k);
return true;
}
});
private void createGroupList() {
groupList = new ArrayList<String>();
groupList.add("Advanced Search");
}
private void createCollection() {
// preparing laptops collection(child)
String[] searches = {"Height", "Weight", "Hair Colour"};
advSearchOption = new LinkedHashMap<String, List<String>>();
for (String advSearch : groupList) {
if (advSearch.equals("Advanced Search")) {
loadChild(searches);
}
advSearchOption.put(advSearch, childList);
}
}
private void loadChild(String[] searchOptions) {
childList = new ArrayList<String>();
for (String model : searchOptions)
childList.add(model);
}
private void setGroupIndicatorToRight() {
/* Get the screen width */
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
包com.mycompany.testing;
public class ExpandableListAdapter扩展了BaseExpandableListAdapter {
private Activity context;
private Map<String, List<String>> advSearchOptions;
private List<String> options;
public ExpandableListAdapter(Activity context, List<String> options,
Map<String, List<String>> advSearchOptions) {
this.context = context;
this.advSearchOptions = advSearchOptions;
this.options = options;
}
public Object getChild(int groupPosition, int childPosition) {
return advSearchOptions.get(options.get(groupPosition)).get(childPosition);
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String laptop = (String) getChild(groupPosition, childPosition);
LayoutInflater inflater = context.getLayoutInflater();
if (convertView == null) {
convertView = inflater.inflate(R.layout.child_item, null);
}
TextView item = (TextView) convertView.findViewById(R.id.advSearch);
item.setText(laptop);
return convertView;
}
public int getChildrenCount(int groupPosition) {
return advSearchOptions.get(options.get(groupPosition)).size();
}
public Object getGroup(int groupPosition) {
return options.get(groupPosition);
}
public int getGroupCount() {
return options.size();
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String laptopName = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.group_item,
null);
}
TextView item = (TextView) convertView.findViewById(R.id.advSearch);
item.setTypeface(null, Typeface.BOLD);
item.setText(laptopName);
return convertView;
}
public boolean hasStableIds() {
return true;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}