我试图用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
那你如何更新呢? –