2017-07-19 31 views
0

我已經通過多個線程試圖已經解決了這個看上去不過這總是命中的「mListView漁獲.setAdapter(mAdapter);」線。只有創建一個視圖層次可以觸摸其觀點原來的線程,我試圖轉移到UI線程,但似乎沒有任何工作

public void populateListView() // adds each string from json file to the listview of puzzles 
{ 
    try { 
     String[] strings = mPuzzleNames.toArray(new String[mPuzzleNames.size()]); 

     mAdapter = new ArrayAdapter<>(mContext, android.R.layout.simple_list_item_1, android.R.id.text1, strings); 
     mListView.setAdapter(mAdapter); 
     mAdapter.notifyDataSetChanged(); //updates UI 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
    } 

} 
+0

什麼調用'populateListView()'?你是如何將它移到UI線程的? – Cheticamp

+0

populateListView()被調用在的AsyncTask,所以我相信這是在後臺線程。 我試圖把 'runOnUiThread(新的Runnable(){ @覆蓋 公共無效的run(){,更新UI //東西 } });'(對不起所有的編輯我無法找出此格式爲我的生活) –

+0

是從'doInBackground稱爲()'的'AsyncTask'?如果是這樣,那將在後臺。當你嘗試使用'runOnUiThread'時會發生什麼?你可以分享該代碼嗎? – Cheticamp

回答

0

嘗試使用處理程序:

public void populateListView() { 
    try { 
     String[] strings = mPuzzleNames.toArray(new String[mPuzzleNames.size()]); 

     Handler handler = new Handler(Looper.getMainLooper()); 
     handler.post(new Runnable() { 
      @Override 
      public void run() { 
       mAdapter = new ArrayAdapter<>(mContext, android.R.layout.simple_list_item_1, android.R.id.text1, strings); 
       mListView.setAdapter(mAdapter); 
      } 
     }); 
    } 
    catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

也沒有必要叫mAdapter.notifyDataSetChanged()因爲你每次實際上重建適配器。

相關問題