在基础适配器中设置进度对话框
Set the progress dialog in base adapter
这是我的列表视图适配器,我在其中将列表视图数据发送到按钮时钟异步任务上的服务器 ans sqlite。我想使用进度对话框,但我在传递 activity 的实例时遇到问题,我的代码在进度对话框中崩溃。
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();
}
}
}
我无法弄清楚问题所在。抱歉,代码太多,提前致谢。
记录猫错误
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.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)
改为在您的 preExecute 方法中:
progressDialog = new ProgressDialog(mcontext);
这样写:
progressDialog = new ProgressDialog(context);
请传递上下文
new SaveDCR(context).execute(dvr);
看看这一行。
new SaveDCR().execute(dvr);
这是你的问题。
不是传递上下文,而是不传递任何内容给 Constuctor。
改为
new SaveDCR(context).execute(dvr);
问题可能在于如何获取上下文。请更改
new PlannningListViewAdapter(getApplicationContext(),list);
至:
new PlannningListViewAdapter(this,list);
在你的Activity
这是我的列表视图适配器,我在其中将列表视图数据发送到按钮时钟异步任务上的服务器 ans sqlite。我想使用进度对话框,但我在传递 activity 的实例时遇到问题,我的代码在进度对话框中崩溃。
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();
}
}
}
我无法弄清楚问题所在。抱歉,代码太多,提前致谢。 记录猫错误
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.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)
改为在您的 preExecute 方法中:
progressDialog = new ProgressDialog(mcontext);
这样写:
progressDialog = new ProgressDialog(context);
请传递上下文
new SaveDCR(context).execute(dvr);
看看这一行。
new SaveDCR().execute(dvr);
这是你的问题。 不是传递上下文,而是不传递任何内容给 Constuctor。
改为
new SaveDCR(context).execute(dvr);
问题可能在于如何获取上下文。请更改
new PlannningListViewAdapter(getApplicationContext(),list);
至:
new PlannningListViewAdapter(this,list);
在你的Activity