1

我正在使用在它自己的类中定义的自定义 OnClickListener 创建一个 android 应用程序。问题是当我想在标题栏中创建 Indeterminate Progress Bar 时,它将在调用 onClick 方法时启动。我无法从 MyOnClickListener 类中设置ProgressBarIntederminateVisibility,因为它不是主要活动,我无法请求 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;
        }

    }

}

我能做些什么?

4

2 回答 2

2

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

于 2013-01-28T15:05:33.680 回答
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;
            }
    }
于 2015-04-08T08:52:39.583 回答