Android Volley将ArrayList转换为CharSequence返回空

时间:2015-02-11 02:42:31

标签: android json arraylist android-volley charsequence

onCreate方法中,我有一个网络操作,它将返回被解析为ArrayLists的JSON,设置为CharSequence[]数组并用于警告对话框。

但是,选中此选项后,警报不会填充生成的JSON数据。到目前为止,我知道Volley实现工作并且数据被传递到ArrayLists。失败的区域是将ArrayList数据传递到CharSequence[]数组,然后传递给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();
        }
    });

我认为失败与网络操作完成和填充AlertDialog的时间有关。

如何让嵌套的AlertDialog填充数据?

1 个答案:

答案 0 :(得分:1)

  

如何使用数据填充嵌套的AlertDialog?

致电addAllnotifyDataSetChanged了解更新AlertDialog数据:

@Override
public void onResponse(JSONObject response) {

 // Parse the JSON:
 ...  
 ListView list = openFriendsAlert.getListView();               
 ArrayAdapter adapter = (ArrayAdapter)list.getAdapter();
 adapter.addAll(groupNameList); 
 adapter.notifyDataSetChanged();
}