我想在我的android活动中显示一个ExpandableListAdapter。 所以我在main_activity中构建了这段代码:
protected void onCreate(Bundle savedInstanceState) {
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.results_activity);
expListView = (ExpandableListView) findViewById(R.id.lvExp);
ResultDAO manager = new ResultDAO(this);
listAdapter = new ResultExpandableListAdapter(this, lista, listChildData);
expListView.setAdapter(listAdapter);
expListView.setOnGroupClickListener(new OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
Toast.makeText(getApplicationContext(),
"Group Clicked " + lista.get(groupPosition),
Toast.LENGTH_SHORT).show();
return false;
}
});
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener() {
@Override
public void onGroupExpand(int groupPosition) {
Toast.makeText(getApplicationContext(),
lista.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
}
});
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener() {
@Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getApplicationContext(),
lista.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
}
});
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
lista.get(groupPosition)
+ " : "
+ listChildData.get(
"a").get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
}
});
}catch(Exception e){
System.out.println("pippo");
}
}
现在,如果我尝试运行我的应用程序,我可以看到所有组,但如果我尝试单击一个项目,则从不调用方法,然后我看不到子行。
EDIT 这是ExpandableListAdapter
public class ResultExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<Result> _listDataHeader; // header titles
private HashMap<String, List<ResultXResult>> _listDataChild;
public TextView startDate, endDate,examination;
public TextView parameter, value,interpretation,description,minMax;
public ResultExpandableListAdapter(Context context, List<Result> listDataHeader,
HashMap<String, List<ResultXResult>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final ResultXResult result = (ResultXResult) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.results_x_result_list_item, null);
}
parameter = (TextView) convertView.findViewById(R.id.parameter);
value = (TextView) convertView.findViewById(R.id.valueUm);
interpretation = (TextView) convertView.findViewById(R.id.interpretation);
minMax= (TextView) convertView.findViewById(R.id.minMax);
description = (TextView) convertView.findViewById(R.id.description);
parameter.setText(result.getInfo().getDisplayName());
value.setText(result.getValue()+" "+result.getValueRanceDescription());
interpretation.setText(result.getInterpretationCode().getDisplayName());
minMax.setText(result.getValueRangeMin()+" / "+result.getValueRangeMax());
description.setText(result.getValueRanceDescription());
//txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
Result result = (Result) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.results_list_row, null);
}
startDate = (TextView) convertView.findViewById(R.id.startDate);
startDate.setText(result.getDateStart());
endDate = (TextView) convertView.findViewById(R.id.endDate);
examination = (TextView) convertView.findViewById(R.id.examination);
startDate.setText(result.getDateStart()!=null ? result.getDateStart() : "");
endDate.setText(result.getDateEnd()!=null ? result.getDateEnd() : "");
examination.setText(result.getInfo().getDisplayName());
return convertView;
}
@Override
public boolean hasStableIds() {
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
答案 0 :(得分:0)
在ExpandableAdapter中扩展BaseExpandableListAdapter
// that your ResultExpandableListAdapter
private class ExpandableAdapter extends BaseExpandableListAdapter {
@Override
public Object getChild(int groupPosition, int childPosition) {
return childrenData.get(groupPosition).get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
TextView text = null;
if (convertView != null) {
text = (TextView)convertView;
text.setText(childrenData.get(groupPosition).get(childPosition));
} else {
text = createView(childrenData.get(groupPosition).get(childPosition));
}
return text;
}
@Override
public int getChildrenCount(int groupPosition) {
return childrenData.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition) {
return groupData.get(groupPosition);
}
@Override
public int getGroupCount() {
return groupData.size();
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
TextView text = null;
if (convertView != null) {
text = (TextView)convertView;
text.setText(groupData.get(groupPosition));
} else {
text = createView(groupData.get(groupPosition));
}
return text;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
private TextView createView(String content) {
AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 38);
TextView text = new TextView(list_tempat.this);
text.setLayoutParams(layoutParams);
text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
text.setPadding(40, 0, 0, 0);
text.setText(content);
return text;
}
}
private void showMessage(String message) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
更多详情refer
答案 1 :(得分:0)
您正在呼叫getAdapter
。更改onCreate
中的内容:
expListView.getExpandableListAdapter(listAdapter);