child list repeat with the no. of item in the Expandable ListView

  • Replies:0
  • OpenNot stickiedUnanswered
  • Forum posts: 1

Apr 25, 2016 12:37:15 PM via Website

Please look at my code please help me the child list repeat with the no. of item in the child list. parent list display correct and child list display repeated and sub child list display correct.

public class MainActivity extends Activity
{

private static final String TAG = AES.class.getSimpleName();
ProgressDialog PD;
ArrayList list;
ArrayList ch_list;
ArrayList sb_ch_list;
Group gru;
Child ch;
Sub_Child sub_child;

private ExpandListAdapter ExpAdapter;
private ExpandableListView ExpandList;

@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

ExpandList = (ExpandableListView) findViewById(R.id.exp_list);

PD = new ProgressDialog(this);
PD.setMessage("Loading.....");
PD.setCancelable(false);

Category();

}

private void Category()
{
// Tag used to cancel the request
String tag_string_req = "req_login";

PD.show();

StringRequest strReq = new StringRequest(Request.Method.POST, AppConfig.MAIN_URL + AppConfig.MENU_URL,
        new Response.Listener<String>()
        {

            @Override
            public void onResponse(String response)
            {
                Log.d(TAG, "Response: " + response.toString());
                PD.dismiss();

                try
                {
                    JSONObject jObj = new JSONObject(response);
                    String Status = jObj.getString("status");

                    if (Status.equals("success"))
                    {
                        list = new ArrayList<Group>();

                        JSONArray jsonArray = jObj.getJSONArray("data");
                        for (int i = 0; i < jsonArray.length(); i++)
                        {
                            JSONObject jsonObject = jsonArray.getJSONObject(i);
                            String Name = jsonObject.getString("Name");
                            gru = new Group();
                            gru.setName(Name);

                            ch_list = new ArrayList<Child>();
                            JSONArray sub_one = jsonObject.getJSONArray("SubCategories");
                            for (int j = 0; j < sub_one.length(); j++)
                            {
                                JSONObject sub_object = sub_one.getJSONObject(j);
                                ch = new Child();
                                ch.setName(sub_object.getString("Name"));

                                sb_ch_list = new ArrayList<Sub_Child>();
                                JSONArray sub_two = sub_object.getJSONArray("SubCategories");
                                for (int k = 0; k < sub_two.length(); k++)
                                {
                                    JSONObject sub_object_two = sub_two.getJSONObject(k);
                                    sub_child = new Sub_Child();
                                    sub_child.setName(sub_object_two.getString("Name"));
                                    sb_ch_list.add(sub_child);

                                }

                                ch.setItems(sb_ch_list);
                                ch_list.add(ch);

                            }

                            gru.setItems(ch_list);
                            list.add(gru);

                        }

                        ExpAdapter = new ExpandListAdapter(MainActivity.this, list);
                        ExpandList.setAdapter(ExpAdapter);

                        PD.dismiss();

                    }
                }
                catch (JSONException e)
                {

                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener()
        {

            @Override
            public void onErrorResponse(VolleyError error)
            {
                Log.e(TAG, "Error: " + error.getMessage());
                Toast.makeText(getApplication(), "Please Check Internet", Toast.LENGTH_LONG).show();
                PD.dismiss();
            }
        })
{

    @Override
    protected Map<String, String> getParams()
    {
        // Posting parameters to login url
        Map<String, String> params = new HashMap<String, String>();

        return params;
    }

};

// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);

}

public class ExpandListAdapter extends BaseExpandableListAdapter
{

private Context             context;
private ArrayList<Group>    groups;

public ExpandListAdapter(Context context, ArrayList<Group> groups)
{
    this.context = context;
    this.groups = groups;
}

@Override
public Object getChild(int groupPosition, int childPosition)
{
    ch_list = groups.get(groupPosition).getItems();
    return ch_list.get(childPosition);
}

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

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

    SecondLevelExpandableListView secondLevel = new SecondLevelExpandableListView(context);
    secondLevel.setAdapter(new ChildExpandListAdapter(context, ch_list));
    secondLevel.setGroupIndicator(null);
    return secondLevel;

}

@Override
public int getChildrenCount(int groupPosition)
{
    ch_list = groups.get(groupPosition).getItems();
    return ch_list.size();
}

@Override
public Object getGroup(int groupPosition)
{
    return groups.get(groupPosition);
}

@Override
public int getGroupCount()
{
    return groups.size();
}

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
{
    Group group = (Group) getGroup(groupPosition);
    if (convertView == null)
    {
        LayoutInflater inf = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = inf.inflate(R.layout.group_item, null);
    }
    TextView tv = (TextView) convertView.findViewById(R.id.group_name);
    tv.setText(group.getName());
    return convertView;
}

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

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

}

public class SecondLevelExpandableListView extends ExpandableListView
{

public SecondLevelExpandableListView(Context context)
{
    super(context);
}

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    // 999999 is a size in pixels. ExpandableListView requires a maximum
    // height in order to do measurement calculations.
    heightMeasureSpec = MeasureSpec.makeMeasureSpec(999999, MeasureSpec.AT_MOST);
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

}

public class ChildExpandListAdapter extends BaseExpandableListAdapter
{

private Context             context;
private ArrayList<Child>    child;

public ChildExpandListAdapter(Context context, ArrayList<Child> child)
{
    this.context = context;
    this.child = child;
}

@Override
public Object getChild(int groupPosition, int childPosition)
{
    ArrayList<Sub_Child> chList = child.get(groupPosition).getItems();
    return chList.get(childPosition);
}

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

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

    Sub_Child sub_child = (Sub_Child) getChild(groupPosition, childPosition);
    if (convertView == null)
    {
        LayoutInflater Inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = Inflater.inflate(R.layout.sub_child_item, null);
    }

    TextView tv = (TextView) convertView.findViewById(R.id.sub_child);
    tv.setText(sub_child.getName().toString());

    return convertView;
}

@Override
public int getChildrenCount(int groupPosition)
{
    ArrayList<Sub_Child> chList = child.get(groupPosition).getItems();
    return chList.size();
}

@Override
public Object getGroup(int groupPosition)
{
    return child.get(groupPosition);
}

@Override
public int getGroupCount()
{
    return child.size();
}

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

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
{
    Child child = (Child) getGroup(groupPosition);
    if (convertView == null)
    {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.child_item, null);
    }
    TextView tv = (TextView) convertView.findViewById(R.id.country_name);
    tv.setText(child.getName());
    return convertView;
}

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

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

}

}

— modified on Apr 25, 2016 12:40:32 PM