Android BaseExpandableListAdapter 例子

//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);
}


ExpandableListView mList = new ExpandableListView(this);
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;
}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值