2016-02-25 69 views
0

即時通訊新的Android,這似乎是沒有理由的過於複雜。Android的 - 從ListView ImageView中的位置圖像從JSON API調用

我有一個simpleadapter將數據添加到列表視圖與5值。其中之一是圖片的網址。我只是希望imageview將其作爲源代碼。

繼承人的JSON請求

private void makeJsonArrayRequest() { 

     showpDialog(); 

     JsonArrayRequest req = new JsonArrayRequest(urlJsonArry, 
       new Response.Listener<JSONArray>() { 
        @Override 
        public void onResponse(JSONArray response) { 
         Log.d(TAG, response.toString()); 

         try { 
          // Parsing json array response 
          // loop through each json object 
          jsonResponse = ""; 
          for (int i = 0; i < response.length(); i++) { 

           JSONObject dealObject = (JSONObject) response.get(i); 

           String dealTitle = dealObject.getString("title"); 
           int dealPrice = dealObject.getInt("price"); 
           String brandLogo = dealObject.getString("brandlogo"); 
           int dealVotes = dealObject.getInt("votes"); 
           String dealRetailer = dealObject.getString("brand"); 

           ImageView thumbnail = (ImageView) findViewById(R.id.brandlogo); 

           jsonResponse += "title: " + dealTitle + "\n\n"; 
           jsonResponse += "price: " + dealPrice + "\n\n"; 
           jsonResponse += "brandlogo: " + brandLogo + "\n\n"; 
           jsonResponse += "votes: " + dealVotes + "\n\n\n"; 
           jsonResponse += "retailer: " + dealRetailer + "\n\n\n\n\n"; 

           HashMap<String, String> map = new HashMap<String, String>(); 

           map.put("1", dealTitle); 
           map.put("2", "€" + dealPrice+".00"); 
           map.put("3", brandLogo); 
           map.put("4", dealVotes+""); 
           map.put("5", dealRetailer); 

           oslist.add(map); 
           list=(ListView)findViewById(R.id.listView); 

           ListAdapter adapter = new SimpleAdapter(MainActivity.this, oslist, 
             R.layout.list_item_1, 
             new String[] { "1","2","3","4","5"}, new int[] { 
             R.id.title, 
             R.id.price, 
             R.id.brandlogo, 
             R.id.votes, 
             R.id.retailer 
           }); 





        list.setAdapter(adapter); 


           list.setOnItemClickListener(new AdapterView.OnItemClickListener() { 

            @Override 
            public void onItemClick(AdapterView<?> parent, View view, 
                  int position, long id) { 
             Toast.makeText(MainActivity.this, "You Clicked at "+oslist.get(+position).get("1"), Toast.LENGTH_SHORT).show(); 

            } 
           }); 

          } 

         } catch (JSONException e) { 
          e.printStackTrace(); 
          Toast.makeText(getApplicationContext(), 
            "Error: " + e.getMessage(), 
            Toast.LENGTH_LONG).show(); 
         } 

         hidepDialog(); 
        } 
       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       VolleyLog.d(TAG, "Error: " + error.getMessage()); 
       Toast.makeText(getApplicationContext(), 
         error.getMessage(), Toast.LENGTH_SHORT).show(); 
       hidepDialog(); 
      } 
     }); 

     // Adding request to request queue 
     VolleyAdapter.getInstance().addToRequestQueue(req); 
    } 

有沒有簡單的方法來簡單地添加「brandlogo」到「縮略圖」?

謝謝

回答

1

如果你需要一個快速簡便的方法來設置只使用URL在觀看圖片,然後凌空有內置的爲您解決。

與com.android.volley.toolbox.NetworkImageView替換您的ImageView,你可以很容易地設置圖像只有兩行代碼:

NetworkImageView:

<com.android.volley.toolbox.NetworkImageView 
    android:id="@+id/brandlogo" 
    android:layout_width="150dp" 
    android:layout_height="170dp" 
    android:layout_centerHorizontal="true" /> 

在您的代碼:

NetworkImageView logo = (NetworkImageView)view.findViewById(R.id.brandlogo); 
    logo.setImageUrl("http://YourURL",mImageLoader); 

查看Android documentation

+1

這可以放在list.setAdapter(適配器)之前;或我在哪裏添加它?謝謝 – ChrisM

+0

最好的地方可能會在你的適配器的getView()。看到[這個例子](http://stackoverflow.com/questions/23142734/how-to-use-networkimageview-in-a-listview) – SoroushA

+0

我必須做錯了什麼,因爲在VolleyAdapter中沒有getView。我也使用SimpleAdapter。 – ChrisM

0

你不能直接添加圖像url到imageview。首先你必須下載該圖像,然後在imageview中設置它。要下載圖像並將其設置爲imageview,您可以使用Glide或Picasso等Image Loader庫。 這裏是鏈接滑翔

Glide Image Loading library

,並就如何使用這個庫按照給出的鏈接教程。

希望這將有助於

相關問題