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
任何想法?提前致謝!!!
這json看起來很好,你如何閱讀網址看起來也很好。我沒有檢查圖像下載代碼,但我只是希望你知道你做了什麼。其餘的看起來也很好。那麼你有沒有得到任何logcat輸出? – rekire 2014-12-27 19:26:38
以及我從這個傢伙跟着這個例子,它看起來很好,但我不知道什麼是錯的 https://www.youtube.com/watch?v=KmYJBhz1gmk – 2014-12-27 19:29:33