How to dynamically get child elements in expandablelistview by clicking on a parent node in android… here is a solution to the problem.
How to dynamically get child elements in expandablelistview by clicking on a parent node in android
I’m new to android, so can anyone easily explain how to make childList dynamic by clicking on a parent?
My problem: I have some static parents in an extensible ListView. My intent is that when I select a parent to view its children, the children will dynamically appear from the database and they will be navigable (I want to see more details or go to a different page.) )
I completed Static Child in three steps.
- Use the extensible ListView to create an XML file.
- On the main Java call adapter
- Bind the adapter to an Extensible ListView (bind). Create a custom adapter.
The adapters are as follows:
package com.example.eschool_only_accrdion_straggle;
import android. R.color;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
public class My_custom_adapter extends BaseExpandableListAdapter {
private Context context;
String [] parentList={"Summer Fruit", "Winter Fruit", "Hard fruit", "Watery Fruit"};
String [][] childList={{"Lychy", "Mango"},{"Banana"},{"Pomy Granade"},{"pani Fruit"}};
public My_custom_adapter(Context context) {
this.context = context;
}
In Here 1st Parameter is for PARENT and 2nd one is for CHILD
@Override
public Object getChild(int groupPosition, int childPosition) {
TODO Auto-generated method stub
return null;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
TODO Auto-generated method stub
return 0;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean arg2, View arg3, ViewGroup arg4) {
TextView TxtV_child=new TextView(context);
TxtV_child.setText(childList[groupPosition][childPosition]);
TxtV_child.setPadding(35, 0, 0, 0);
TxtV_child.setTextColor(Color.CYAN);
TxtV_child.setTextSize(17);
return TxtV_child;
}
@Override
public int getChildrenCount(int groupPosition) {
return childList[groupPosition].length;
}
@Override
public Object getGroup(int groupPosition) {
return groupPosition;
}
@Override
public int getGroupCount() {
TODO Auto-generated method stub
return parentList.length;
}
@Override
public long getGroupId(int groupPosition) {
TODO Auto-generated method stub
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean childPosition, View arg2, ViewGroup arg3) {
TextView TxtV_Parent=new TextView(context);
TxtV_Parent.setText(parentList[groupPosition]);
TxtV_Parent.setPadding(5, 5, 0, 5);
TxtV_Parent.setTextColor(Color.MAGENTA);
TxtV_Parent.setTextSize(25);
return TxtV_Parent;
}
@Override
public boolean hasStableIds() {
TODO Auto-generated method stub
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
TODO Auto-generated method stub
return true;
}
}
Solution
Create an activity that extends “ExpandableListActivity”, for example by following this tutorial
Initialize your data
private ArrayList<String> groups = new ArrayList<String>() ;
private ArrayList<ArrayList<String>> children;
private NewAdapter mNewAdapter = null;
public void onCreate(Bundle savedInstanceState) {
// ....
groups.add("Summer Fruit");
groups.add("Winter Fruit");
groups.add("Hard fruit");
groups.add("Watery Fruit");
children = new ArrayList<ArrayList<String>>();
ArrayList<String> a = new ArrayList<String>();
a.add("Lychy");
a.add("Mango");
children.add(a);
a = new ArrayList<String>();
a.add("Banana");
children.add(a);
a = new ArrayList<String>();
a.add("Pomy Granade");
children.add(a);
a = new ArrayList<String>();
a.add("pani Fruit");
children.add(a);
mNewAdapter = new NewAdapter(groups, children);
mNewAdapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE),this);
getExpandableListView().setAdapter(mNewAdapter);
int cpt = mNewAdapter.getGroupCount();
for(int i = 0; i < cpt; i++ ){
getExpandableListView().expandGroup(i);
}
}
Then be sure to overlay the following method into your activity
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
Do here what you need to do when a child is selected
return true;
}
Finally, add your adapter
class NewAdapter extends BaseExpandableListAdapter{
public LayoutInflater minflater;
public Activity activity;
public ArrayList<String> groups;
public ArrayList<ArrayList<String>> points;
public NewAdapter(ArrayList<String> groups, ArrayList<ArrayList<String>> points){
this.groups = groups;
this.points = points;
}
public void setInflater(LayoutInflater mInflater, Activity act) {
this.minflater = mInflater;
activity = act;
}
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return null;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return 0;
}
@Override
public int getChildrenCount(int groupPosition) {
return ((ArrayList<String>) points.get(groupPosition)).size();
}
@Override
public Object getGroup(int groupPosition) {
return groups.get(groupPosition);
}
@Override
public int getGroupCount() {
return groups.size();
}
@Override
public void onGroupCollapsed(int groupPosition) {
super.onGroupCollapsed(groupPosition);
}
@Override
public long getGroupId(int groupPosition) {
return 0;
}
@Override
public View getGroupView(int groupPosition, boolean childPosition, View arg2, ViewGroup arg3) {
// ...
return v;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View v, ViewGroup parent) {
// ....
return v;
}
}
Ensure that isChildSelectable(int groupPosition, int childPosition) returns “true” in the adapter.