Search code examples
androidjsonarraylistandroid-volleycharsequence

Android Volley Converting ArrayList to CharSequence Returning Empty


In the onCreate method, I have a network operation that is to return JSON that is parsed into ArrayLists, to be set into a CharSequence[] array and used in an alert dialog.

However, when selected, the alert does not populate with the resulting JSON data. Thus far, I know the Volley implementation works and the data is passed into the ArrayLists. The area of failure is passing the ArrayList data into the CharSequence[] array that is then to be passed into the AlertDialog:

    // Initialize Volley:
    final RequestQueue requestQueue = VolleySingleton.getsInstance().getRequestQueue();

    getUsersGroupsURL = usersGroupsActionURL + hardUserName + JSON;

    JsonObjectRequest getGroupData = new JsonObjectRequest(Request.Method.GET, getUsersGroupsURL, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {

                    // Parse the JSON:
                    try {
                        groupResults = response.getJSONArray("users_groups");

                        for (int i = 0; i <= groupResults.length() - 1; i++) {
                            JSONObject oneGroup = groupResults.getJSONObject(i);
                            groupNameList.add(oneGroup.getString("groupName"));
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d("Error.Response", error.toString());
                }
            }
    );

    requestQueue.add(getGroupData);

    groupNames = groupNameList.toArray(new CharSequence[groupNameList.size()]);

    inviteFriend.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {


            final CharSequence[] items = {
                    "Share an Idea", "Share a story"
            };

            AlertDialog.Builder builder = new AlertDialog.Builder(SingleSpecial.this);
            builder.setTitle(R.string.sharing_type);
            builder.setItems(items, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int selection) {

                    if (selection == 0) {

                        // Create the lists of groups and friends


                        // If the first, go to the selection of friend/group
                        AlertDialog.Builder openFriends = new AlertDialog.Builder(SingleSpecial.this);
                        openFriends.setTitle(R.string.sharing_type);
                        openFriends.setItems(groupNames, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int selection) {


                            }
                        });

                        AlertDialog openFriendsAlert = openFriends.create();
                        openFriendsAlert.show();

                    } else if (selection == 1) {
                        // If the second, select location, then the friend/group


                    }
                }
            });
            AlertDialog alert = builder.create();
            alert.show();
        }
    });

I think the failure has to do with the time for the network operation to finish and the populating of the AlertDialog.

How can I get the nested AlertDialog to be populated with the data?


Solution

  • How can I get the nested AlertDialog to be populated with the data?

    Call addAll and notifyDataSetChanged for update AlertDialog data :

    @Override
    public void onResponse(JSONObject response) {
    
     // Parse the JSON:
     ...  
     ListView list = openFriendsAlert.getListView();               
     ArrayAdapter adapter = (ArrayAdapter)list.getAdapter();
     adapter.addAll(groupNameList); 
     adapter.notifyDataSetChanged();
    }