Android ProgressBar从非Activity类开始

时间:2013-01-28 15:01:15

标签: android android-activity android-progressbar

我正在创建一个带有自定义OnClickListener的Android应用程序,该应用程序在其自己的类中定义。 问题是当我想在标题栏中创建Indeterminate Progress Bar时,将在调用onClick方法时启动它。 我无法从MyOnClickListener类中设置setProgressBarIntederminateVisibility,因为它不是主要活动,我不能请求getParent,因为它不是活动。

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;
        }

    }

}

我该怎么办?

2 个答案:

答案 0 :(得分:2)

通常,您将此作为Activity的内部类,然后对您的“外部”Activity类进行隐式引用。作为替代方案,您当然可以在构造侦听器对象时传递对Activity的引用。

答案 1 :(得分:0)

如果它帮助其他人,我有一个活动调用非活动类,从服务器下载xml数据文件。我想展示进度指标。我是这样做的(这不是一个完整的类实现,但应该给出一个好主意):

     /*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();
      }

}

然后在我的非活动课程中 - 我进行下载

       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;
            }
    }