2017-09-13 29 views

回答

2

谷歌建議寫一個單獨的類

public class MySingleton { 
     private static MySingleton mInstance; 
     private RequestQueue mRequestQueue; 
     private ImageLoader mImageLoader; 
     private static Context mCtx; 

     private MySingleton(Context context) { 
      mCtx = context; 
      mRequestQueue = getRequestQueue(); 

      mImageLoader = new ImageLoader(mRequestQueue, 
        new ImageLoader.ImageCache() { 
       private final LruCache<String, Bitmap> 
         cache = new LruCache<String, Bitmap>(20); 

       @Override 
       public Bitmap getBitmap(String url) { 
        return cache.get(url); 
       } 

       @Override 
       public void putBitmap(String url, Bitmap bitmap) { 
        cache.put(url, bitmap); 
       } 
      }); 
     } 

     public static synchronized MySingleton getInstance(Context context) { 
      if (mInstance == null) { 
       mInstance = new MySingleton(context); 
      } 
      return mInstance; 
     } 

     public RequestQueue getRequestQueue() { 
      if (mRequestQueue == null) { 
       // getApplicationContext() is key, it keeps you from leaking the 
       // Activity or BroadcastReceiver if someone passes one in. 
       mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext()); 
      } 
      return mRequestQueue; 
     } 

     public <T> void addToRequestQueue(Request<T> req) { 
      getRequestQueue().add(req); 
     } 

     public ImageLoader getImageLoader() { 
      return mImageLoader; 
     } 
    } 

然後提出一個要求,在您的活動隊列,並把它添加到你的列表視圖中,你可以使用這種方法

List<String> genericList = new LinkedList<String>(); 
    String url = "http://YOUR-JSON-URL.COM"; 
    ListView lvStatus = (ListView) findViewById(R.id.listView); // Bind your list view with R.id.YOUR-LISTVIEW-ID 

    JsonObjectRequest jsObjRequest = new JsonObjectRequest 
      (Request.Method.GET, url, null, new Response.Listener<JSONObject>() { 

     @Override 
     public void onResponse(JSONObject response) { 

     try { 
      result = response.getString("Name"); // Use this to parse an specific value from your JSON, here i'm getting the field "Name", otherwise use reponse.toString() 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     genericList.add(result); // Add the string to a List 
     String[] status = generic.toArray(new String[0]); // Convert your List to an Array 
     lvStatus.setAdapter(new ArrayAdapter<String>(MainActivity.this, 
       android.R.layout.simple_list_item_1, status)); // Set the Array to your adapter and that's it 

     } 
    }, new Response.ErrorListener() { 

     @Override 
     public void onErrorResponse(VolleyError error) { 
      // TODO Auto-generated method stub 

     } 
    }); 
MySingleton.getInstance(this).addToRequestQueue(jsObjRequest); 

有用鏈接:

Google Developers - Making a Standard Request

希望它有幫助

+0

謝謝,但它必須在列表視圖中查看文本,我不確定在列表視圖中顯示文本。所以請給我一些例子。 –

+0

更新了答案,以便響應可以添加到您的ListView,希望它有幫助 –

0

這個網站幫了我很多。有很好的例子: androidhive

+0

雅我已檢查他們,但我認爲,他們沒有給出在列表視圖中顯示文本 –

相關問題