There is a list of countries, it is static (Russia, China, USA, etc.), each country has a list of cities. I use ExpandableListView with a custom Adapter: The list of countries is assigned as a Group , and the list of cities must be loaded from the server dynamically and crammed into Child

Question. How from Adapter to make request for the server and to use in expandableListView ?

Ps Without Expandable - not an option. For requests I use Retrofit.

  • Some kind of abstract question. In the implementation of the adapter, you can make AsyncTask and AsyncTask to the server. Then in publishProgress push it to the list - Herrgott
  • The request to the server should be sent when the user clicked on the group element. The method getChild . But in the time when AsyncTask has completed, the method will already return null. - Alexey
  • Did not understand, what exactly returns null ? Why will return? - Herrgott
  • 2
    Why two almost identical questions? - pavlofff
  • @pavlofff, closed as a duplicate to this question. It is strange that the system gave it to do even in the absence of pluses on the answer here) - YuriySPb ♦

1 answer 1

  1. The launch of the request to the server should not occur in the method of drawing an element of the adapter, but in the listener method of clicking on the element-group.

  2. Next, run the task of downloading information in this place and get the answer, you need to add the data to the list displayed by the adapter.

  3. After that, notify the adapter of the changes by notifyDataSetChanged() method notifyDataSetChanged()

  • That's the problem. That requests cannot be sent in the main thread. There is a method OnGroupClick, if you start a stream with a connection in it, then until the answer comes the application already throws the exception. Can I somehow suspend the main thread? - Alexey
  • @ Alexey, if there is no data at the moment of clicking on a group, then the adapter should not attempt to display something and, accordingly, fall. And you should not put the main stream to sleep - there is a risk to get a drop by ANR - "the application is not responding." In general, you apparently have written something wrong in the adapter. - Yuriy SPb ♦
  • You were right. Thank! - Alexey