首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ProgressBar直到JSONParser返回数据

ProgressBar直到JSONParser返回数据
EN

Stack Overflow用户
提问于 2013-06-07 22:58:00
回答 2查看 1.4K关注 0票数 1

我的应用程序使用TabView来显示数据。其中一个TabActivity对应JSONArray,另一个是GoogleMap。当我点击它们中的一个时,应用程序会停止几秒钟,然后显示内容。所以我有一个问题--如何使用ProgressBar让它立即启动,当数据加载时,它将启动activity的内容。我就是根本不知道如何使用ProgressBar(旋转的东西)。

提前谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-06-07 23:03:54

使用AsyncTask下载您的数据。这样,在下载数据之前,您的UI不会冻结。

始终尝试将下载数据与UI线程分开。

您可以使用onProgressUpdate()方法创建一个很好的进度条。

如果你不能计算任何进度,你应该在执行任务之前启用一个没有进度范围的对话框,并在onPostExecute()上删除它。

有关使用示例,请参阅文档中的使用示例。

公共类HomeActivity扩展了Activity {

代码语言:javascript
复制
private static String url = "address here";
private static final String TAG_CONTENT = "content";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_last_minute);
    TextView tv = (TextView) findViewById(R.id.lastMinuteText);

    // Create progress dialog     
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    // Get the layout inflater
    LayoutInflater inflater = getLayoutInflater();
    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog
    builder.setView(inflater.inflate(R.layout.progressdialog, null));
    progressDialog = builder.create();
    progressDialog.setTitle("Downloading JSON");
    progressDialog.setButton(AlertDialog.BUTTON_NEGATIVE, "Whatever",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            }
        });
    // Show dialog
    progressDialog.show();
    //download json
    new downloadJSON().Execute(url);
}
private class downloadJSON extends AsyncTask<String, Void, String> {

    private String allMessages = "";
    @Override
    protected Void doInBackground(String... params) {
        String url = params;
        //JSON PARSER
        JSONParser jParser = new JSONParser();

        try {
            JSONArray messages = jParser.getJSON(url);
        } catch(JSONException e) {
            e.printStackTrace();
        }

        for(int i = 0; i < messages.length(); i++) {
            JSONObject c;
            try {
                c = messages.getJSONObject(i);
                allMessages = allMessages + c.getString(TAG_CONTENT) + 
                    "\n-----------------------------------------\n";
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        return allMessages ;
    }

    @Override
    protected void onPostExecute(String result) {
        tv.setText(result);
        progressDialog.cancel();
    }

    @Override
    protected void onProgressUpdate(Void... values) {

    }

}

}

您的对话框(R.layout.progressdialog):

代码语言:javascript
复制
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="vertical" >

    <ProgressBar
        android:id="@+id/progressBar1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>
票数 2
EN

Stack Overflow用户

发布于 2013-06-07 23:06:15

使用asyncTask

将此代码放入oncreate()方法中

代码语言:javascript
复制
private static ProgressDialog dialog;
LongOperation MyTask= new LongOperation();
        MyTask.execute();
dialog = ProgressDialog.show(this, "", "Wait Please");

private class LongOperation extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... params) {
        // Do here what you want Every thing process should be here only
    }      

    @Override
    protected void onPostExecute(String result) { 
     dialog.dissmiss();

       //now you can see the result here 
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16987144

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档