1

TLDR: What I need to do is make multiple asynchronous HTTP requests and use the data to populate a list view -- I'm open to a completely new approach if necessary.

Hi, I'm new to android and I am trying to use Loopj's async http client library. I have implemented the client the way he suggests and I am retrieving the data no problem. My problem is getting the data from my handler's onSuccess back to the data set I want to use for my listview's adapter.

I have done quite a bit of reading; on stackoverflow and other sources. What makes my situation different than most of the solutions I've found is that I have to make multiple http requests before I can set my adapter.

My adapter's data source is an array of a custom type I implemented, ScoutProfile. Side note: everything works fine if I hard code the data set's values.

I have tried setting the adapter before executing the requests with dummy data. Swapping the adapters dataset out for the updated array. Calling notifyDataSetChanged and invalidateViews.

If I don't initialize the array I get a null exception(the changes in onSuccess aren't taking effect?) . If I do instantiate it -- this means I use notifyDataSetChanged -- the UI never gets updated.

I have tried the runOnUIThread method as well.

Here's the code in my fragment:

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.fragment_scoutprofile, container, 
                                                                               false);
    RiotApiClient client = new RiotApiClient();
    profiles = new ScoutProfile[ids.length];

    mListView = (AbsListView) view.findViewById(android.R.id.list);

            final IntWrapper count = new IntWrapper(0);
    for(int j = 0; j < ids.length; j++) {
        final int index = j;
        String url = String.format("/api/lol/na/v2.3/league/by-summoner/%d/entry?", 
                                                                               ids[j]);
        client.get(url, null, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(String r) {
                try {
                    JSONArray response = new JSONArray(r);
                    for (int i = 0; i < response.length(); i++) {
                        JSONObject profile = response.getJSONObject(i);
                        if (profile.get("queueType") == "RANKED_SOLO_5x5") {
                            profiles[index] = new 
    ScoutProfile(profile.getString("playerOrTeamName"), "Silver", "Silver", "n/a", 40);
                            Log.e("ScoutProfileFragment", profiles[index].getName());
                            i = response.length();
                        }
                    }
                    count.integer += 1;
                    Log.e("ScoutProfileFragment", Integer.toString(count.integer));
                    if(count.integer == ids.length)
                    {
                        setUpAdapter(view);
                    }

                } catch (Exception ex) {

                }

            }
        });
    }
    return view;
}

I am certain that the code I didn't include: ScoutProfile.java, ScoutProfileAdapter.java, RiotApiClient, etc are not the problem -- but if anyone requests it I will add it to my question.

Edit: I meant to include this(also in fragment code):

public void setUpAdapter(View view)
{
    Log.e("ScoutProfileFragment", "setUpAdapter");
    mAdapter = new ScoutProfileAdapter(view.getContext(), profiles);
    ((AdapterView<ListAdapter>) mListView).setAdapter(mAdapter);
}
rocktheartsm4l
  • 2,129
  • 23
  • 38

0 Answers0