0

我有一个惰性列表加载图像列表,此时 url 被硬编码在 String[] 中,我想通过 json 提要加载它们。所以我的问题是如何从 JsonObject 创建一个字符串 []?

这是我到目前为止所得到的

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);

    }   
4

1 回答 1

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的循环中,使用if语句确保没有错误蔓延,返回的值被添加到它们各自的 ArrayLists 和adapter设置之前的 6 行代码,来自 ArrayLists 的值被添加到 String[]。希望这有助于解决您的问题。

于 2012-05-16T10:37:57.857 回答