简体   繁体   English

更新GUI时的Android进度对话框

[英]Android Progress Dialog Box While updating GUI

So I've been at this for 2 days and still I can't get it working. 所以我已经待了两天了,但仍然无法正常工作。 I've tried solutions of implementing runnable, asynctask but it just never seems to work with my code. 我已经尝试过实现可运行的asynctask的解决方案,但是似乎从来没有与我的代码一起使用。 Perhaps I implemented it the wrong way... 也许我以错误的方式实现了它...

Anyway, I have the following code written. 无论如何,我编写了以下代码。 When I create this activity I want to show a progressdialog with the text "Loading". 创建此活动时,我想显示带有文本“正在加载”的进度对话框。 Problem is, you can't update GUI-elements from another thread. 问题是,您无法从另一个线程更新GUI元素。 That is where I'm stuck. 那就是我卡住的地方。 Hope you can help me out! 希望你能帮帮我!

PS: The reason i need a ProgressDialog is because the line PS:之所以需要ProgressDialog是因为该行

ArrayList<String> genres = MysqlHandler.getAllGenres();

Can take quite some time to load. 可能需要花费一些时间来加载。 Also i have some other activities which need to do the same, and there it can take up to 5 seconds to load. 另外,我还有其他一些活动需要执行相同的操作,因此加载可能需要5秒钟的时间。

public class GenreActivity extends Activity implements OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_genre);

    try {
        ArrayList<String> genres = MysqlHandler.getAllGenres();

        LinearLayout layout = (LinearLayout) findViewById(R.id.AllGenreLayout);

        for(int i = 0; i < genres.size(); i++)
        {
            Button myButton = new Button(this);
            myButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.yellow_button));
            myButton.setTextAppearance(this, R.style.ButtonText);
            myButton.setText(genres.get(i));
            myButton.setOnClickListener(this);
            layout.addView(myButton);
        }
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
/// try :)

progressgialog.post(new Runnable(){
run ()
{
progressgialog.setMessage("asd");
}
})

//// catch :)

activity.runOnUiThread(new Runnable()
{
progressgialog.setMessage("asd");
});

I found a work-around and thought I would share it. 我找到了解决方法,并认为我会分享。 Since I was making another thread inside the oncreate method, I figured I could also make a thread when i was starting the intent. 由于我在oncreate方法中创建了另一个线程,所以我认为在启动该意图时也可以创建一个线程。 So instead of this code: 因此,代替此代码:

Intent inte = new Intent(FirstSearchActivity.this, GenreActivity.class);
startActivity(inte);

Now I'm doing: 现在我正在做:

//start the progress dialog
progressDialog = ProgressDialog.show(FirstSearchActivity.this, "", "Loading...");
new Thread() 
{
public void run() 
    {
        Intent inte = new Intent(FirstSearchActivity.this, GenreActivity.class);
        startActivity(inte);
        progressDialog.dismiss();
    }
}.start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM