屏幕中的活动指示器

时间:2012-11-28 10:13:39

标签: android widget activity-indicator

在我的应用程序中,我想显示一个活动指示器。在我的应用程序中,我有一个galleryview,因为如果用户点击galleryview中的项目,我将从URL加载数据并在listview中显示它。在加载和显示数据之间我需要一个指标。

我的活动代码:

gal.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position,
                long id) {
            // TODO Auto-generated method stub  

            context.getInstance().setAppVariable("sectionurl", adapter.sectionurl[position]);
            ListView list=(ListView)findViewById(R.id.list);
            listadapter = new ListViewwithimageAdapter(this);
            list.setAdapter(listadapter);

        }
    }); 

我还找到了1个指标代码:

ProgressDialog mDialog = new ProgressDialog(contxt);
      mDialog.setMessage("Please wait...");
      mDialog.setCancelable(true);
      mDialog.show();

我的getview代码:

public View getView(int position, View convertView, ViewGroup parent) 
    {

         ProgressDialog mDialog = new ProgressDialog(contxt);
         mDialog.setMessage("Please wait...");
         mDialog.setCancelable(false);
         mDialog.show();
        // TODO Auto-generated method stub      
         Bitmap bitmap = DownloadImage(
                 kickerimage[position] );        
//       View listView = convertView;
         if (convertView   == null) 
            {
                //this should only ever run if you do not get a view back            
             LayoutInflater  inflater = (LayoutInflater) contxt
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
             convertView  = inflater.inflate(R.layout.homelistrow, null); 
            }
//       else
//       {
//           holder.removeAllViews();
//       }
//      View listView;
//      if (convertView == null) 
//      {
//          listView = new View(contxt);             
//          LinearLayout holder = (LinearLayout)convertView.findViewById(android.R.id);
////            holder = inflater.inflate(R.layout.homelistrow, null); 
//          System.out.println("pos="+position);
//          System.out.println("item="+getItem(position));
//       else
//       {
            TextView textView = (TextView) convertView
                    .findViewById(R.id.name_label);
            textView.setText(itemsarray[position]);

            ImageView imageView = (ImageView) convertView
                    .findViewById(R.id.icon);
            imageView.setImageBitmap(bitmap);
//      }
//      else        
//      {
//          listView = (View) convertView;
//      } 
//       }
            mDialog.dismiss();
        return convertView ;        
    }

我的Logcat:

11-28 16:01:55.589: D/AndroidRuntime(788): Shutting down VM
11-28 16:01:55.589: W/dalvikvm(788): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
11-28 16:01:55.589: E/AndroidRuntime(788): Uncaught handler: thread main exiting due to uncaught exception
11-28 16:01:55.650: E/AndroidRuntime(788): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
11-28 16:01:55.650: E/AndroidRuntime(788):  at android.view.ViewRoot.setView(ViewRoot.java:472)
11-28 16:01:55.650: E/AndroidRuntime(788):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:177)
11-28 16:01:55.650: E/AndroidRuntime(788):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
11-28 16:01:55.650: E/AndroidRuntime(788):  at android.app.Dialog.show(Dialog.java:239)
11-28 16:01:55.650: E/AndroidRuntime(788):  at com.newspaper.NewsPaperNov28MainGalleryActivity$1.onItemClick(NewsPaperNov28MainGalleryActivity.java:68)
11-28 16:01:55.650: E/AndroidRuntime(788):  at android.widget.AdapterView.performItemClick(AdapterView.java:284)
11-28 16:01:55.650: E/AndroidRuntime(788):  at android.widget.Gallery.onSingleTapUp(Gallery.java:864)
11-28 16:01:55.650: E/AndroidRuntime(788):  at android.view.GestureDetector.onTouchEvent(GestureDetector.java:506)
11-28 16:01:55.650: E/AndroidRuntime(788):  at android.widget.Gallery.onTouchEvent(Gallery.java:839)
11-28 16:01:55.650: E/AndroidRuntime(788):  at android.view.View.dispatchTouchEvent(View.java:3709)
11-28 16:01:55.650: E/AndroidRuntime(788):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:852)
11-28 16:01:55.650: E/AndroidRuntime(788):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
11-28 16:01:55.650: E/AndroidRuntime(788):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
11-28 16:01:55.650: E/AndroidRuntime(788):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
11-28 16:01:55.650: E/AndroidRuntime(788):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
11-28 16:01:55.650: E/AndroidRuntime(788):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
11-28 16:01:55.650: E/AndroidRuntime(788):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
11-28 16:01:55.650: E/AndroidRuntime(788):  at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
11-28 16:01:55.650: E/AndroidRuntime(788):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
11-28 16:01:55.650: E/AndroidRuntime(788):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
11-28 16:01:55.650: E/AndroidRuntime(788):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-28 16:01:55.650: E/AndroidRuntime(788):  at android.os.Looper.loop(Looper.java:123)
11-28 16:01:55.650: E/AndroidRuntime(788):  at android.app.ActivityThread.main(ActivityThread.java:4363)
11-28 16:01:55.650: E/AndroidRuntime(788):  at java.lang.reflect.Method.invokeNative(Native Method)
11-28 16:01:55.650: E/AndroidRuntime(788):  at java.lang.reflect.Method.invoke(Method.java:521)
11-28 16:01:55.650: E/AndroidRuntime(788):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
11-28 16:01:55.650: E/AndroidRuntime(788):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
11-28 16:01:55.650: E/AndroidRuntime(788):  at dalvik.system.NativeStart.main(Native Method)
11-28 16:01:55.739: I/dalvikvm(788): threadid=7: reacting to signal 3
11-28 16:01:55.739: I/dalvikvm(788): Wrote stack trace to '/data/anr/traces.txt'

实际上我把这段代码放在我的getview()的适配器类中。但指标并没有结束它总是显示..请帮助我..提前谢谢..

2 个答案:

答案 0 :(得分:3)

您需要致电

mDialog.dismiss();
加载完成后

编辑:

我正在添加用于类似任务的代码。

//This is my AsyncTask which needed some time
new InsertDbTask().execute();


//Actual task
public class InsertDbTask extends AsyncTask<Void, Void, Void> {
        private final ProgressDialog dialog = new ProgressDialog(mContext);

        @Override
        protected Void doInBackground(Void... params) {
            // TODO Auto-generated method stub
            try {
                prepareInsert();
            } catch (Exception e) {
                // Log.e("Error: ", e.getMessage());
            }
            return null;
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            this.dialog.setCanceledOnTouchOutside(false);
            this.dialog.setMessage("Inserting Record In Local DB Please Wait.");
            this.dialog.show();
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);

            if (this.dialog.isShowing()) {
                this.dialog.dismiss();
            }
            saveButton.setEnabled(true);    // enable save button after execution of prepareInsert().

        }

    }

public void prepareInsert()
{
      // Do something 
}

答案 1 :(得分:0)

您需要在gal.setOnItemClickListener&amp;中放置进度对话框代码。当加载列表时,忽略该进度对话框。