2016-07-22 96 views
0

所以我已經能夠顯示組文本,但是當我展開我不知道如何獲取子文本。擴展的列表視圖不顯示子對象

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

我知道這是不對的,但林不知道如何從我的子對象的名稱。

+0

什麼是DoctorObject \ –

+0

@RobinDijkhof DoctorObject是具有名稱,專業和幾個對象更多的東西。基本上我如何指定我想.getName(); – Adilp

回答

1

你想投DoctorObject字符串,這是不可能的。而不是做這在你DoctorObject(其中getString回報名稱或DoctroObject標題)創建getString()方法,並做如下:?

DoctorObject model = (DoctorObject)getChild(groupPosition, childPosition); 
String childText = model.getName(); 
+0

我覺得我們很接近,當我這樣做,並擴大了標籤中的列表視圖顯示「com .... DoctorObject @ 2c36 ....」(我把...因爲有一些額外的垃圾)其中一個是相同的,但「@ 3fd ...」我假設這顯示對象的引用作爲一個字符串? DoctorObject還有一些屬性,如名稱,專業,位置等。因此,它必須是.getName();某處。 – Adilp

+0

@Adilp是的確定它返回字符串。通過getString()我的意思是返回名稱或位置。 – Amir

+0

我忘記了.getString();將無法工作。我不得不使用toString();任何事情都要展現出來。以同樣的方式.getName();將無法工作。唯一的選擇是toString,equals,hashCode等。 – Adilp