2017-02-02 2 views
-3

웹 사이트에서 JSON 데이터를 가져 와서 표시하는 앱을 개발 중입니다. 데이터를 표시 할 때 작동하지만 웹 사이트에서 데이터가 변경되면 목록보기를 업데이트 할 수 없습니다.ListAdapter를 자동 새로 고침 또는 자동 다시로드하는 방법은 무엇입니까?

@Override 
     protected void onPostExecute(Void result) { 
      super.onPostExecute(result); 
      // Dismiss the progress dialog 
      if (pDialog.isShowing()) 
       pDialog.dismiss(); 
      /** 
      * Updating parsed JSON data into ListView 
      * */ 

     ListAdapter adapter = new SimpleAdapter(MainActivity.this, contactList, R.layout.list_item, 
      new String[]{"field1","updated_at"}, new int[]{ R.id.field1,R.id.updated_at}); 
      listview.setAdapter(adapter); 

     } 
    } 

내가 처리기와 시도했지만 작동하지 않습니다

다음은 비동기 작업의 onPostExecute의 내 JAVA 코드입니다.

도움을드립니다.

미리 감사드립니다. 여기에 업데이트

은 내 완전한

private class GetContacts extends AsyncTask<Void, Void, Void> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     // Showing progress dialog 
     pDialog = new ProgressDialog(MainActivity.this); 
     pDialog.setMessage("Please wait..."); 
     pDialog.setCancelable(false); 
     pDialog.show(); 

    } 

    @Override 
    protected Void doInBackground(Void... arg0) { 


     HttpHandler sh = new HttpHandler(); 

     // Making a request to url and getting response 
     String jsonStr = sh.makeServiceCall(url); 

     Log.e(TAG, "Response from url: " + jsonStr); 

     if (jsonStr != null) { 
      try { 
       JSONObject jsonObj = new JSONObject(jsonStr); 


       // Getting JSON Array node 
       JSONArray feeds = jsonObj.getJSONArray("feeds"); 

       // looping through All feeds 
       for (int i = 0; i < feeds.length(); i++) { 

        JSONObject c = feeds.getJSONObject(i); 

        JSONObject chaObj=(new JSONObject(jsonStr)).getJSONObject("channel"); 

        field1 = c.getString("field1"); 
        updated_at = chaObj.getString("updated_at"); 
        HashMap<String, String> contact = new HashMap<>(); 

        contact.put("field1", field1); 
        contact.put("updated_at",updated_at); 
        contactList.add(contact); 
       } 
      } catch (final JSONException e) { 
       Log.e(TAG, "Json parsing error: " + e.getMessage()); 
       runOnUiThread(new Runnable() { 
        @Override 
        public void run() { 
         Toast.makeText(getApplicationContext(), 
           "Json parsing error: " + e.getMessage(), 
           Toast.LENGTH_LONG) 
           .show(); 
        } 
       }); 

      } 
     } else { 
      Log.e(TAG, "Couldn't get json from server."); 
      runOnUiThread(new Runnable() { 
       @Override 
       public void run() { 
        Toast.makeText(getApplicationContext(), 
          "Couldn't get json from server. Check LogCat for possible errors!", 
          Toast.LENGTH_LONG) 
          .show(); 
       } 
      }); 

     } 


     return null; 
    } 

     @Override 
     protected void onPostExecute(Void result) { 
      super.onPostExecute(result); 
      // Dismiss the progress dialog 
      if (pDialog.isShowing()) 
       pDialog.dismiss(); 
      /** 
      * Updating parsed JSON data into ListView 
      * */ 

      ListAdapter adapter = new SimpleAdapter(MainActivity.this, contactList, R.layout.list_item, 
        new String[]{"field1","updated_at"}, new int[]{ R.id.field1,R.id.updated_at}); 

       lv.setAdapter(adapter); 
     } 


    } 
시도하고 어댑터를 onPostExecute에서 각각의 모든 시간을 설정하지 마십시오
+0

업데이트 된 데이터가 JSON으로 구문 분석되는지 확인하십시오. "예"인 경우 해당 데이터가 "contactList"변수에로드 되었습니까? listview 변수가 UI에서 참조하는 변수입니까? – Arnab

+0

어댑터를 매번 설정하지 마십시오. 데이터가 변경되면 어댑터 소스 목록을 업데이트하고 'adapter.notifydatasetchanged()'를 사용하십시오. –

+0

asynctask가 다시 호출 될 때 코드는 새 데이터 만 표시하므로 웹의 데이터를 새로 고칠 때 일정한 간격이 있으면 당신은 이것을 위해 알림을 사용해야하는 다른 핸들러를 사용할 수 있습니다 –

답변

-1

, 그냥 목록을 설정 한 번 할 adapter.notifyDataSetChanged()와 같은 이 :

if(adapter == null) 
{ 
    ...set list and adapter work 
} 
else 
{ 
    adapter.notifyDataSetChanged(); 
}