扩展列表视图未显示子对象

时间:2016-07-22 18:42:46

标签: android arrays listview object expandablelistview

所以我能够显示组文本,但是当我展开时我不确定如何获取子文本。

 List<TeamObject> listDataDoc;
HashMap<TeamObject, List<DoctorObject>> listDataChildStaff;

我将listDataDoc和ListDataChildStaff传递给我的适配器。在我的主要活动中:

listAdapter = new ExpandableListAdapter(this,  listDataDoc, listDataChildStaff);

此前我正在调用prepareListData();

private void prepareListData() {
    listDataDoc = new ArrayList<>();
    listDataChildStaff = new HashMap<>();

    ArrayList<TeamObject> all = new ArrayList<>();


    TeamObject adil = new TeamObject();
    adil.setStaffName("Adil Patel");
    adil.setStaffType(1);

    listDataDoc.add(adil);

    List<DoctorObject> adilGroup = new ArrayList<>();

    DoctorObject cindy = new DoctorObject("Cindy G");

    DoctorObject Bettina = new DoctorObject("Bettina");

    adilGroup.add(cindy);
    adilGroup.add(Bettina);

    listDataChildStaff.put(listDataDoc.get(0),adilGroup);

我的适配器看起来像这样:

public class ExpandableListAdapter extends BaseExpandableListAdapter {

private Context _context;
private List<TeamObject> _listDataHeader; // header titles
private HashMap<TeamObject, List<DoctorObject>> _listDataChild;

//Keep track of checks
//ArrayList<ArrayList<Integer>> check_states = new ArrayList<ArrayList<Integer>>();

public ExpandableListAdapter(Context context, List<TeamObject> listDataHeader, HashMap<TeamObject, List<DoctorObject>> 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 String childText = (String) getChild(groupPosition, childPosition);




    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_item_create_team_child, null);
    }

    final CheckBox chk = (CheckBox) convertView.findViewById(R.id.checkBoxChild);

    chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                                        @Override
                                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                                                if (isChecked == true){
                                                    //_listDataChild.get(childPosition)
                                                }
                                        }
                                    }
    );

    //String test = String.valueOf(_listDataChild.containsKey("Adil"));

    TextView txtListChild = (TextView) convertView
            .findViewById(R.id.teamNameChild);

    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) {



    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_item_create_team, null);
    }

    TextView lblListHeader = (TextView) convertView
            .findViewById(R.id.teamName);
    lblListHeader.setTypeface(null, Typeface.BOLD);
    lblListHeader.setText(_listDataHeader.get(groupPosition).getStaffName());

    return convertView;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
    return true;
}

}

现在,当我使用该代码运行应用程序时,我最初得到了这个:

enter image description here

大!但是,当我扩展

我收到以下错误

FATAL EXCEPTION: main
                                                                     Process: com.adilpatel.vitalengine, PID: 4493
                                                                     java.lang.ClassCastException: com.adilpatel.vitalengine.expand.DoctorObject cannot be cast to java.lang.String
                                                                         at com.adilpatel.vitalengine.Expand2.ExpandableListAdapter.getChildView(ExpandableListAdapter.java:54)

第54行:

final String childText = (String) getChild(groupPosition, childPosition);

我知道这是错的,但我不知道如何从我的子对象中获取名称。

1 个答案:

答案 0 :(得分:1)

您想将Sub subjectLine() Dim Title As String Set objItem = GetCurrentItem() Title = objItem.subject MsgBox (Title) End Sub 转换为字符串,但这是不可能的。而不是这样做,而是在DoctorObjectgetString()返回DoctorObject的名称或标题)中创建getString方法并执行以下操作:

DoctroObject