1

I am new to android and currently i'm confronted with a problem i can't solve.

I have an ExpandableListView with a custom adapter which extends the BaseExpandableListAdapter. Only the group data is loaded when the ExpandableListView is initiated. The corresponding child data is loaded when I click on the group.

The group data holds an object. When I click on a group I get the ID of the groups object. I then pass this ID to a database procedure. The database procedure then returns the corresponding child data for that particular group. I perform this database call inside of an AsyncTask.

For the click I use a ExpandableListView.OnGroupClickListener.

And here is my problem:

When I perform the click, the group expands before the child data is returned by the database procedure. When I collapse the group right after I first expanded it, and then expand it again, the child data is displayed. So basically what I found is, that there is a timing problem. The AsyncTask is not finished before the group is expanded.

My preferrable solution:

The group should not be expanded before the child data is returned from the database procedure (So basically the group should not be expanded while the AsyncTask is doint its work). Or the group should automatically refresh itself after the AsyncTask is finished, without collapsing, and display the child data.

Here is my code:

The onclick event!

lv.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
@Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {

    selected = (DoitService) lv.getExpandableListAdapter().getGroup(groupPosition);
    servId = selected.getServiceIdService();
    if (!isConnected) {
        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getParentActivity());

        // set title
        alertDialogBuilder.setTitle("MyApp");
        alertDialogBuilder.setMessage("Your Internet Connection is not sufficient to perform this task ")
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
    } else {


        AsyncCallIndividualService asyncCallIndividualService = new AsyncCallIndividualService();
        asyncCallIndividualService.execute(servId);

    }

    return false;
}
});

The onPostExecute code of AsyncCallIndividualService (m1String is the new child data which is returned by the database procedure):

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);

        List<String> childData = new ArrayList<String>();
               childData.add(m1String);
               Log.d("m1String on Expand: ", m1String);
        // Add children as an array to the HashMap
              ExpandableServiceListAdapter. _listDataChild.put(selected, childData);

    }


}

PS: I already tried the onGroupExpanded method from the BaseExpandableListAdapter. I had the same problem with it.

I'm really grateful for every help! Thanks

0 Answers0