2012-05-16 67 views
0

我有一個惰性列表,加載在圖像列表中,此時這些網址是用String []硬編碼的,我想通過json feed加載它們。所以我的問題是如何從JsonObject創建一個字符串[]?Android從Json Feed創建一個String []

繼承人什麼Ive得到了迄今爲​​止

try { 
     post.setEntity (new UrlEncodedFormEntity(pairs)); 
     HttpResponse response = client.execute(post); 
     BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8")); 
     String json = reader.readLine(); 
     fulldata = String.valueOf(json); 
     Log.v("myApp","newsdata" + fulldata); 

     newsList = new ArrayList<String>(); 
     newsList2 = new ArrayList<String>(); 
     newsList3 = new ArrayList<String>(); 



     JSONObject obj = new JSONObject(json);  
     JSONObject objData = obj.getJSONObject("data"); 
     JSONArray jArray = objData.getJSONArray("news"); 


      for(int t = 0; t < newsAmount; t++){ 
       JSONObject newsTitleDict = jArray.getJSONObject(t); 

//this is where i want to load the images into a String[] 
JSONArray ImageArray = objData.getJSONArray("news"); 

      newsList3.add(newsTitleDict.getString("title")); 

      } 

      for(int t = 0; t < 1; t++){ 
       JSONObject newsTitleDict = jArray.getJSONObject(t); 

      newsList.add(newsTitleDict.getString("title")); 
      newsList2.add(newsTitleDict.getString("title")); 

      } 



    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 




    arrayAdapter = new ArrayAdapter<String>(this, R.layout.single_item, newsList); 
    arrayAdapter2 = new ArrayAdapter<String>(this, R.layout.single_item, newsList2); 
    //arrayAdapter3 = new ArrayAdapter(this, R.layout.complex_item, newsList3); 



    String[] mStrings={ 
      "http://a3.twimg.com/profile_images/670625317/aam-logo-v3-twitter.png", 
      "http://a3.twimg.com/profile_images/740897825/AndroidCast-350_normal.png", 
      "http://a3.twimg.com/profile_images/121630227/Droid_normal.jpg", 
      "http://a1.twimg.com/profile_images/957149154/twitterhalf_normal.jpg", 
      "http://a1.twimg.com/profile_images/97470808/icon_normal.png", 


    }; 



    arrayAdapter3 = new LazyAdapter(this, mStrings); 


     ListView list = getListView(); 
      list.setTextFilterEnabled(true); 

      LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      View header = inflater.inflate(R.layout.homeheader, list, false); 
      View header2 = inflater.inflate(R.layout.homeheader2, list, false); 
      View header3 = inflater.inflate(R.layout.homeheader3, list, false); 



     adapter = new MergeAdapter(); 
     adapter.addView(header); 
     adapter.addAdapter(arrayAdapter); 
     adapter.addView(header2); 
     adapter.addAdapter(arrayAdapter2); 
     adapter.addView(header3); 
     adapter.addAdapter(arrayAdapter3); 
     setListAdapter(adapter); 

    } 

回答

0

這是我如何使用從一個JSONObject一個ArrayList添加值的String []。我的例子使用JSONArray,但你可以隨時改變它以適合你的代碼。

ArrayList<String> arrayName = new ArrayList<String>(); 
ArrayList<String> arrayPicture = new ArrayList<String>(); 

Bundle extras = getIntent().getExtras(); 
apiResponse = extras.getString("API_RESPONSE"); 

try { 

    JSONArray JAFriends = new JSONArray(apiResponse); 

    for (int i = 0; i < JAFriends.length(); i++) { 
     json_data = JAFriends.getJSONObject(i); 


     if (json_data.has("name")) { 
      String getFriendName = json_data.getString("name").toUpperCase(); 
      arrayName.add(getFriendName); 
     } else { 
      String getFriendName = null; 
      arrayName.add(getFriendName); 
     }  

     if (json_data.has("pic_square")) { 
      String getFriendPhoto = json_data.getString("pic_square"); 
      arrayPicture.add(getFriendPhoto); 
     } else { 
      String getFriendPhoto = null; 
      arrayPicture.add(getFriendPhoto); 
     } 
    } 
} catch (JSONException e) { 
    return; 
} 
stringName = new String[arrayName.size()]; 
stringName = arrayName.toArray(stringName); 

stringPicture = new String[arrayPicture.size()]; 
stringPicture = arrayPicture.toArray(stringPicture); 

listofFriends = (ListView)findViewById(R.id.list); 
adapter = new FriendsAdapter(this, stringName, stringPicture); 

listofFriends.setAdapter(adapter); 

在他for循環,使用如果表,以確保沒有錯誤蠕變,返回的值被添加到它們各自的ArrayList和6行的代碼的adapter之前被設定,從所述的ArrayList的值是添加到String []中。 希望這有助於解決你的問題。