2013-07-15 42 views
0

即時嘗試解剖此json工作表下面和源代碼probem是文本將顯示,但圖像,我嘗試從json表單中的url加載但圖像不只顯示空白scrreen顯示se這圖像http://imgur.com/hsYqtyd圖像現在顯示在屏幕上時,json解析

{ 
    "worldpopulation": 
    [ 
     { 
      "rank":1, 
      "name": "BREAKFAST", 
      "url": "http://www.androidbegin.com/tutorial/flag/china.png" 
     }, 
     { 
      "rank":2, 
      "name": "LUNCH ", 
      "url": "http://www.androidbegin.com/tutorial/flag/china.png" 
     }, 
     { 
      "rank":3, 
      "name": "SUPPER", 
      "url": "http://www.androidbegin.com/tutorial/flag/china.png" 
     } 
    ] 
} 

public void parseJSONData(){ 
    //CategoryAPI = Utils.CategoryAPI+"?accesskey="+Utils.AccessKey;  
    CategoryAPI = Utils.CategoryAPI;   
    clearData(); 
    try {    
     HttpClient client = new DefaultHttpClient(); 
     HttpConnectionParams.setConnectionTimeout(client.getParams(), 15000); 
     HttpConnectionParams.setSoTimeout(client.getParams(), 15000); 
     HttpUriRequest request = new HttpGet(CategoryAPI); 
     HttpResponse response = client.execute(request); 
     InputStream atomInputStream = response.getEntity().getContent(); 
     BufferedReader in = new BufferedReader(new 
     InputStreamReader(atomInputStream)); 

     String line; 
     String str = ""; 
     while ((line = in.readLine()) != null) { 
      str += line; 
     }  

     JSONObject json = new JSONObject(str); 
     JSONArray data = json.getJSONArray("worldpopulation"); 

     for (int i = 0; i < data.length(); i++) { 
      JSONObject object = data.getJSONObject(i);     
      //JSONObject category = object.getJSONObject("Category");      
      Category_ID.add(Long.parseLong(object.getString("rank"))); 
      Category_name.add(object.getString("name")); 
      Category_image.add(object.getString("url")); 
      Log.d("Category name", Category_name.get(i)); 

     }       
    } catch (MalformedURLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     IOConnect = 1; 
     e.printStackTrace(); 
    } catch (JSONException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

class CategoryListAdapter extends BaseAdapter { 
    private Activity activity; 
    private ImageLoader imageLoader; 

    public CategoryListAdapter(Activity act) { 
     this.activity = act; 
     imageLoader = new ImageLoader(act); 
    } 

    public int getCount() { 
     // TODO Auto-generated method stub 
     return CategoryList.Category_ID.size(); 
    } 

    public Object getItem(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    public long getItemId(int position) { 
     // TODO Auto-generated method stub 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     // TODO Auto-generated method stub 
     ViewHolder holder; 

     if(convertView == null) { 
      LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.category_list_item, null); 
      holder = new ViewHolder(); 

      convertView.setTag(holder); 
     } else { 
      holder = (ViewHolder) convertView.getTag(); 
     } 


     holder.txtText = (TextView) convertView.findViewById(R.id.txtText); 
     holder.imgThumb = (ImageView) 
     convertView.findViewById(R.id.imgThumb); 

     holder.txtText.setText(CategoryList.Category_name.get(position)); 

     imageLoader.DisplayImage(Utils.AdminPageURL+CategoryList.Category_image.get(position), activity, holder.imgThumb); 

     return convertView; 
    } 

    static class ViewHolder { 
     TextView txtText; 
     ImageView imgThumb; 
    }   
} 
+0

如果可以的話,發佈'imageLoader'的代碼將會非常有用。 –

回答

0

你要下載的圖像,並顯示在圖像視圖

0

你沒有張貼imageLoader.DisplayImage()代碼,但我覺得這是你的形象和設置的下載到ImageView的。

從附加的圖像中,我想說在通過setImage()或setBackground()將照片添加到ImageView後,您需要在ImageView上調用invalidate()

+0

在ImageView上調用'setImageDrawable()'或任何這些類型的方法都會使其自身失效,所以不需要再次調用'invalidate()'。 –