pfulop
pfulop

Reputation: 1009

Android ProgressBar started from non-Activity class

I am creating an android application with custom OnClickListener that's defined in it's own class. The problem is when I want to create Indeterminate Progress Bar in title bar that will be started when the onClick method will be called. I can't setProgressBarIntederminateVisibility from MyOnClickListener class because it's not the main activity, and I can't request getParent because it's not a activity.

public class MyOnClickListener implements OnClickListener {
    private message;

    public MyOnClickListener(Context context,TextView mstatus, TextView message) {
        this.message=message;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id){
        case R.id.next:
            this.message.setText(GetValue.getNextValue());//during this operation I want progress bar to be spinning
            Log.v("MyOnClickListener", "next pressed");
            break;
        case R.id.prev:
            this.message.setText(GetValue.getPrevValue());
            Log.v("MyOnClickListener","prev pressed");
            break;
        case R.id.last:
            this.message.setText(GetValue.getlastvalue());
            break;
        default: break;
        }

    }

}

What Can I do?

Upvotes: 1

Views: 1393

Answers (2)

JanB
JanB

Reputation: 914

In case it helps someone else, I have an activity that calls a non-activity class which downloads an xml data file from a server. I wanted to show a progress indicator. I did it like this (this isn't a complete implementation of the classes but should give a good idea):

     /*Setup Activity Class */
            public class MySetup extends Activity implements ThreadCompleteListener{
                Button btnLogin, btnItems, btnConfig, btnStart;
                ProgressDialog pDialog;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.setup_activity);
                //set up links to buttons, etc
             }

           public void onBtnClicked(View v){
            Intent intent;
            switch(v.getId()){
                case R.id.btn_download_items: 
                    final Context ctx = this;
                    startProgressIndicator(); 
                    //async read a list of items from a server
                    myList=ItemsList.getItemsListInstance(ctx);
                    btnConfig.setEnabled(true);
                    break;
              //other options here 
           }
        }

        public void startProgressIndicator(){
           pDialog = new ProgressDialog(this);
           pDialog.setMessage("Downloading items...");
           pDialog.setIndeterminate(false);
           pDialog.setCancelable(false);
           pDialog.show();
       }

       public void endProgressIndicator(){
         pDialog.dismiss();
      }

}

Then in my non-activity class - I do the download

       public class ItemsList implements ThreadCompleteListener{
            //create a list of MyItem
            private ArrayList<MyItem> items = new ArrayList<MyItem>();
            //create a single static instance
            private static ItemsList itemsListInstance;
            String baseUrl; //to connect to webservice
            Context ctx;


            public ItemsList(Context context){
                readJSONFeed("http://someurl/", context);
                ctx=context;        
            }

        public  void readJSONFeed(String theURL, Context context) {
                //Read JSON string from URL

                final Context ctx = context;
                setBaseUrl(); 
                //NotifyThread is a class I found in a Stack Overflow answer
                //that provides a simple notification when the async process has completed
                NotifyThread getItemsThread = new NotifyThread(){
                     @Override
                     public void doRun(){
                         try {
                             //do the JSON read stuff here...
                         }catch (Exception e) {
                         } 
                     };
                     getItemsThread.addListener(this);
                     getItemsThread.start();
                }

//Overload the NotifyThread method 
                public void notifyOfThreadComplete(final Thread thread){
                //CALL the method in the calling activity to stop the progress indicator
                    ((MySetup)ctx).endProgressIndicator();
               }

            public  static AuctionItemsList getItemsListInstance(Context context)         {
                if (itemsListInstance == null){
                    itemsListInstance = new itemsList(context);
                }
                return itemsListInstance;
            }
    }

Upvotes: 0

class stacker
class stacker

Reputation: 5347

Typically, you'd have this as an inner class of your Activity, which then has an implicit reference to your "outer" Activity class. As an alternative, you can of course pass a reference to your Activity when you construct your listener object.

Upvotes: 2

Related Questions