如何刷新我的ListView?

问题描述:

public class MainActivity extends Activity { 
    private static final String TAG = MainActivity.class.getSimpleName(); 
    private ListView listView; 
    private FeedListAdapter listAdapter; 
    private List<FeedItem> feedItems; 
    private String URL_FEED = "http://myozawoo.esy.es/data.php"; 
    private String URL_FEED2 = "http://api.androidhive.info/feed/feed.json"; 

    private SwipeRefreshLayout swipeContainer; 

//  String page = getIntent().getExtras().getString("page"); 
    @SuppressLint("NewApi") 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 0); 
     setContentView(R.layout.activity_main); 

//  String page = getIntent().getExtras().getString("page"); 

     swipeContainer = (SwipeRefreshLayout) findViewById(R.id.swipeContainer); 
     swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 
      @Override 
      public void onRefresh() { 
       parseJsonFeed(); 

      } 
     }); 


     listView = (ListView) findViewById(R.id.list); 

     feedItems = new ArrayList<FeedItem>(); 

     listAdapter = new FeedListAdapter(this, feedItems); 
     listView.setAdapter(listAdapter); 

     // These two lines not needed, 
     // just to get the look of facebook (changing background color & hiding the icon) 
//  getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#3b5998"))); 
//  getActionBar().setIcon(
//    new ColorDrawable(getResources().getColor(android.R.color.transparent))); 

     // We first check for cached request 
//  Cache cache = AppController.getInstance().getRequestQueue().getCache(); 

     // Page One 
     String page = getIntent().getExtras().getString("page"); 
     if(page.equals("1")) { 
      Cache cache = AppController.getInstance().getRequestQueue().getCache(); 
      Entry entry = cache.get(URL_FEED); 
      if (entry != null) { 
       // fetch the data from cache 
       try { 
        String data = new String(entry.data, "UTF-8"); 
        try { 
         parseJsonFeed(new JSONObject(data)); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } catch (UnsupportedEncodingException e) { 
        e.printStackTrace(); 
       } 

      } else { 
       // making fresh volley request and getting json 
       JsonObjectRequest jsonReq = new JsonObjectRequest(Method.GET, 
         URL_FEED, null, new Response.Listener<JSONObject>() { 

        @Override 
        public void onResponse(JSONObject response) { 
         VolleyLog.d(TAG, "Response: " + response.toString()); 
         if (response != null) { 
          parseJsonFeed(response); 
         } 
        } 
       }, new Response.ErrorListener() { 

        @Override 
        public void onErrorResponse(VolleyError error) { 
         VolleyLog.d(TAG, "Error: " + error.getMessage()); 
        } 
       }); 

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

     //Page Two 

     else if (page.equals("2")) { 
      Cache cache = AppController.getInstance().getRequestQueue().getCache(); 
      Entry entry = cache.get(URL_FEED2); 
      if (entry != null) { 
       // fetch the data from cache 
       try { 
        String data = new String(entry.data, "UTF-8"); 
        try { 
         parseJsonFeed(new JSONObject(data)); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } catch (UnsupportedEncodingException e) { 
        e.printStackTrace(); 
       } 

      } else { 
       // making fresh volley request and getting json 
       JsonObjectRequest jsonReq = new JsonObjectRequest(Method.GET, 
         URL_FEED2, null, new Response.Listener<JSONObject>() { 

        @Override 
        public void onResponse(JSONObject response) { 
         VolleyLog.d(TAG, "Response: " + response.toString()); 
         if (response != null) { 
          parseJsonFeed(response); 
         } 
        } 
       }, new Response.ErrorListener() { 

        @Override 
        public void onErrorResponse(VolleyError error) { 
         VolleyLog.d(TAG, "Error: " + error.getMessage()); 
        } 
       }); 

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

     // Other Four Pages 
     else { 
      Cache cache = AppController.getInstance().getRequestQueue().getCache(); 
       Entry entry = cache.get(URL_FEED); 
       if (entry != null) { 
        // fetch the data from cache 
        try { 
         String data = new String(entry.data, "UTF-8"); 
         try { 
          parseJsonFeed(new JSONObject(data)); 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
        } catch (UnsupportedEncodingException e) { 
         e.printStackTrace(); 
        } 

       } else { 
        // making fresh volley request and getting json 
        JsonObjectRequest jsonReq = new JsonObjectRequest(Method.GET, 
          URL_FEED, null, new Response.Listener<JSONObject>() { 

         @Override 
         public void onResponse(JSONObject response) { 
          VolleyLog.d(TAG, "Response: " + response.toString()); 
          if (response != null) { 
           parseJsonFeed(response); 
          } 
         } 
        }, new Response.ErrorListener() { 

         @Override 
         public void onErrorResponse(VolleyError error) { 
          VolleyLog.d(TAG, "Error: " + error.getMessage()); 
         } 
        }); 

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

     } 

     swipeContainer.setColorSchemeColors(android.R.color.holo_blue_bright, 
       android.R.color.holo_green_light, 
       android.R.color.holo_orange_light, 
       android.R.color.holo_red_light); 

    } 





    /** 
    * Parsing json reponse and passing the data to feed view list adapter 
    * */ 
    public void parseJsonFeed(JSONObject response) { 
     try { 
//   String page = getIntent().getExtras().getString("page"); 
//   if (page.equals("1")) 
      JSONArray feedArray = response.getJSONArray("feed"); 

      for (int i = 0; i < feedArray.length(); i++) { 
       JSONObject feedObj = (JSONObject) feedArray.get(i); 

       final FeedItem item = new FeedItem(); 
       item.setId(feedObj.getInt("id")); 
       item.setName(feedObj.getString("name")); 

       // Image might be null sometimes 
       String image = feedObj.isNull("image") ? null : feedObj 
         .getString("image"); 
       item.setImge(image); 
       item.setStatus(feedObj.getString("status")); 
       item.setProfilePic(feedObj.getString("profilePic")); 
       item.setTimeStamp(feedObj.getString("timeStamp")); 

       // url might be null sometimes 
       String feedUrl = feedObj.isNull("url") ? null : feedObj 
         .getString("url"); 
       item.setUrl(feedUrl); 

       feedItems.add(item); 
      } 

      // notify data changes to list adapater 
      listAdapter.notifyDataSetChanged(); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     swipeContainer.setRefreshing(false); 
    } 


} 

我想刷新我的ListView。现在,我无法刷新。我不知道如何刷新。在onRefresh(){}中如何做。我无法调用parseJSON()到onRefresh(){}。请告诉我一个人。非常感谢你! :-)如何刷新我的ListView?

在你的页面变化通话,使用适配器来清除的ListView

listAdapter.clear(); 
adapter.notifyDataSetChanged(); 

的项目如果您使用的是Android的扩展ArrayAdapter定制的适配器,您可能无法找到.clear(),因为私人课程因实施情况而异。例如,.update()

无论如何,请尝试在这里进行更改,看看它是否有效。

swipeContainer.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() { 
      @Override 
      public void onRefresh() { 
       // ---- RIGHT HERE THIS LINE  
       listAdapter.notifyDataSetChanged(); 
      } 
     }); 
+0

我不知道你的意思。 :-( – 2014-11-22 14:40:49

+0

请填写示例代码。我希望!对不起,我英文不好。 – 2014-11-22 14:41:32

您已经使用adaper在列表查看方式

listAdapter = new FeedListAdapter(this, feedItems); 
    listView.setAdapter(listAdapter); 

现在更新其价值后,您可以拨打

listAdapter.notifyDataSetChanged();

您正在寻找刷新列表视图数据的方法,然后在适配器notifyDataSetChanged()中使用其方法;

在小时pareseJsonFeed更新

it... 



    /** 
    * Parsing json reponse and passing the data to feed view list adapter 
    * */ 
    public void parseJsonFeed(JSONObject response) { 
     try { 
//   String page = getIntent().getExtras().getString("page"); 
//   if (page.equals("1")) 
      JSONArray feedArray = response.getJSONArray("feed"); 

      for (int i = 0; i < feedArray.length(); i++) { 
       JSONObject feedObj = (JSONObject) feedArray.get(i); 

       final FeedItem item = new FeedItem(); 
       item.setId(feedObj.getInt("id")); 
       item.setName(feedObj.getString("name")); 

       // Image might be null sometimes 
       String image = feedObj.isNull("image") ? null : feedObj 
         .getString("image"); 
       item.setImge(image); 
       item.setStatus(feedObj.getString("status")); 
       item.setProfilePic(feedObj.getString("profilePic")); 
       item.setTimeStamp(feedObj.getString("timeStamp")); 

       // url might be null sometimes 
       String feedUrl = feedObj.isNull("url") ? null : feedObj 
         .getString("url"); 
       item.setUrl(feedUrl); 

       feedItems.add(item); 
      } 
      listAdapter.clear(); 
      listAdapter.addAll(feedItems); 
      // notify data changes to list adapater 
      listAdapter.notifyDataSetChanged(); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     swipeContainer.setRefreshing(false); 
    } 


} 
+0

你是什么意思?swipeContainer.setOnRefreshListener(新SwipeRefreshLayout.OnRefreshListener(){ @覆盖 公共无效onRefresh(){ listAdapter.notifyDataSetChanged( ); } }); – 2014-11-22 14:49:38

+0

listAdapter.clear()和listAdapter.addAll(feedItems)无法写入,当我写入时,我会面对红色下划线:-( – 2014-11-22 15:16:14

+0

它在编译时错误中显示的内容 – Avi 2014-11-22 15:19:28

尝试无效的缓存数据,这是当你再次调用到服务器。这是最后一次打电话AppController.getInstance().getRequestQueue().getCache().invalidate(key,boolean)