1

大家好,我正在尝试显示我从包含名称、图像和内容(textview、imageview 和 textview)的 JSON 字符串中获取的数据。我能够从 JSON 中获取数据,但我无法显示它,甚至我的 Logcat 也没有出现任何错误。

我正在尝试的代码是

try {
     bookDetails = json.getJSONArray(TAG_DETAILS);
     for (int i = 0; i < bookDetails.length(); i++) 
     {
     JSONObject c = bookDetails.getJSONObject(i);
     String image1 = c.getString(TAG_IMAGE1);
     String image2=c.getString(TAG_IMAGE2);
     String Bid = c.getString(TAG_ID);
     String Bname = c.getString(TAG_NAME);
     String Bdesc= c.getString(TAG_DESCRIPTION);
     String BbookId=c.getString(TAG_BOOKID);
     HashMap<String, String> map = new HashMap<String, String>();
     map.put(TAG_IMAGE1, image1);
     map.put(TAG_IMAGE2, image2);
     map.put(TAG_ID,Bid);
     map.put(TAG_NAME,Bname);
     map.put(TAG_DESCRIPTION,Bdesc);
     map.put(TAG_BOOKID,BbookId);
     booksList.add(map);
     String[] captionArray = (String[]) ((List<NameValuePair>) map).toArray(new String[map.size()]);
     ItemsAdapter itemsAdapter=new ItemsAdapter(DetailsActivity.this, R.layout.details,booksList);
     setListAdapter(itemsAdapter);
     }
       } catch (JSONException e)
         {
             e.printStackTrace();
          }
            return null;
   }

我的适配器是

 private class ItemsAdapter extends BaseAdapter implements ListAdapter
        {
            String[] items;


            public ItemsAdapter(DetailsActivity detailsActivity,
                    int details, ArrayList<HashMap<String, String>> booksList) {
                // TODO Auto-generated constructor stub
                this.items = items;
            }
            public View getView( int POSITION, View convertView,ViewGroup parent)
            {
                TextView cap = null ;
                View view = convertView;
                ImageView img = null;
                if (view == null) 
                {
                    LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    view = vi.inflate(R.layout.details, null);
                }
                img = (ImageView) view.findViewById(R.id.smallImage);
                Log.i("sss","sss");
                cap = (TextView) view.findViewById(R.id.caption);
                cap.setText(mapName.get(POSITION));
                img.setImageBitmap(BitmapFactory.decodeByteArray(map.get(POSITION), 0, map.get(POSITION).length));
                return view;
            }
            public int getCount() 
            {
                return items.length;
            }
            public Object getItem(int position) 
            {
                return position;
            }
            public long getItemId(int position) 
            {
                return position;
            }
        }

我的 JSON 字符串是

{
    "bookdetails": [
        {
            "bid": "2",
            "name": "Android 2 Application Dev",
            "bookid": "1",
            "description": "Providing in-depth coverage of how to build mobile applications using the next major release of the Android SDK, this invaluable resource takes a hands-on approach to discussing Android with a series of projects, each of which introduces a new feature and highlights techniques and best practices to get the most out of Android.",
            "image1": "http://media.shelf-awareness.com/shelfcontest.png",
            "image2": "http://cache0.bdcdn.net/assets/images/book/medium/9780/4704/9780470452622.jpg"
        }
    ]
}
4

1 回答 1

0

你错过了 JsonObject 和 JsonArray,就用这个

JSONObject mJsonObject=null;
JSONArray bookDetails = null;
try {
    mJsonObject= new JSONObject("YourJson");
    bookDetails = mJsonObject.getJSONArray("bookdetails");
} catch (JSONException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}

try {
    System.out.println("bookDetails.length() : "+bookDetails.length());
    for (int i = 0; i < bookDetails.length(); i++) 
    {
        JSONObject c = bookDetails.getJSONObject(i);

        String image1 = c.getString("image1");
        String image2=c.getString("image2");
        String Bid = c.getString("bid");
        String Bname = c.getString("name");
        String Bdesc= c.getString("description");
        String BbookId=c.getString("bookid");
        }
} catch (JSONException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
于 2012-11-21T11:59:03.900 回答