Set the progress dialog in base adapter

2019-09-20 10:27发布

问题:

This is my list view adapter in which I am sending listview data to server ans sqlite on the button clock asyn task. I want to use a progress dialog but I'm having problem while passing the activity's instance and my code crashes on the progress dialog.

   public class PlannningListViewAdapter extends BaseAdapter{

   Context context;
   public ArrayList<Planning> planArraylist;
   private static LayoutInflater inflater = null;
   public PlanningList_activity planActivity;
   public ListView mListview;
   Db_sqlite databaseHelper;
   Calendar calObj = Calendar.getInstance();
   DateFormat cdf = new SimpleDateFormat("MM-dd-yyyy hh:mm:ss a");
   String currentDate = cdf.format(calObj.getTime()).toString();

  public PlannningListViewAdapter(Context context,ArrayList<Planning>   planArraylist) {
     this.context = context;
     this.planArraylist = planArraylist;
     inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     databaseHelper = new Db_sqlite(context);

}
@Override
public int getCount() {
    return planArraylist.size();
}
@Override
 public Planning getItem(int arg0) {
    return planArraylist.get(arg0);
} 

@Override
public long getItemId(int arg0) {
    return arg0;
}
 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
           ViewHolder holder;

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.single_lv_item, parent, false);
  holder = new ViewHolder();

            holder.tvdrCode =(TextView)onvertView.findViewById(R.id.Plan_no);
            holder.tvDrName = (TextView) convertView.findViewById(R.id.doc_name);
            holder.tvterrcode= (TextView) convertView.findViewById(R.id.mon);
            holder.tvAdr = (TextView) convertView.findViewById(R.id.adr);
            holder.tvClassDesc = (TextView) convertView.findViewById(R.id.class_desc);
            holder.tvspeciality = (TextView) convertView.findViewById(R.id.ff_code);
            holder.tvAreaName = (TextView) convertView.findViewById(R.id.area_name);
            holder.tvShift = (TextView) convertView.findViewById(R.id.shift);
            holder.btn_visited =(Button)convertView.findViewById(R.id.button1);
            holder.btn_visited.setTag(position);
            holder.btn_visited.setOnClickListener(ButtonClickListener);

            convertView.setTag(holder);

        } 
        else {
            holder = (ViewHolder) convertView.getTag();
        }

        Planning p = planArraylist.get(position);
        holder.tvdrCode.setText(p.getDocCode());
        holder.tvDrName.setText(p.getDocName());
        holder.tvterrcode.setText(p.getTerrCode());
        holder.tvAdr.setText(p.getAdr());
        holder.tvClassDesc.setText(p.getClassDesc());
        holder.tvspeciality.setText(p.getSP());
        holder.tvAreaName.setText(p.getAreaName());
        holder.tvShift.setText(p.getMornEven());

        return convertView;
    }

     static class ViewHolder {
         TextView tvdrCode;
         TextView tvDrName;
         TextView tvterrcode;
         TextView tvAdr;
         TextView tvClassDesc;
         TextView tvspeciality;
         TextView tvAreaName;
         TextView tvShift;
         Button btn_visited;
    }
  private OnClickListener ButtonNotVistedClickListener = new OnClickListener(){

         public void onClick(View v) {
             final  int position = (Integer) v.getTag(); //2nd method  String d = planArraylist.get(position).getDocCode();
             Planning planning = getItem(position); 

              String replan_no = "1";
              String drCode =  planning.getDocCode();
              String terrcode = planning.getTerrCode();

              String prev_plan_date = currentDate;
              String replan_date = currentDate;
              String shift = planning.getMornEven();
              String created_date =currentDate;
              String nvr_no = "783882";
              String Sffcode = PlanningList_activity.FFCODE;
              String Sffmgr= PlanningList_activity.FFMGR;


            Intent intent=new Intent(context.getApplicationContext(),NotVisited_Activtiy.class); // goto to attendance class
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

            Bundle userBundle = new Bundle();
            userBundle.putString("DR_CODE", planning.getDocCode());
            userBundle.putString("DR_NAME",planning.getDocName());
            userBundle.putString("TERR_CODE", planning.getTerrCode());
            userBundle.putString("ADR", planning.getAdr());
            userBundle.putString("CLASS_DESC", planning.getClassDesc());
            userBundle.putString("SPECIALITY", planning.getSP());
            userBundle.putString("AREA_NAME", planning.getAreaName());
            userBundle.putString("SHIFT", planning.getMornEven());

            intent.putExtras(userBundle);

            context.startActivity(intent); 
             }
            };

    private OnClickListener ButtonClickListener = new OnClickListener() {

     public void onClick(View v) {

      final  int position = (Integer) v.getTag(); //2nd method  String d = planArraylist.get(position).getDocCode();
      Planning planning = getItem(position); 

      String dcr_date = currentDate;
      String drCode =  planning.getDocCode();
      String shift = planning.getMornEven();
      String terrcode = planning.getTerrCode();
      String created_date =currentDate;
      String ref_no = "SLS51";
      String Sffcode = PlanningList_activity.FFCODE;
      String Sffmgr= PlanningList_activity.FFMGR;

     Dcr_main dvr = new Dcr_main(dcr_date,drCode,shift,Sffcode,terrcode,Sffmgr,created_date,ref_no);
     new SaveDCR(context).execute(dvr);

    }
};

protected class SaveDCR extends AsyncTask<Dcr_main, Void, Void> {

    Context mcontext;
    ProgressDialog progressDialog;

     public SaveDCR(Context mcontext) {
            // TODO Auto-generated constructor stub
              this.mcontext = mcontext;
          }

        @Override
          protected void onPreExecute() {

              progressDialog = new ProgressDialog(mcontext);
              progressDialog.setMessage("Saving...");
              progressDialog.setCancelable(false);
              progressDialog.show();
              Log.e( "sending dvr to sqlite","yes"); 

          }  

          @Override
           protected Void doInBackground(Dcr_main... params) {
             RestAPI api = new RestAPI();
             try
             {

                // insert into device database
                databaseHelper.insertDVR(params[0].getDcr_Date(),
                        params[0].getDr_Code(),
                        params[0].getShift(),
                        params[0].getFf_code(),
                        params[0].getTerr_code(),
                        params[0].getFF_mgr(),
                        params[0].getCreatedDate(),
                        params[0].getRef_no());
                Log.e(" inserting dvr in sqlite","yes");               
                databaseHelper .close();     

                   api.DVRInsert(params[0].getDcr_Date(),
                            params[0].getDr_Code(),
                            params[0].getShift(),
                            params[0].getFf_code(),
                            params[0].getTerr_code(),
                            params[0].getFF_mgr(),
                            params[0].getRef_no());
                     Log.e( "sent dvr to server, do inbackground","yes");    

               }
            catch (Exception e) {
             // TODO Auto-generated catch block
                 Log.d("Failed to connect to server", e.getMessage());
           }
            return null;
        }


        @Override
        protected void onPostExecute(Void result) {

            if(progressDialog.isShowing()) {
                progressDialog.dismiss();


            } 
          }
        }

I cannot figure out the problem. Sorry for so much code, thanks in advance. Log cat error

02-09 12:22:51.932: E/AndroidRuntime(26893):     android.view.WindowManager$BadTokenException: Unable to add window -- token null    is not for an application
02-09 12:22:51.932: E/AndroidRuntime(26893):    at   android.view.ViewRootImpl.setView(ViewRootImpl.java:540)
02-09 12:22:51.932: E/AndroidRuntime(26893):    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
02-09 12:22:51.932: E/AndroidRuntime(26893):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
02-09 12:22:51.932: E/AndroidRuntime(26893):    at android.app.Dialog.show(Dialog.java:286)
02-09 12:22:51.932: E/AndroidRuntime(26893):    at com.example.db_client.PlannningListViewAdapter$SaveDCR.onPreExecute(PlannningListViewAdapter.java:235)
02-09 12:22:51.932: E/AndroidRuntime(26893):    at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
02-09 12:22:51.932: E/AndroidRuntime(26893):    at android.os.AsyncTask.execute(AsyncTask.java:535)
02-09 12:22:51.932: E/AndroidRuntime(26893):    at com.example.db_client.PlannningListViewAdapter$2.onClick(PlannningListViewAdapter.java:213)
02-09 12:22:51.932: E/AndroidRuntime(26893):    at android.view.View.performClick(View.java:4438)
02-09 12:22:51.932: E/AndroidRuntime(26893):    at android.view.View$PerformClick.run(View.java:18422)
02-09 12:22:51.932: E/AndroidRuntime(26893):    at android.os.Handler.handleCallback(Handler.java:733)
02-09 12:22:51.932: E/AndroidRuntime(26893):    at android.os.Handler.dispatchMessage(Handler.java:95)

回答1:

The problem is probably with way how to get context. Please change

 new PlannningListViewAdapter(getApplicationContext(),list);

to :

 new PlannningListViewAdapter(this,list);

in your Activity



回答2:

Please pass the context

new SaveDCR(context).execute(dvr);



回答3:

Look at this line.

new SaveDCR().execute(dvr);

here is your problem. Instead of passing context you passing nothing to Constuctor.

change it to

new SaveDCR(context).execute(dvr);


回答4:

In your preExecute method instead:

progressDialog = new ProgressDialog(mcontext);

Write this:

progressDialog = new ProgressDialog(context);