0

I'm developing an Android 3.1 and above application.

I have added a thread to make a REST request using Spring Framework. This is my code:

public class FormsListActivity extends ListActivity
{
    private List<Form> forms;
    private ProgressDialog progressDialog;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
      super.onCreate(savedInstanceState);
      progressDialog = ProgressDialog.show(this, "Please Wait.", "Loading forms...");
      getUpdates.start();
    }

    private Thread getUpdates = new Thread ()
    {
        public void run() 
        {
            try
            {
                forms = FormSpringController.LoadAll();
                progressDialog.dismiss();
            }
            catch (Exception e)
            {
                e.printStackTrace();
                //display = e.getLocalizedMessage();
            }

            runOnUiThread(new Runnable()
            {
                public void run()
                {
                    setListAdapter(new FormAdapter(FormsListActivity.this, R.layout.form_list_item, forms));

                    ListView lv = getListView();
                    lv.setTextFilterEnabled(true);
                }
            });
        }
    };
}

I want to show a dialog inside catch block:

catch (Exception e)
{
    e.printStackTrace();
    //display = e.getLocalizedMessage();
}

I've thought to set display variable it there is an error, and then check it inside runOnUiThread(new Runnable(). If display has a value then show a dialog.

What do you think? Is there any better approach?

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
VansFannel
  • 45,055
  • 107
  • 359
  • 626

2 Answers2

2

check out this . put all code of your thread's run method inside handleMessage(Message message) method

private Handler handler = new Handler(){
        public void handleMessage(Message message){
             try
             {
                 forms = FormSpringController.LoadAll();
                 progressDialog.dismiss();
                 setListAdapter(new FormAdapter(FormsListActivity.this, R.layout.form_list_item, forms));

                 ListView lv = getListView();
                 lv.setTextFilterEnabled(true);
             }
             catch (Exception e)
             {
                 e.printStackTrace();
                 //display = e.getLocalizedMessage();
             }

        }
    }

and call this inside your Thread's run method

handler .sendEmptyMessage(0);
Ravi1187342
  • 1,247
  • 7
  • 14
0
runOnUiThread(new Runnable(){
public void run(){
////code for alert dialog
}
});

is not recommened but you can use it. Better to go with another thread that handled all UI tasks.

Shankar Agarwal
  • 34,573
  • 7
  • 66
  • 64
  • Thanks for your answer. Can you say me where I can find some sample code about "thread that handled all UI tasks"? Thanks – VansFannel Apr 12 '12 at 16:37
  • because `FormSpringController.LoadAll()` is a REST GET Request (and I think it is a synchronous call). You can see `FormSpringController.LoadAll()` source code here http://stackoverflow.com/questions/10073290/expected-begin-array-but-was-begin-object-with-an-array-of-three-elements – VansFannel Apr 12 '12 at 16:47
  • If that is the then better uo use async task – Shankar Agarwal Apr 12 '12 at 16:53
  • Thanks, but, how can I show a dialog inside catch block? I think I can't. Thanks. – VansFannel Apr 13 '12 at 16:07
  • try the solution mentioned above in your catch block – Shankar Agarwal Apr 13 '12 at 16:15