我有一个包含群组的可扩展列表:大陆和子群:国家/地区。单击一个国家/地区时,我希望将国家/地区显示在另一个类的文本视图中。
package com.zeus.eca;
import android.app.ExpandableListActivity;
import android.content.res.Resources;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;
public class ListCountries extends ExpandableListActivity implements ExpandableListView.OnChildClickListener{
ExpandableListAdapter mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mAdapter=new MyExpandableListAdapter();;
setListAdapter(mAdapter);
getExpandableListView().setOnChildClickListener(this);
};
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
return false;
//return true;
}
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
// Sample data set. children[i] contains the children (String[]) for groups[i].
private String[] groups ={"Africa","Asia","Europe","North America","South America","Oceania","Antartica"};
Resources res = getResources();
private String[] strAfrica=res.getStringArray(R.array.arrayAfrica);
private String[] strAsia=res.getStringArray(R.array.arrayAsia);
private String[] strEurope=res.getStringArray(R.array.arrayEurope);
private String[] strNAmerica=res.getStringArray(R.array.arrayNAmerica);
private String[] strSAmerica=res.getStringArray(R.array.arraySAmerica);
private String[] strOceania=res.getStringArray(R.array.arrayOceania);
private String[] strAntartica=res.getStringArray(R.array.arrayAntartica);
private String[][] children = {
strAfrica,strAsia,strEurope,strNAmerica,strSAmerica,strOceania,strAntartica
};
public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
}
public TextView getGenericView() {
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 55);
TextView textView = new TextView(ListCountries.this);
textView.setLayoutParams(lp);
// Center the text vertically
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textView.setTypeface(Typeface.DEFAULT_BOLD);
// Set the text starting position
textView.setPadding(36, 0, 0, 0);
// textView.setBackgroundResource(R.drawable.colorWhite);
// textView.setTextColor(getResources().getColor(R.color.colorBackground));
return textView;
}
public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getChild(groupPosition, childPosition).toString());
return textView;
}
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}
public int getGroupCount() {
return groups.length;
}
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getGroup(groupPosition).toString());
return textView;
}
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
public boolean hasStableIds() {
return true;
}
}
}
答案 0 :(得分:12)
这应该有效
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
String country = (String)mAdapter.getChild(groupPosition, childPosition);
// update the text view with the country
return true;
}