Android异步任务进度百分比有问题
我想不出计算进度百分比的方法。我使用JSON检索数据,然后将所有数据插入sqlite。我想在运行asynctask时计算所有进程并显示一个百分比Android异步任务进度百分比有问题,android,json,android-asynctask,percentage,android-sqlite,Android,Json,Android Asynctask,Percentage,Android Sqlite,我想不出计算进度百分比的方法。我使用JSON检索数据,然后将所有数据插入sqlite。我想在运行asynctask时计算所有进程并显示一个百分比 @Override protected Void doInBackground(Void... params) { //int progress = 0; //progress testing //while(progress < 100) { //progress += 10; //SystemCloc
@Override
protected Void doInBackground(Void... params) {
//int progress = 0; //progress testing
//while(progress < 100) {
//progress += 10;
//SystemClock.sleep(1000);
//publishProgress(progress);
//}
getAllChannels(); //function get json data and insert db.
return null;
}
...
public void getAllChannels(){
try {
headline = jsheadline.getJSONArray(TAG_NEWLIST);
headline2 = jsheadline2.getJSONArray(TAG_NEWLIST2);
headline3 = jsheadline3.getJSONArray(TAG_NEWLIST3);
insertDatabase(headline,TABLE_HEADLINE);
insertDatabase(headline2,TABLE_HEADLINE2);
insertDatabase(headline3,TABLE_HEADLINE3);
}catch (Exception e) {
}
}
public void insertDatabase(JSONArray total, String tablename) throws JSONException{
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
for(int i = 0; i < total.length(); i++){
JSONObject c = total.getJSONObject(i);
// Storing each json item in tablecolumn
String subject = c.getString(KEY_NEWS_SUBJECT);
db.addNews(tablename, subject);
}
@覆盖
受保护的Void doInBackground(Void…参数){
//int progress=0;//进度测试
//而(进度<100){
//进展+=10;
//系统时钟。睡眠(1000);
//出版进度(progress);
//}
getAllChannels();//函数获取json数据并插入数据库。
返回null;
}
...
public void getAllChannels(){
试一试{
headline=jsheadline.getJSONArray(TAG_NEWLIST);
headline2=jsheadline2.getJSONArray(TAG_NEWLIST2);
headline3=jsheadline3.getJSONArray(TAG_NEWLIST3);
插入数据库(标题,表标题);
插入数据库(headline2,表_headline2);
插入数据库(headline3,表_headline3);
}捕获(例外e){
}
}
public void insertDatabase(JSONArray total,String tablename)抛出JSONException{
DatabaseHandler db=新的DatabaseHandler(getApplicationContext());
对于(int i=0;i
这是我如何将数据存储到sqlite中的示例。请指导我。
编辑:是否涉及多个插入?
谢谢进度报告有点主观,根据您的任务执行情况而有所不同,但对于您的情况,您可以使用的进行计算
private int progress;
private int max;
max = total.length()
for(int i = 0; i < total.length(); i++){
progress = i;
publishProgress(progress);
}
private-int-progress;
私人int max;
max=总长度()
对于(int i=0;i
onProgressUpdate支持多个参数:
protected void onProgressUpdate(Integer... progress) {
int current = progress[0];
int total = progress[1];
float percentage = 100 * (float)current / (float)total;
// Display your progress here
}
您可以采用如下处理方法:
protected Void doInBackground(Void... params) {
JSONArray headlines = jsheadline.getJSONArray(TAG_NEWLIST);
int count = headlines.length();
for (int i=0; i<count; ++i) {
// Insert a single item in the DB
JSONObject headlineItem = headlines.getJSONObject(i);
insertDatabase(headlineItem);
// We have processed item 'i' out of 'count'
publishProgress(i, count);
// Escape early if cancel() is called
if (isCancelled()) break;
}
return null;
}
受保护的Void doInBackground(Void…params){
JSONArray headlines=jsheadline.getJSONArray(TAG_NEWLIST);
int count=headlines.length();
对于(int i=0;我不知道如何从GetAllChannel()计算值,可以指导我吗?哦,你建议的是操作insertDatabase()函数。我会尝试一下。是的。你为循环计算你的
,这就是进度。我编辑了我的代码。如果多个插入涉及怎么办?如果多个插入涉及怎么办?那么你需要做一些工作来找出答案。进度指示留给你,你只知道如何显示。我的答案提供了你需要的所有技术信息制作您自己的进度算法。感谢您提供的信息,我通过重新构建代码获得了结果,这比现在好多了。