Loop ExpandableListview children for each parent

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Loop ExpandableListview children for each parent



I am trying to assign each of the children in my expandable listview to its proper parents, but I ended up assigning all child data to each parent in the list view.



This is my activity:


api.USER_VM_LIST(UserId)
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Observer<UserVmList>()
@Override
public void onCompleted()



@Override
public void onError(Throwable e)



@Override
public void onNext(UserVmList userVmList)



listVmTitle = new ArrayList<>();
listHashMap = new HashMap<>();
List<DetailsOfVmObject> detailsOfVmObjects = new ArrayList<>();

for (int i = 0; i < userVmList.getVm_data().size(); i++)


//loop Listview RowTitle
VmObject vmObject = new VmObject(userVmList.getVm_data().get(i).getHostname(),
userVmList.getVm_data().get(i).getStatus(), userVmList.getVm_data().get(i).getImage());
listVmTitle.add(vmObject);

//Loop Listview RowChildren
DetailsOfVmObject details = new DetailsOfVmObject(userVmList.getVm_data().get(i).getFlavor(),
userVmList.getVm_data().get(i).getIp_vm());
detailsOfVmObjects.add(details);

//loop hashmap
listHashMap.put(listVmTitle.get(i),detailsOfVmObjects);




LayoutInflater layoutInflater = getLayoutInflater();
ViewGroup header = (ViewGroup)layoutInflater.inflate(R.layout.listview_header,expandableListView,false);
expandableListView.addHeaderView(header);


ExpandableListAdapter adapter = new ExpandableListAdapter(mContext, listVmTitle,listHashMap,detailsOfVmObjects);
expandableListView.setAdapter(adapter);

expandableListView.setOnGroupClickListener((parent, v, groupPosition, id) -> false);


);
}



Here is my Expandablelistadapter definition:


Expandablelistadapter


private Context mContext;
private List<VmObject> listVmTitle;
private HashMap<VmObject, List<DetailsOfVmObject>> listHashMap;
private List<DetailsOfVmObject> detailsOfVmObjects;

public ExpandableListAdapter(Context mContext, List<VmObject> listVmTitle, HashMap<VmObject, List<DetailsOfVmObject>> listHashMap,
List<DetailsOfVmObject> detailsOfVmObjects )
this.mContext = mContext;
this.listVmTitle = listVmTitle;
this.listHashMap = listHashMap;
this.detailsOfVmObjects = detailsOfVmObjects;


@Override
public int getGroupCount()
return listVmTitle.size();


@Override
public int getChildrenCount(int groupPosition)
return listHashMap.get(listVmTitle.get(groupPosition)).size();



@Override
public Object getGroup(int groupPosition)
return listVmTitle.get(groupPosition);


@Override
public Object getChild(int groupPosition, int childPosition)
return listHashMap.get(listVmTitle.get(groupPosition)).get(childPosition);


@Override
public long getGroupId(int groupPosition)
return groupPosition;


@Override
public long getChildId(int groupPosition, int childPosition)
return childPosition;


@Override
public boolean hasStableIds()
return false;


@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
final String VmHostName = listVmTitle.get(groupPosition).getHostName();
final String VmStatus = listVmTitle.get(groupPosition).getStatus();
final String VmOs = listVmTitle.get(groupPosition).getOs();

if (convertView == null)
LayoutInflater layoutInflater = (LayoutInflater)this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.expandable_row,null);


TextView tvHostname = (TextView) convertView.findViewById(R.id.FirstText1);
TextView tvStatus = (TextView) convertView.findViewById(R.id.SecondText2);
TextView tvOs = (TextView) convertView.findViewById(R.id.ThirdText3);

tvHostname.setText(VmHostName);
tvStatus.setText(VmStatus);
tvOs.setText(VmOs);

return convertView;



@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)

String Flavor = detailsOfVmObjects.get(childPosition).getFlavor();
String Ip = detailsOfVmObjects.get(childPosition).getIpAddress();

if (convertView == null)

LayoutInflater layoutInflater = (LayoutInflater)this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.expandable_row_children,null);



TextView TvFlavor = (TextView) convertView.findViewById(R.id.expandable1);
TextView TvIp = (TextView) convertView.findViewById(R.id.expandable2);

TvFlavor.setText(Flavor);
TvIp.setText(Ip);

return convertView;


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



screenshot of the listview oin my current state :



here's what my expandable listview look like now




1 Answer
1



Assume that the hostName is different between groups, try to prepare your data lists like below:


List<VmObject> listVmTitle = new ArrayList<>();
HashMap<VmObject, List<DetailsOfVmObject>> listHashMap = new HashMap<>();

for (int i = 0; i < userVmList.getVm_data().size(); i++)
VmObject vmObject = new VmObject(userVmList.getVm_data().get(i).getHostname(),
userVmList.getVm_data().get(i).getStatus(), userVmList.getVm_data().get(i).getImage());

List<DetailsOfVmObject> detailsOfVmObjects;
for (int j = 0; j < listVmTitle.size(); j++)
if (listVmTitle.get(j).getHostName().equals(vmObject.getHostName())) // Group exists
detailsOfVmObjects = listHashMap.get(vmObject);
else // Group not exists
listVmTitle.add(vmObject); // Add new group
detailsOfVmObjects = new ArrayList<>(); // Create child list for the group
listHashMap.put(listVmTitle.get(i), detailsOfVmObjects);

DetailsOfVmObject details = new DetailsOfVmObject(userVmList.getVm_data().get(i).getFlavor(),
userVmList.getVm_data().get(i).getIp_vm());
detailsOfVmObjects.add(details);




Please note that now only two lists are needed to create the expandable list view, so make changes accordingly. Also in adapter change:


@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent)

DetailsOfVmObject childObject = (DetailsOfVmObject)getChild(groupPosition, childPosition);
String Flavor = childObject.getFlavor();
String Ip = childObject.getIpAddress();

if (convertView == null)

LayoutInflater layoutInflater = (LayoutInflater)this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.expandable_row_children,null);



TextView TvFlavor = (TextView) convertView.findViewById(R.id.expandable1);
TextView TvIp = (TextView) convertView.findViewById(R.id.expandable2);

TvFlavor.setText(Flavor);
TvIp.setText(Ip);

return convertView;



Hope that helps!






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard