//MyActivity.java 片段
private List<String> groupArray;
private List<List<String>> childArray;
private AlertDialog industrialDialog;
public void onClickIndustrialButton(View view){
groupArray = new ArrayList<String>();
childArray = new ArrayList<List<String>>();
String[] stringArray = getResources().getStringArray(R.array.industrial_classification_array);
for (String str : stringArray) {
groupArray.add(str);
}
int[] childIds={
R.array.food_classification_array,
R.array.recreation_classification_array,
R.array.living_services_classification_array,
R.array.shopping_classification_array,
R.array.hotel_classification_array,
R.array.beauty_classification_array,
R.array.sports_fitness_classification_array,
};
List<String> childsStringArray;
for(int id:childIds){
childsStringArray = new ArrayList<String>();
stringArray = getResources().getStringArray(id);
for (String str : stringArray) {
childsStringArray.add(str);
}
childArray.add(childsStringArray);
}
mList.setChoiceMode(ExpandableListView.CHOICE_MODE_SINGLE);
ExpandableAdapter mAdapter = new ExpandableAdapter(this, groupArray, childArray);
mList.setAdapter(mAdapter);
mList.setOnChildClickListener(new OnChildClickListener() {
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
mIndustry = ((TextView) v).getText().toString();
getStoreSearch();
industrialDialog.cancel();
return false;
}
});
AlertDialog.Builder builder = new Builder(this);
builder.setTitle(R.string.industrial_classification);
industrialDialog = builder.create();
industrialDialog.setView(mList);
industrialDialog.show();
}
====================================================
ExpandableAdapter .java:
package com.pafee.stroll;
import java.util.List;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class ExpandableAdapter extends BaseExpandableListAdapter{
private Context context;
private List<String> groupArray;
private List<List<String>> childArray;
public ExpandableAdapter(Context a,List<String> groupArray,List<List<String>> childArray)
{
context = a;
this.groupArray = groupArray;
this.childArray = childArray;
}
public Object getChild(int groupPosition, int childPosition)
{
return childArray.get(groupPosition).get(childPosition);
}
public long getChildId(int groupPosition, int childPosition)
{
return childPosition;
}
public int getChildrenCount(int groupPosition)
{
return childArray.get(groupPosition).size();
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent)
{
String string = childArray.get(groupPosition).get(childPosition);
return getGenericView(string);
}
// group method stub
public Object getGroup(int groupPosition)
{
return groupArray.get(groupPosition);
}
public int getGroupCount()
{
return groupArray.size();
}
public long getGroupId(int groupPosition)
{
return groupPosition;
}
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
{
String string = groupArray.get(groupPosition);
return getGenericView(string);
}
// View stub to create Group/Children 's View
public TextView getGenericView(String string)
{
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 64);
TextView text = new TextView(context);
text.setLayoutParams(layoutParams);
// Center the text vertically
text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
text.setPadding(50, 0, 0, 0);
text.setText(string);
return text;
}
public boolean hasStableIds(){
return false;
}
public boolean isChildSelectable(int groupPosition, int childPosition){
return true;
}
}