2013-07-25 5 views
0

나는 AsyncTask을 사용하고 twitter 피드를 비동기 적으로 가져 와서 listView에 표시합니다. 그것은 잘 작동합니다 그럼 목록보기를 새로 고치려면 RunnableRun()에 asyncTask 호출을 넣어 특정 간격 후에로드합니다. 특정 시간 간격 후에 피드가로드되지만 목록보기가 업데이트되지 않습니다. asyncTask 내부에서 notifyDataSetChanged()을 사용하지만 작동하지 않습니다. AsyncTask에서 ListView를 새로 고치는 방법은 무엇입니까?

내 노력 코드 : 귀하의에서 onCreate 방법에 된 setContentView 후

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

:

public class Twitter7FeedActivity extends Activity { 

    LazyAdapter adapter; 
    ListView list; 
    JSONObject jsonObj = null; 
    JSONArray jsonArray = null; 

    @SuppressLint("SimpleDateFormat") 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.layout_twitter7_feed); 
     callAsynchronousTask(); 
    } 

    public void callAsynchronousTask() { 
     final Handler handler = new Handler(); 
     Timer timer = new Timer(); 
     TimerTask doAsynchronousTask = new TimerTask() { 

      @Override 
      public void run() { 
       handler.post(new Runnable() { 
        public void run() { 
         try { 
          new GetFeedTask().execute(CommonUtils.BEARER_TOKEN, 
            CommonUtils.URL); 
         } catch (Exception e) { 
         } 
        } 
       }); 
      } 
     }; 

     timer.schedule(doAsynchronousTask, 0, 50000); 
    } 

    protected class GetFeedTask extends AsyncTask<String, Void, String> { 
     private ProgressDialog dialog = new ProgressDialog(
       Twitter7FeedActivity.this); 

     protected void onPreExecute() { 
      this.dialog.setMessage("Please wait"); 
      this.dialog.show(); 
     } 

     @Override 
     protected String doInBackground(String... params) { 
      // related code 
     } 

     @Override 
     protected void onPostExecute(String jsonText) { 
      // related code 
      adapter = new LazyAdapter(Twitter7FeedActivity.this, 
      // tweets); 
      list = (ListView) findViewById(R.id.list); 
      list.setAdapter(adapter); 
      adapter.notifyDataSetChanged(); 
     } 
    } 
} 
+0

알림 어댑터 사용 – KOTIOS

+1

그는 이미이 어댑터를 사용하고 있습니다. adapter.notifyDataSetChanged(); –

+0

이 솔루션을 사용해 보시겠습니까? http://stackoverflow.com/questions/15472539/refresh-spinner-view-after-execute-asynchronous-task-in-another-class – tsohtan

답변

0

당신이 봤어이 라인을 이동?

+0

예, 이미 시도했지만 작동하지 않습니다. ( –

+0

@ Noor Azam, 대답은 다음과 같습니다. 대답이 아닙니다.이 유형의 혼동/제안을 코멘트 섹션에 넣으십시오. –

0
// related code adapter = new LazyAdapter(Twitter7FeedActivity.this, tweets); 

이 코드의 주석 처리는 onPostExecute() 메소드에 있습니다. 나는 그것이 당신을 도울 것 같아요. 감사합니다 ...

0

Handler만을 사용하여 질문에 언급 된 작업을 수행 할 수 있습니다. 이런 식으로 뭔가 : 당신이 TimerTask 내부에 AsyncTask을 사용하지

public class Twitter7FeedActivity extends Activity { 
LazyAdapter adapter; 
ListView list; 
JSONObject jsonObj = null; 
JSONArray jsonArray = null; 

@SuppressLint("SimpleDateFormat") 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.layout_twitter7_feed); 
    callAsynchronousTask(); 
} 

public void callAsynchronousTask() { 
    final Handler handler = new Handler(); 
    Runnable runnable = new Runnable() { 

     ProgressDialog dialog; 

     public void run() { 
        try { 

         //Show your dialog here 
         runOnUiThread(new Runnable() { 
          public void run() { 
           runnable.this.dialog = new ProgressDialog(Twitter7FeedActivity.this); 
           runnable.this.dialog.setMessage("Please wait"); 
           runnable.this.dialog.show(); 
          } 

         }); 

         //Do your AsyncTask's doInBackground work here 

         //Update the UI with runOnUiThread or using the Ui threa handler 
         runOnUiThread(new Runnable() { 
          public void run() { 
           // related code adapter = new LazyAdapter(Twitter7FeedActivity.this, tweets); 
           list = (ListView) findViewById(R.id.list); 
           list.setAdapter(adapter); 
           adapter.notifyDataSetChanged(); 
          } 

         }); 

         //Then post the thread to excecute after 50000 milliseconds. 
         handler.postDelayed(runnable, 50000); 
        } catch (Exception e) 
        { } 
       } 
    }; 
    handler.post(runnable); 
    } 
} 

이 방법.

관련 문제