2015-04-21 60 views
-1

我想加載圖像和一個字符串值對應於從服務器到我的android應用程序在列表視圖中使用volley.jar庫中的每個圖像。我無法得到這個工作,並找不到我做錯了什麼。我很感激,如果有人可以請告訴我我做錯了什麼。使用volley.jar從服務器加載圖像到我的應用程序

我有在那裏我已經宣佈的方法來獲取和設置變量標題的Movie類和thumbnailUrl

這是我CustomListAdapater類

public class CustomListAdapter extends BaseAdapter { 
    private Activity activity; 
    private LayoutInflater inflater; 
    private List<Movie> movieItems; 
    ImageLoader imageLoader = AppController.getInstance().getImageLoader(); 

    public CustomListAdapter(Activity activity, List<Movie> movieItems) { 
     this.activity = activity; 
     this.movieItems = movieItems; 
    } 

    @Override 
    public int getCount() { 
     return movieItems.size(); 
    } 

    @Override 
    public Object getItem(int location) { 
     return movieItems.get(location); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     if (inflater == null) 
      inflater = (LayoutInflater) activity 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     if (convertView == null) 
      convertView = inflater.inflate(R.layout.list_row, null); 

     if (imageLoader == null) 
      imageLoader = AppController.getInstance().getImageLoader(); 
     NetworkImageView thumbNail = (NetworkImageView) convertView 
       .findViewById(R.id.thumbnail); 
     TextView title = (TextView) convertView.findViewById(R.id.title); 

     // getting movie data for the row 
     Movie m = movieItems.get(position); 

     // thumbnail image 
     thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader); 

     // title 
     title.setText(m.getTitle()); 

     return convertView; 
    } 
} 

這在我的MainActivity類別

public class MainActivity extends Activity { 
    // Log tag 
    private static final String TAG = MainActivity.class.getSimpleName(); 

    // Movies json url 
    private static final String url = "---------------------my api url-------------"; 
    private ProgressDialog pDialog; 
    private List<Movie> movieList = new ArrayList<Movie>(); 
    private ListView listView; 
    private CustomListAdapter adapter; 

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

     listView = (ListView) findViewById(R.id.list); 
     adapter = new CustomListAdapter(this, movieList); 
     listView.setAdapter(adapter); 

     pDialog = new ProgressDialog(this); 
     // Showing progress dialog before making http request 
     pDialog.setMessage("Loading..."); 
     pDialog.show(); 

     // changing action bar color 
     try { 
      getActionBar().setBackgroundDrawable(
        new ColorDrawable(Color.parseColor("#1b1b1b"))); 
     }catch (Exception e) { 
     } 

     // Creating volley request obj 
     JsonArrayRequest movieReq = new JsonArrayRequest(url, 
       new Response.Listener<JSONArray>() { 
        @Override 
        public void onResponse(JSONArray response) { 
         Log.e(TAG+" response", response.toString()); 
         hidePDialog(); 

         // Parsing json 
         for (int i = 0; i < response.length(); i++) { 
          try { 

           JSONObject obj = response.getJSONObject(i); 
           Movie movie = new Movie(); 
           movie.setTitle(obj.getString("description")); 
           String logo = obj.getString("logo"); 
           String src ="------------Image directory path-------------"+logo; 
           movie.setThumbnailUrl(src); 

           // adding movie to movies array 
           movieList.add(movie); 

          } catch (JSONException e) { 
           e.printStackTrace(); 
          } 

         } 

         // notifying list adapter about data changes 
         // so that it renders the list view with updated data 
         adapter.notifyDataSetChanged(); 
        } 
       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       VolleyLog.d(TAG, "Error: " + error.getMessage()); 
       hidePDialog(); 

      } 
     }); 

     // Adding request to request queue 
     AppController.getInstance().addToRequestQueue(movieReq); 
    } 
} 

這就是我的logcat

D/OpenGLRenderer﹕ Flushing caches (mode 0) 
D/AbsListView﹕ Get MotionRecognitionManager 
D/TextLayoutCache﹕ Using debug level: 0 - Debug Enabled: 0 
D/libEGL﹕ loaded /system/lib/egl/libGLES_android.so 
D/libEGL﹕ loaded /system/lib/egl/libEGL_adreno200.so 
D/libEGL﹕ loaded /system/lib/egl/libGLESv1_CM_adreno200.so 
D/libEGL﹕ loaded /system/lib/egl/libGLESv2_adreno200.so 
I/Adreno200-EGLSUB﹕ <ConfigWindowMatch:2218>: Format RGBA_8888. 
D/memalloc﹕ /dev/pmem: Mapped buffer base:0x51edf000 size:307200 offset:0 fd:64 
D/OpenGLRenderer﹕ Enabling debug mode 0 
I/Adreno200-EGLSUB﹕ <ConfigWindowMatch:2218>: Format RGBA_8888. 
D/memalloc﹕ /dev/pmem: Mapped buffer base:0x5203a000 size:8990720 offset:7454720 fd:67 
D/memalloc﹕ /dev/pmem: Mapped buffer base:0x5293d000 size:614400 offset:307200 fd:70 
D/memalloc﹕ /dev/pmem: Mapped buffer base:0x529d3000 size:20766720 offset:19230720 fd:73 
D/memalloc﹕ /dev/pmem: Mapped buffer base:0x53ea1000 size:921600 offset:614400 fd:76 
D/Volley﹕ [1] 2.onErrorResponse: MainActivity 
D/Volley﹕ [1] Request.finish: 3487 ms: [ ] http:----------------------------myApi.php 0xcb62917f NORMAL 1 
D/memalloc﹕ /dev/pmem: Unmapping buffer base:0x51edf000 size:307200 offset:0 
D/memalloc﹕ /dev/pmem: Unmapping buffer base:0x5293d000 size:614400 offset:307200 
D/memalloc﹕ /dev/pmem: Unmapping buffer base:0x53ea1000 size:921600 offset:614400 
D/CLIPBOARD﹕ Hide Clipboard dialog at Starting input: finished by someone else... ! 
+0

嘗試使用畢加索: – kamal2305

+0

我發現我犯了錯誤。其實錯誤的是,該方法返回JSONArray值,我試圖直接從它得到JSONObject沒有明確轉換它.... –

回答

2

嘗試使用畢加索強大的圖像下載和緩存庫爲Android,你只需要包括jar文件,寫一行代碼的進一步詳細信息如下鏈接:

Picasso library for Android

相關問題