【问题标题】:Trouble with Android AsyncTask Progress PercentageAndroid AsyncTask 进度百分比的问题
【发布时间】:2012-11-09 09:27:39
【问题描述】:

我想不出一种计算进度百分比的方法。我正在使用 JSON 来检索数据,然后所有数据都将插入到 sqlite 中。我想在运行 asynctask 时计算所有进程并显示百分比。

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

这是我如何将数据存储到 sqlite 的示例。请指导我。 编辑:如果涉及多个插入? 谢谢

【问题讨论】:

    标签: android json android-asynctask percentage android-sqlite


    【解决方案1】:

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

    SDK documentation 上还有一个示例

    【讨论】:

    • 如果涉及多个插入怎么办?
    • 那么你需要努力找出答案。进度指示留给您,您只知道如何呈现它。我的回答提供了制作自己的进度算法所需的所有技术信息。
    • 感谢您的信息,我设法通过重构我的代码来获得结果,这现在好多了。
    【解决方案2】:

    进度报告有点主观,并且根据您的任务执行情况而有很大差异,但对于您的情况,您可以使用for 进行计算。

    private int progress;
    private int max;
    
    max = total.length()
    for(int i = 0; i < total.length(); i++){
       progress = i;
       publishProgress(progress);
    }
    

    【讨论】:

    • 我不知道如何计算 getAllChannels() 的值,也许你能指导我?
    • 哦,您的建议是操作 insertDatabase() 函数。我试试看。
    • 是的。你算上你的for loop,这就是进步。我编辑了我的代码。
    • 如果涉及多个插入怎么办?
    猜你喜欢
    • 2014-08-06
    • 1970-01-01
    • 1970-01-01
    • 2013-05-27
    • 2015-11-29
    • 2011-03-20
    • 1970-01-01
    • 2016-10-13
    • 2012-12-23
    相关资源
    最近更新 更多