Android Volley 将 ArrayList 转换为 CharSequence 返回空
Android Volley Converting ArrayList to CharSequence Returning Empty
在onCreate
方法中,我有一个网络操作就是把returnJSON解析成ArrayLists,设置成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
填充数据?
How can I get the nested AlertDialog to be populated with the data?
调用 addAll
和 notifyDataSetChanged
更新 AlertDialog
数据:
@Override
public void onResponse(JSONObject response) {
// Parse the JSON:
...
ListView list = openFriendsAlert.getListView();
ArrayAdapter adapter = (ArrayAdapter)list.getAdapter();
adapter.addAll(groupNameList);
adapter.notifyDataSetChanged();
}
在onCreate
方法中,我有一个网络操作就是把returnJSON解析成ArrayLists,设置成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
填充数据?
How can I get the nested AlertDialog to be populated with the data?
调用 addAll
和 notifyDataSetChanged
更新 AlertDialog
数据:
@Override
public void onResponse(JSONObject response) {
// Parse the JSON:
...
ListView list = openFriendsAlert.getListView();
ArrayAdapter adapter = (ArrayAdapter)list.getAdapter();
adapter.addAll(groupNameList);
adapter.notifyDataSetChanged();
}