2015-02-11 57 views
-1

onCreate方法中,我有一個網絡操作,用於返回JSON並將其解析爲ArrayLists,並將其設置爲CharSequence[]陣列並用於警報對話框。Android Volley將ArrayList轉換爲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

如何獲取嵌套的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(); 
} 
+0

如何使用'openFriendsAlert.getListView();'甚至被宣佈AlertDialog內之前呢? – Sauron 2015-02-11 03:30:41

+0

@Sauron:全局聲明'openFriendsAlert'變量,而不是方法 – 2015-02-11 03:50:07

+0

@Sauron:像全局移動'AlertDialog openFriendsAlert;'行並使用'openFriendsAlert = openFriends.create();'在'inviteFriend'按鈕中點擊 – 2015-02-11 03:51:45