2014-12-27 115 views
0

你好,我正在構建一個應用程序,它使用來自mysql的數據填充listview。圖片無法從url加載

問題是,除圖像外,所有其他數據都正在顯示。我用這個方法來從URL獲取圖像

public Bitmap getBitmapFromUrl(String src) { 
     try { 
      URL url = new URL(src); 
      HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 
      connection.setDoInput(true); 
      connection.connect(); 
      InputStream inputStream = connection.getInputStream(); 
      Bitmap myBitmap = BitmapFactory.decodeStream(inputStream); 
      return myBitmap; 
     } catch (Exception e) { 
      e.printStackTrace(); 
      return null; 
     } 
    } 

然後我適配器使用setImageBitmap像這樣

ImageView stockImage = (ImageView)stockView.findViewById(R.id.imagestartinglist); 
      stockImage.setImageBitmap(current.getStockImage()); 

的ImageView的,我從JSON獲得圖像這樣

public void ListDrawer() { 
     customList = new ArrayList<StockList>(); 
     try { 
      JSONObject jsonResponse = new JSONObject(jsonResult); 
      JSONArray jsonMainNode = jsonResponse.optJSONArray("metoxes"); 
      for (int i = 0; i < jsonMainNode.length(); i++) { 
       JSONObject jsonChildNode = jsonMainNode.getJSONObject(i); 
       String name = jsonChildNode.optString("name"); 
       String price = jsonChildNode.optString("price"); 
       String price1 = jsonChildNode.optString("price1"); 
       String price2 = jsonChildNode.optString("price2"); 
       String price3 = jsonChildNode.optString("price3"); 
       String price4 = jsonChildNode.optString("price4"); 
       String price5 = jsonChildNode.optString("price5"); 
       String price6 = jsonChildNode.optString("price6"); 
       String price7 = jsonChildNode.optString("price7"); 
       String price8 = jsonChildNode.optString("price8"); 
       String price9 = jsonChildNode.optString("price9"); 
       String price10 = jsonChildNode.optString("price10"); 
       String price11 = jsonChildNode.optString("price11"); 
       String price12 = jsonChildNode.optString("price12"); 
       String price13 = jsonChildNode.optString("price13"); 
       String price14 = jsonChildNode.optString("price14"); 
       String price15 = jsonChildNode.optString("price15"); 

       String image = jsonChildNode.optString("image"); 

       Bitmap bmp = getBitmapFromUrl(image); 

       loipesTimes = new String[]{price1, price2, price3, price4, price5, price6, price7, price8, price9, 
         price10, price11, price12, price13, price14, price15}; 
       customList.add(new StockList(name, price, bmp, loipesTimes)); 
      } 
     } catch (Exception e) { 
      Intent intent1 = new Intent(ListLoaderActivity.this, 
        RefreshActivity.class); 
      startActivity(intent1); 
      ListLoaderActivity.this.finish(); 
     } 

     ArrayAdapter adapter = new MyStocksAdapter(ListLoaderActivity.this, R.layout.list_item, customList); 
     adapter.notifyDataSetChanged(); 
     startList.setAdapter(adapter); 
    } 

我的PHP文件從MySQL數據庫檢索數據是Here

任何想法?提前致謝!!!

+0

這json看起來很好,你如何閱讀網址看起來也很好。我沒有檢查圖像下載代碼,但我只是希望你知道你做了什麼。其餘的看起來也很好。那麼你有沒有得到任何logcat輸出? – rekire 2014-12-27 19:26:38

+0

以及我從這個傢伙跟着這個例子,它看起來很好,但我不知道什麼是錯的 https://www.youtube.com/watch?v=KmYJBhz1gmk – 2014-12-27 19:29:33

回答

0

我終於得到這個打算使用這個庫從This Guy

Modyfing我適配器像

ImageView stockImage = (ImageView)stockView.findViewById(R.id.imagestartinglist); 
      Ion.with(stockImage).placeholder(R.drawable.ic_launcher_stock_custom_icon).error(R.drawable.ic_launcher_stock_custom_icon).load(current.getStockImage()); 

我加載數據,這樣的方法:

public void ListDrawer() { 
     customList = new ArrayList<StockList>(); 
     try { 
      JSONObject jsonResponse = new JSONObject(jsonResult); 
      JSONArray jsonMainNode = jsonResponse.optJSONArray("metoxes"); 
      for (int i = 0; i < jsonMainNode.length(); i++) { 
       JSONObject jsonChildNode = jsonMainNode.getJSONObject(i); 
       name = jsonChildNode.optString("name"); 
       price = jsonChildNode.optString("price"); 
       price1 = jsonChildNode.optString("price1"); 
       price2 = jsonChildNode.optString("price2"); 
       price3 = jsonChildNode.optString("price3"); 
       price4 = jsonChildNode.optString("price4"); 
       price5 = jsonChildNode.optString("price5"); 
       price6 = jsonChildNode.optString("price6"); 
       price7 = jsonChildNode.optString("price7"); 
       price8 = jsonChildNode.optString("price8"); 
       price9 = jsonChildNode.optString("price9"); 
       price10 = jsonChildNode.optString("price10"); 
       price11 = jsonChildNode.optString("price11"); 
       price12 = jsonChildNode.optString("price12"); 
       price13 = jsonChildNode.optString("price13"); 
       price14 = jsonChildNode.optString("price14"); 
       price15 = jsonChildNode.optString("price15"); 

       image = jsonChildNode.optString("image"); 

       loipesTimes = new String[]{price1, price2, price3, price4, price5, price6, price7, price8, price9, 
         price10, price11, price12, price13, price14, price15}; 
       customList.add(new StockList(name, price, image, loipesTimes)); 

      } 
     } catch (Exception e) { 
      Intent intent1 = new Intent(ListLoaderActivity.this, 
        RefreshActivity.class); 
      startActivity(intent1); 
      ListLoaderActivity.this.finish(); 
     } 

     ArrayAdapter adapter = new MyStocksAdapter(ListLoaderActivity.this, R.layout.list_item, customList); 
     adapter.notifyDataSetChanged(); 
     startList.setAdapter(adapter); 
    } 

以及每個項目應看起來像這樣:

public StockList(String stockCurrentName, String stockCurrentPrice, String stockImage, String[] restPrices) { 
     this.stockCurrentName = stockCurrentName; 
     this.stockCurrentPrice = stockCurrentPrice; 
     this.stockImage = stockImage; 
     this.restPrices = restPrices; 
    } 

而不是我在構造函數中使用的位圖,我現在使用一個字符串,它的作用就像一個魅力!