我試圖用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);
}
});
感謝您的幫助
不要把runOnUiThread裏面的for循環。這不是一個好的做法 – Sridhar
那你如何更新呢? –