Gopal Singoriya
Gopal Singoriya

Reputation: 54

How do I add child items dynamically onClick on the parent Items expandable listview in android?

I am developing an Android Application that Contain one Expandable List view for Load data from web service, but I do not want to load whole data on start up but first load parent list then click on the parent list expand particular list item of child of list view load that particular item's child item so first is it possible or not ?

Thank you

Upvotes: 1

Views: 1889

Answers (2)

Manpreet
Manpreet

Reputation: 173

I'm not fully sure what your question means, but if what you want is to dynamically load child items in a BaseExpandableListAdapter e.g. load the "child items" when you click on the relevant group header (as below), I have a solution.

v Group header 1
| -- Child item 1
| -- Child item 2
v Group header 2
| -- Child item 1
| -- Child item 2

In your Activity, find your ExpandableListView like this:

ExpandableListView expandableListView = (ExpandableListView) findViewById(R.id.expandable_list_view);

Then add a listener to check for group clicks:

expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener()
{
    @Override
    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id)
    {
        if (expandableListView.isGroupExpanded(groupPosition))
            expandableListView.collapseGroup(groupPosition);
        else
            new BackgroundTask(groupPosition).execute();

        return true;
    }
});

The return true will "consume" the click and stop the default expansion of the group when you click it. That's why in your ASyncTask, you need to manually expand it. If you return false, the list view will automatically be expanded in the GUI, and you won't see the results of your background computation until you collapse and re-expand the group.

In my code, I had a SparseBooleanArray to track whether the data had already been set for a given group, and I checked that value in the onGroupClick() method; I did this so that the data would only have to be loaded once when a group was expanded, but obviously, if you want the data to change on each expansion, you wouldn't want this. Your question was rather vague, so I'm not sure what the exact scenario is for your usage.

private class BackgroundTask extends AsyncTask<Void, Void, List<ReturnType>>
{
    int groupPosition;

    private BackgroundTask(int groupPosition)
    {
        this.groupPosition = groupPosition;
    }

    @Override
    protected List<ReturnType> doInBackground(String... params)
    {
        // TODO: Do your background computation...
        // Then return it
    }

    @Override
    protected void onPostExecute(List<ReturnType> resultsList)
    {
        super.onPostExecute(resultsList);

        // TODO: Add the resultsList to whatever structure you're using to store the data in the ListView Adapter
        expandableListView.expandGroup(groupPosition);
    }
}

I'm not sure if this is the best way to do it, but it certainly worked for me.

Upvotes: 2

paul_hundal
paul_hundal

Reputation: 381

I would need to know what the api looks like. Whatever this "parent" data is, could is come from some api? And could you retrieve this "child" data from another api call?

One way it could be done is:

  1. Make an api call -> getParentList() that retrieves all elements that go in your parent.
  2. Bind this list in your adapter as parent values
  3. onClick of each parent, make another api that returns the child element of that parent i.e -> getChildOfParent(int parent_id) that returns info of that particular parent.

Things to be aware of

Remember, when binding to the UI you should have some sort of strategy. How big is this list? Will it need to be paginated? How will you handle that pagination? How/where will you cache your list of items? What is the user experience supposed to look like?

These are all things, plus a lot more, to think about.

Some things you should look into for this are retrofit/okhttp, sqlite/realm, Picasso (in case you want to expand images), CursorAdapter (pagination).

Upvotes: 0

Related Questions