2014-07-20 136 views
0

我正在嘗試使用url填充json的listview。 Json通過Mysql數據庫從php頁面刷新。 當我添加新行或刪除行時,我想刷新列表。 現在,要查看更改,我應用此步驟 - >設置 - >應用程序 - > MyApp->清理緩存 我試過listAdapter.notifyDataSetChanged();但沒有工作。 我試圖用intent調用MainActivity,但這不起作用。 我無法實現拉刷新 對不起,髒話 親切的問候 這裏是我的代碼 - >如何在Android中刷新MainActivity或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://mehmetcantas.info/images/"; 
    public Button refreshs; 

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

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

     refreshs = (Button) findViewById(R.id.refresh); 

     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(); 
     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); 
     } 

    } 



    /** 
    * Parsing json reponse and passing the data to feed view list adapter 
    * */ 
    private void parseJsonFeed(JSONObject response) { 
     try { 
      JSONArray feedArray = response.getJSONArray("feed"); 

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

       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(); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 
// public void onClick(View view) { 
//  switch (view.getId()) { 
// 
// 
//  case R.id.refresh: 
//   
//    Intent intent = getIntent(); 
//    finish(); 
//    startActivity(intent); 
// 
//   break; 
//  } 
// 
// } 
// 

} 
+0

你的問題太含糊。你需要確定原因。 – VM4

+0

@ VM4我需要更新列表視圖。 – cantas

回答

0

我解決了問題,但效率不高。我分享它,如果有人需要。

我decleare一個reflesh按鍵,調意圖當前活動與的onDestroy

public void onClick(View view) { 
     switch (view.getId()) { 
     case R.id.refresh: 
      Intent intent = getIntent(); 
      onDestroy(); 
      finish(); 
      startActivity(intent); 

      break; 
     } 

    } 

我的onDestroy方法這樣

@Override 
protected void onDestroy() { 
    super.onDestroy(); 

    try { 
     trimCache(this); 
     // Toast.makeText(this,"onDestroy " ,Toast.LENGTH_LONG).show(); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

} 
public static void trimCache(Context context) { 
    try { 
     File dir = context.getCacheDir(); 
     if (dir != null && dir.isDirectory()) { 
      deleteDir(dir); 
     } 
    } catch (Exception e) { 
     // TODO: handle exception 
    } 
} 

public static boolean deleteDir(File dir) { 
    if (dir != null && dir.isDirectory()) { 
     String[] children = dir.list(); 
     for (int i = 0; i < children.length; i++) { 
      boolean success = deleteDir(new File(dir, children[i])); 
      if (!success) { 
      return false; 
      } 
     } 
    } 

    // The directory is now empty so delete it 
    return dir.delete(); 
} 
0

我想你應該重新設置適配器刪除後,每次進行時間變化

這樣或添加或任何更改只是調用此:

listView.setAdapter(listAdapter); 
+0

我叫它 listAdapter = new FeedListAdapter(this,feedItems); listView.setAdapter(listAdapter); 在哪裏再打電話? – cantas

+0

調用它,無論你想刷新....我看到你打電話給這個:listAdapter.notifyDataSetChanged();如果這是你想刷新的地方,那就在那裏打電話 – majed