【发布时间】:2016-05-04 10:38:31
【问题描述】:
我想在我的 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");
}
}
现在,如果我尝试运行我的应用程序,我可以看到所有组,但如果我尝试单击一个项目,则永远不会调用方法,那么我就看不到子行。
编辑 这是 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;
}
}
【问题讨论】:
-
你确定 getChildrenCount() 返回的值不是 0
-
不,这很奇怪,因为我在getChildId、getChildView、getChildrenCount等方法中插入了一个断点,当我启动应用程序时,这些方法没有被调用
标签: android expandablelistview