Android ProgressBar未在片段中更新
我的应用程序中有一个表示静态值的ProgressBar,该值是从服务器获取的 片段中的代码位于onCreateView中,如下所示:Android ProgressBar未在片段中更新,android,android-fragments,synchronized,android-progressbar,Android,Android Fragments,Synchronized,Android Progressbar,我的应用程序中有一个表示静态值的ProgressBar,该值是从服务器获取的 片段中的代码位于onCreateView中,如下所示: public class AgeFragment extends Fragment { List<TableStatsCache> tableStatsCacheList; String place,partial1,partial2, partial3, partial4, partial5, partial6; @Override public
public class AgeFragment extends Fragment {
List<TableStatsCache> tableStatsCacheList;
String place,partial1,partial2, partial3, partial4, partial5, partial6;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView placeTextView;
TextView partial1TextView, partial2TextView, partial3TextView, partial4TextView, partial5TextView, partial6TextView;
ProgressBar bar1ProgressBar, bar2ProgressBar, bar3ProgressBar, bar4ProgressBar, bar5ProgressBar, bar6ProgressBar;
readTableStatsCache();
View rootView = inflater.inflate(R.layout.fragment_age, container, false);
placeTextView = (TextView) rootView.findViewById(R.id.placeTextView);
partial1TextView = (TextView) rootView.findViewById(R.id.partial1TextView);
partial2TextView = (TextView) rootView.findViewById(R.id.partial2TextView);
partial3TextView = (TextView) rootView.findViewById(R.id.partial3TextView);
partial4TextView = (TextView) rootView.findViewById(R.id.partial4TextView);
partial5TextView = (TextView) rootView.findViewById(R.id.partial5TextView);
partial6TextView = (TextView) rootView.findViewById(R.id.partial6TextView);
bar1ProgressBar = (ProgressBar) rootView.findViewById(R.id.bar1ProgressBar);
bar2ProgressBar = (ProgressBar) rootView.findViewById(R.id.bar2ProgressBar);
bar3ProgressBar = (ProgressBar) rootView.findViewById(R.id.bar3ProgressBar);
bar4ProgressBar = (ProgressBar) rootView.findViewById(R.id.bar4ProgressBar);
bar5ProgressBar = (ProgressBar) rootView.findViewById(R.id.bar5ProgressBar);
bar6ProgressBar = (ProgressBar) rootView.findViewById(R.id.bar6ProgressBar);
placeTextView.setText(place);
partial1TextView.setText(partial1+"%");
partial2TextView.setText(partial2+"%");
partial3TextView.setText(partial3+"%");
partial4TextView.setText(partial4+"%");
partial5TextView.setText(partial5+"%");
partial6TextView.setText(partial6+"%");
bar1ProgressBar.setProgress(Math.round(Float.valueOf(partial1)));
bar2ProgressBar.setProgress(Math.round(Float.valueOf(partial2)));
bar3ProgressBar.setProgress(Math.round(Float.valueOf(partial3)));
bar4ProgressBar.setProgress(Math.round(Float.valueOf(partial4)));
bar5ProgressBar.setProgress(Math.round(Float.valueOf(partial5)));
bar6ProgressBar.setProgress(Math.round(Float.valueOf(partial6)));
return rootView;
}
public void readTableStatsCache(){
DatabaseHandlerTableStatsCache db_read = new DatabaseHandlerTableStatsCache(CallFragment.context);
tableStatsCacheList = db_read.getAllLines();
for (TableStatsCache cn : tableStatsCacheList) {
place = cn.getPlace();
partial1 = cn.getPartial1();
partial2 = cn.getPartial2();
partial3 = cn.getPartial3();
partial4 = cn.getPartial4();
partial5 = cn.getPartial5();
partial6 = cn.getPartial6();
}
}
}
我认为问题可能是因为方法“setProgress”是同步的
如何将旧线程释放到新线程才能运行该方法?我不知道如何从服务器获取值,但我认为问题在于静态值。它不会刷新,工具栏上的进度也不会更改。您如何从服务器获取价值?我知道我回答这个问题已经很晚了。也许你已经解决了这个问题,但它可能对将来遇到类似问题的人有所帮助。我也面临着类似的问题。在我的例子中,
progressbar.setProgress(someStaticValue)代码>在viewPager中的片段内不起作用。当viewPager显示最后一页时,我正在添加一个新片段。数据来自服务器,在进行一些计算后,我需要显示进度状态。我的片段包含4个progressBar和一些文本视图,所有这些都应该用someStaticValue更新。这是进度条第一次成功更新,但在添加一个片段后,它无法更新进度条的进度状态,尽管文本视图正在更新。我做了很多研究,但没有找到合适的解决办法。然后我看到了你的帖子,你暗示问题可能是,由于setProgress()
的同步性。因此我尝试在新线程中调用setProgress(),但该值应该在主线程上更新。为了这个目的,我使用了。下面是我如何更新进度条的
new Handler().post(new Runnable() {
@Override
public void run() {
mScienceProgressBar.setProgress(scienceScore);
}
});
在这里,更新操作将在不同的线程上执行,但是更新将反映在主线程中。你能发布更多的代码吗?整个片段?另外,您是否正在更新新线程中的片段?嗨@Adryan,您的问题解决了吗?如果是,您可以告诉它的解决方案是什么?TextView中使用相同的值,并且TextView会正确更新。因此,问题不在于价值。
new Handler().post(new Runnable() {
@Override
public void run() {
mScienceProgressBar.setProgress(scienceScore);
}
});