Home > Mobile >  About the android callback function to handle problems in a for loop
About the android callback function to handle problems in a for loop

Time:10-07

In a loop access network to obtain the Json data, parse the Json data in the callback function and give the adapter assignment, display the data in the RecycleView, found that a data, displaying only the last four data, also is not show tracking within the callback function, found that performed only once, what should I do?
Code:
for (int i=0; I & lt; 4. I + +) {
OneNetApi. SetAppKey (KeyList. Get (I));
OneNetApi. QuerySingleDevice (IDList. Get (I), the new OneNetApiCallback () {//callback function
@ Override
Public void onSuccess (String response) {
JsonObject resp=new JsonParser (). The parse (response). GetAsJsonObject ();
Int errno=resp. Get (" errno). GetAsInt ();
If (0==errno) {
MDeviceItems. Add (resp. Get (" title "). GetAsString ());
MAdapter. SetNewData (mDeviceItems);
}
}

@ Override
Public void onFailed (Exception e) {
.
}

CodePudding user response:

This must have 4 times request, you can print a 4 times returned as a result, you setNewData adapter (mDeviceItems) method of how to write, I doubt you is direct assignment, not the add

CodePudding user response:

Request is four times that's right, but the request is performed for the main thread, basic no time interval, so at the time of the query, a look have had a request directly cancel the last request, so they will only have one last request, I don't understand why a list of data need to request four times, this is bad design, have to write this way, can increase the interval of the request, such as a request over to start on the second, the method has a lot of kinds, counter, what variables can be,
  • Related