2017-01-09 175 views
0

我試圖用I值更新文本視圖,因爲它在後臺線程中循環訪問循環。我已經看過其他答案並嘗試了幾種方法,但沒有更新。任何意見,我在做什麼錯誤表示讚賞。更新片段後臺線程中的TextView(UI線程)

這是我最新的嘗試:

 new Thread(){ 
      public void run(){ 
       // Start the detailed search of channels 
       YoutubeChannelInfoConnector yc = new YoutubeChannelInfoConnector(getActivity()); 
       channelinfosearchResults = yc.search(chanelIdToSearchStringBuilder.toString()); 

       handler.post(new Runnable(){ 
        public void run(){ 

           // Add the new influencers to the database 
           for (int i = 0; i < youtubeCompleteArraylistOfChannelsSearchedInDetail.size(); i++){ 

            final int finalI = i; 
            getActivity().runOnUiThread(new Runnable() { 
             public void run() { 
              String addedInt = String.valueOf(finalI); 
              newRecordsAddedToDatabaseTextView.setText(addedInt); 
             } 
            }); 

            Log.d("Database","record added " + i); 
           } 

           Toast.makeText(getActivity(), "Database Updated", Toast.LENGTH_SHORT).show(); 
           loadingview.setVisibility(View.GONE); 




          } else { 

           // continue in the detailed search of channels 
           searchYoutubeChannels(channelsToAdd); 
          } 
         } else { 
          Toast.makeText(getActivity(), "ERROR: THE SEARCH RETURNED NO RESULTS!", Toast.LENGTH_LONG).show(); 
         } 

        } 
       }); 
      } 
     }.start(); 
    } 

這可能不會讓絕對意義上的,因爲我不得不削減這麼多行,使其有可能在計算器閱讀。

基本上是:

我有新的線程,該線程我有一個可運行的,其運行的for循環如下的處理程序:

// Add the new influencers to the database 
           for (int i = 0; i < youtubeCompleteArraylistOfChannelsSearchedInDetail.size(); i++){ 

            final int finalI = i; 
            getActivity().runOnUiThread(new Runnable() { 
             public void run() { 
              String addedInt = String.valueOf(finalI); 
              newRecordsAddedToDatabaseTextView.setText(addedInt); 
             } 
            }); 

            Log.d("Database","record added " + i); 
           } 

我使用getActivity.runonUI,因爲它是在一個片段運行。那 可能是代碼運行的原因,但仍不能更新TextView?

UPDATE:

我也試過下面的代碼用新的處理程序,但沒有工作,要麼:提前

final int finalI = i; 
            new Handler().post(new Runnable() { 
             @Override 
             public void run() { 
              String addedInt = String.valueOf(finalI); 
              newRecordsAddedToDatabaseTextView.setText(addedInt); 
             } 
            }); 

感謝您的幫助

+0

不要把runOnUiThread裏面的for循環。這不是一個好的做法 – Sridhar

+0

那你如何更新呢? –

回答

1

做這樣的

Handler handler=new Handler(); 
    handler.postDelayed(new Runnable() { 
     @Override 
     public void run() { 

      // Add the new influencers to the database 
      for (int i = 0; i < 100; i++){ 
       String addedInt = String.valueOf(i); 
       Log.e("data",addedInt); 
       view.setText(addedInt); 
       Toast.makeText(MainActivity.this, "Database Updated", Toast.LENGTH_SHORT).show(); 

     } 

     } 
    },2000); 
+0

這不是在後臺線程? –

+0

您可以使用asynctask()來處理它,並onpostexecute()做這個工作 –

+0

謝謝反正。那不工作我想更新,而後臺線程運行不在後臺任務結束時postexecute –

0

做這樣的事情;

Handler handler=new Handler(Looper.getMainLooper()); 
     handler.postDelayed(new Runnable() { 
      @Override 
      public void run() { 

       // Add the new influencers to the database 
       for (int i = 0; i < 100; i++){ 
        String addedInt = String.valueOf(i); 
        Log.e("data",addedInt); 
        view.setText(addedInt); 
        Toast.makeText(MainActivity.this, "Database Updated", Toast.LENGTH_SHORT).show(); 

      } 

      } 
     },2000); 
+0

這是不在後臺線程? –

+0

然後使用Asynctask進行背景數據更新。 在doInBackground(Void ... params)方法下在Asynctask中使用上面的方法 –

0

使用此:

public class Data extends AsyncTask<Void,Void,String> { 
    @Override 
    protected String doInBackground(Void... params) { 
     Handler handler=new Handler(Looper.getMainLooper()); 
     handler.postDelayed(new Runnable() { 
      @Override 
      public void run() { 

       // Add the new influencers to the database 
       for (int i = 0; i < 100; i++){ 
        String addedInt = String.valueOf(i); 
        Log.e("data",addedInt); 
        view.setText(addedInt); 
        Toast.makeText(MainActivity.this, "Database Updated", Toast.LENGTH_SHORT).show(); 

       } 

      } 
     },2000); 

     return txt; 
    } 
}