2016-07-18 101 views
0

我試圖從.net web服務中獲取數據。我得到像this這樣的json響應。但我無法獲取圖像並將其設置爲圖像視圖。我做的代碼如下。給解決方案如何從數據庫中獲取圖像並在listview中顯示它們?

public class DisplayList extends ListActivity { 

    private static final String TAG_CATEGORY = "main_name"; 
    private static final String TAG_SUBCAT = "cat_name"; 
    private static final String TAG_CODE = "code"; 
    private static final String TAG_NAME = "name"; 
    private static final String TAG_RATE = "rate"; 
    private static final String TAG_RATE2 = "rate2"; 
    private static final String TAG_RATE3 = "rate3"; 
    private static final String TAG_IMAGE = "img"; 

    ArrayList<DetailsVO> mListCustomerDetails = null; 
    ArrayList<HashMap<String, String>> productlist; 

    String subc; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_display_list); 

     productlist = new ArrayList<HashMap<String, String>>(); 

     Bundle extras = getIntent().getExtras(); 
     if (extras != null) { 
      subc = extras.getString("Subcat"); 
     } 

     new AsyncCallSoap1().execute(); 
     productlist.clear(); 
    } 

    public class AsyncCallSoap1 extends AsyncTask<String,Void,String> { 
     private final ProgressDialog dialog=new ProgressDialog(DisplayList.this); 

     //String tal_on=taluka.getText().toString(); 

     @Override 
     protected String doInBackground(String... params) { 
      CallSoap com=new CallSoap(); 
      String SOAP_ACTION="http://tempuri.org/IprotechService/SelectSuCatgeory"; 
      String OPERATION_NAME="SelectSuCatgeory"; 
      String response = com.getDetails(OPERATION_NAME,SOAP_ACTION); 

      if(response!=null){ 
       try { 

        JSONArray array1 = new JSONArray(response); 

        for(int i=0;i<array1.length();i++) 
        { 
         JSONObject obj1 = array1.getJSONObject(i); 
         String out_category = obj1.getString(TAG_CATEGORY); 
         String out_subcat = obj1.getString(TAG_SUBCAT); 
         String out_code = obj1.getString(TAG_CODE); 
         String out_name = obj1.getString(TAG_NAME); 
         String out_rate = obj1.getString(TAG_RATE); 
         String out_rate2 = obj1.getString(TAG_RATE2); 
         String out_rate3 = obj1.getString(TAG_RATE3); 
         String out_img = obj1.getString(TAG_IMAGE); 
         // Toast.makeText(DetailsList.this, categoryId, Toast.LENGTH_LONG).show(); 

         if(subc.equalsIgnoreCase(out_subcat)){ 
          HashMap<String, String> details = new HashMap<String, String>(); 
          details.put(TAG_NAME,out_name); 
          details.put(TAG_RATE,"Rs. "+out_rate); 
          details.put(TAG_IMAGE,out_img); 

          productlist.add(details); 
         } 
        } 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 
      return response; 
     } 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      //Showing progress dialog 
      dialog.setMessage("Please wait..."); 
      dialog.setCancelable(false); 
      dialog.show(); 
     } 

     @Override 
     protected void onPostExecute(String result) { 
      super.onPostExecute(result); 
      dialog.dismiss(); 

      ListAdapter adapter = new SimpleAdapter(
        DisplayList.this, productlist, 
        R.layout.row_layout, new String[] {TAG_IMAGE, TAG_NAME, TAG_RATE,}, 
        new int[] { R.id.imageView2,R.id.textView2, 
          R.id.textView3}); 

      // contactList.notifyDataSetChanged(); 
      setListAdapter(adapter); 
     } 
    } 
} 
+0

是您的圖片url是完全限定的。 –

+0

你的迴應仍然有「NAME.jpg」。這裏沒有提到基地網址。 您可以提供用於將哈希標記值提取到圖像視圖的代碼。 – Sujewan

+0

@Mayur嘗試使用Picasso或Glide從服務器檢索圖像。看到這個鏈接:https://github.com/codepath/android_guides/wiki/Displaying-Images-with-the-Picasso-Library和這個:https://github.com/bumptech/glide – Sushrita

回答

1

你應該問後端的傢伙什麼是圖像的根文件夾。

它應該是這樣的: 「http://domain.com/images/

,那麼你只會CONCAT圖像名稱:

String image_name = "toy.png" 
String image_url = "http://domain.com/images/"+image_name; 

然後使用加載圖像的畢加索或其他方式的圖像視圖:

Picasso.with(context) 
    .load(image_url) 
    .resize(50, 50) 
    .centerCrop() 
    .into(imageView) 
+0

好。非常感謝 –

0

用於在android中的imageview中顯示圖像首先編譯滑動到您的gradle中。

compile 'com.github.bumptech.glide:glide:3.7.0' 

然後在你的適配器getView方法從這樣的URL獲取圖像。

ImageView imageView = (ImageView) itemView.findViewById(R.id.imageView); 
GlideDrawableImageViewTarget imageViewTarget = new GlideDrawableImageViewTarget(imageView); 
Glide.with(mContext).load(url).placeholder(R.raw.loading).error(R.drawable.nophoto).into(imageViewTarget); 
+0

謝謝。我會試試看 –

0
  1. 如果你有像在你的數據庫byteArray意味着,

    • 接收該byteArray,然後將其轉換爲位圖。使用,

      Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray , 0, byteArray.length); 
      
  2. 如果你有image as an url手段,你CA使用一些庫作爲,

這可能會幫助你。

相關問題