【发布时间】:2020-11-19 09:44:52
【问题描述】:
当我尝试使用retrofit2库通过rest api将数据从mysql同步到sqlite时,我无法更新同步状态的实时百分比
ProgressBar progressBar;
Retrofit retrofit;
Baseurl bs;
List<Voterdata> voterslist;
TextView downloadstatus;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bs=new Baseurl();
retrofit =new Retrofit.Builder()
.baseUrl(bs.getBaseUrl())
.addConverterFactory(GsonConverterFactory.create())
.build();
downloadstatus=findViewById(R.id.tvdownloadstatus);
progressBar=findViewById(R.id.progressbarvotersdata);
}
改造代码分为onresponse和offline sync两部分
public void syncvoterdata(View V){
downloadstatus.setText("Fetching from server");//is being displayed
Api_voterdata api=retrofit.create(Api_voterdata.class);
Call<List<Voterdata>> call=api.voterdata();
call.enqueue(new Callback<List<Voterdata>>() {
@Override
public void onResponse(Call<List<Voterdata>> call, Response<List<Voterdata>> response) {
voterslist=new ArrayList<>(response.body());
downloadstatus.setText("fetch completed starting sync with local storage total records:-"+voterslist.size());
//no change in display
synctake(voterslist);
}
@Override
public void onFailure(Call<List<Voterdata>> call, Throwable t) {
}
});
}
同步到 sqlite 在这里
public void synctake(List<Voterdata> data){
int totalrecords=data.size();
Log.d("no of rows", String.valueOf(data.size()));// working in logcat
internaldatabase db=new internaldatabase(MainActivity.this);
//both components are not getting updated here
downloadstatus.setText("saved "+0+" of "+totalrecords);
progressBar.setMax(totalrecords);
progressBar.setProgress(0);
progressBar.animate();
for(int i=0;i<data.size();i++){
db.Syncdata(data.get(i));
downloadstatus.setText("saved "+i+" of "+totalrecords);
Log.d("status","saved "+i+" of "+totalrecords);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
progressBar.setProgress(i,true);
}
}
db.close();
}
只有在 sqlite 同步任务完成后,textview 和进度条都显示为 100,中间没有变化
【问题讨论】:
标签: android textview retrofit android-livedata android-progressbar