user826323
user826323

Reputation: 2338

android - update progressbar

I would like to update spinner object after I get a google calendar name in a different thread. When I execute this, it crashes. I am not sure if I need to make it work with a different approach or if there is something wrong with it.

private void updateGoogleCalendar() {
  try {
      Thread.sleep(4000);
      List<String> list = new ArrayList<String>();
      list.add("Sample Calendar");
      updatedCalendarNames = list.toArray(new String[0]);
      progressBar.dismiss();

    } catch (InterruptedException e) {
      e.printStackTrace();
    }
}
 public void initializeWidgets() {
    final Spinner spinner = (Spinner) layout.findViewById(R.id.googleCalendarSelection);
    final Button refreshCalendarBtn = (Button) layout.findViewById(R.id.refreshCalendarBtn);
    refreshCalendarBtn.setOnClickListener(new OnClickListener() {
       @Override
         public void onClick(View v) {
         progressBar = ProgressDialog.show(getContext(), "", "Loading...");
         new Thread(new Runnable() {

       @Override
         public void run() {
         updateGoogleCalendar();
         final ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getContext(), android.R.layout.simple_spinner_item);
         adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
         spinner.setAdapter(adapter);
         while (updatedCalendarNames == null) {
           try {
               Thread.sleep(500);

             } catch (InterruptedException e) {
               e.printStackTrace();
             }
        }
            for (String calendarName : updatedCalendarNames) {
            CharSequence charSequence = calendarName + "";
            adapter.add(charSequence);
          }                     
            }
         }).start();
       }
    });
  }

Upvotes: 0

Views: 396

Answers (2)

jeet
jeet

Reputation: 29199

you need to add your ui update code into event thread only, and to notify UI/Event Thread you need to implement Handler or AsyncTask, for example you can update by handler as follows:

public void initializeWidgets() {

    final Spinner spinner = (Spinner) layout.findViewById(R.id.googleCalendarSelection);


    final Button refreshCalendarBtn = (Button) layout.findViewById(R.id.refreshCalendarBtn);
    refreshCalendarBtn.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            progressBar = ProgressDialog.show(getContext(), "", "Loading...");
            new Thread(new Runnable() {
                @Override
                public void run() {
                    updateGoogleCalendar();
                    final ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(getContext(), android.R.layout.simple_spinner_item);
                    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    spinner.setAdapter(adapter);

                    while (updatedCalendarNames == null) {
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    for (String calendarName : updatedCalendarNames) {
                        Message msg=handler.obtainMessage();
                        msg.obj = calendarName + "";
                        handler.sendMessage(msg);
                    }                       
                }
            }).start();
        }
    });

}


Handler handler=new Handler()
{
    public void handleMessage(Message msg)
    {
        String str=(String)msg.obj;
        adapter.add(charSequence);

    }
};

Upvotes: 1

Brian Dupuis
Brian Dupuis

Reputation: 8176

You don't say where it crashes or how, but I imagine it might be due to you trying to update the UI from a non-UI thread. Take a look at the AsyncTask (see here) for information.

Upvotes: 1

Related Questions