gpt4 book ai didi

java - 在fragment后台线程中更新TextView(UI线程)

转载 作者:行者123 更新时间:2023-12-01 09:02:42 25 4
gpt4 key购买 nike

我正在尝试使用 I 的值更新 TextView ,因为它在后台线程中迭代 for 循环。我查看了其他答案并尝试了几种方法,但它没有更新。任何有关我做错的事情的建议都会受到赞赏。

这是我最近的尝试:

        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();
}

这可能没有绝对意义,因为我必须删掉很多行,以便可以在 stackoverflow 上阅读。

基本上:

我有一个新线程,在该线程中我有一个带有 runnable 的处理程序,它正在运行以下 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,因为它在 fragment 中运行。可以吗可能是代码正在运行但仍未更新 TextView 的原因?

更新:

我还使用新的处理程序尝试了以下代码,但也不起作用:

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

预先感谢您的帮助

最佳答案

这样做

 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);

关于java - 在fragment后台线程中更新TextView(UI线程),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41548308/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com