Android 从单独的类调用asynctask时,asynctask中未显示进度对话框
我已经发布了一个问题 但我还没有得到正确的答案。我已经使用异步任务来显示进度对话框,但它没有显示。 下面是示例代码Android 从单独的类调用asynctask时,asynctask中未显示进度对话框,android,json,Android,Json,我已经发布了一个问题 但我还没有得到正确的答案。我已经使用异步任务来显示进度对话框,但它没有显示。 下面是示例代码 public class JsonData extends AsyncTask<String, String, String> { private ProgressDialog mProgressDialog; Context context; public JsonData(Context context) { this.context=cont
public class JsonData extends AsyncTask<String, String, String> {
private ProgressDialog mProgressDialog;
Context context;
public JsonData(Context context)
{
this.context=context;
mProgressDialog = new ProgressDialog(context);
mProgressDialog.setMessage("Loading Please Wait.");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(true);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog.show();
}
@Override
protected String doInBackground(String... aurl) {
String results="";
try {
int k=0;
URL url1;
url1 = new URL(aurl[0]);
InputStream input=url1.openStream();
BufferedInputStream bis=new BufferedInputStream(input);
ByteArrayBuffer baf=new ByteArrayBuffer(1000);
while((k=bis.read())!=-1)
{
baf.append((byte)k);
}
results=new String(baf.toByteArray());
} catch (Exception e) {
e.printStackTrace();
}
return results;
}
@Override
protected void onPostExecute(String jsondata) {
mProgressDialog.dismiss();
}
}
公共类JsonData扩展异步任务{
private ProgressDialog mProgressDialog;
语境;
公共JsonData(上下文)
{
this.context=context;
mProgressDialog=新建进度对话框(上下文);
setMessage(“正在加载,请稍候”);
mProgressDialog.setUndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_水平);
mProgressDialog.setCancelable(真);
}
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
mProgressDialog.show();
}
@凌驾
受保护的字符串背景(字符串…aurl){
字符串结果=”;
试一试{
int k=0;
URL url1;
url1=新URL(aurl[0]);
InputStream输入=url1.openStream();
BufferedInputStream bis=新的BufferedInputStream(输入);
ByteArrayBuffer baf=新ByteArrayBuffer(1000);
而((k=bis.read())!=-1)
{
baf.append((字节)k);
}
结果=新字符串(baf.toByteArray());
}捕获(例外e){
e、 printStackTrace();
}
返回结果;
}
@凌驾
受保护的void onPostExecute(字符串jsondata){
mProgressDialog.disclose();
}
}
下面是我调用异步任务的方法
private void getRecordsByCount(final String data) {
try {
int color=Color.BLACK;
tableLayoutGrid.removeAllViews();
final String[] details = data.split("_");
SimpleDateFormat df = new SimpleDateFormat("MM/dd/yyyy");
String formattedDate = df.format(new Date());
String url = ipaddress + "/GrantLeavesList?Companyid=" + user_info.get("CompanyId") + "&divisionid=" + details[3] + "&userid=" + user_info.get("userid") + "&roleid="
+ user_info.get("RoleId") + "&Employeeid=" + user_info.get("EmployeeId") + "&leavetypeid=" + staticDetails.get(details[0]) + "&strStatus=" + staticDetails.get(details[1])
+ "&type=" + staticDetails.get(details[2]) + "&Date=" + formattedDate;
String url2=ipaddress + "/GrantLeavesChildList?Companyid=" + user_info.get("CompanyId") + "&divisionid=" + details[3] + "&userid=" + user_info.get("userid") + "&roleid="
+ user_info.get("RoleId") + "&Employeeid=" + user_info.get("EmployeeId") + "&leavetypeid=" + staticDetails.get(details[0]) + "&strStatus=" + staticDetails.get(details[1])
+ "&type=" + staticDetails.get(details[2]) + "&Date=" + formattedDate;
JsonData jdata=new JsonData(context);
jdata.execute(url,null,null);
String jsonString=jdata.get();
JSONObject obj=new JSONObject(jsonString);
JsonData jdataChild=new JsonData(context);
jdataChild.execute(url2,null,null);
String jsonChildString=jdataChild.get();
JSONObject objchild=new JSONObject(jsonChildString);
btnGrantSubmit.setVisibility(View.GONE);
if (obj != null) {
leaveforwardcounts = obj.getJSONArray("Table1");
leaveforwardchildcounts=objchild.getJSONArray("Table11");
ScrollView scrollGrid = new ScrollView(this);
TableRow datarow = new TableRow(this);
datarow.setWeightSum(100);
TableLayout table = new TableLayout(this);
for (int i = 0; i < leaveforwardcounts.length(); i++) {
btnGrantSubmit.setVisibility(View.VISIBLE);
JSONObject record = leaveforwardcounts.getJSONObject(i);
String applicantname = record.getString("Applicant");
String toDate = record.getString("ToDate");
String noofdays = record.getString("NumberOfDays");
String LOP = record.getString("LOP");
if(LOP!=null && LOP.trim().length()!=0)
{
color=Color.RED;
}
final int id = i;
final Button gridbutton = new Button(this);
gridbutton.setText(status);
gridbutton.setTextColor(Color.BLACK);
gridbutton.setBackgroundResource(R.drawable.grdbutton_30x30);
gridbutton.setGravity(Gravity.CENTER);
gridbutton.setPadding(2, 0, 2, 0);
gridbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
changeRadioButtonState(gridbutton, id, data);
}
});
gridbutton.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
setSelection(gridbutton);
return true;
}
});
TextView tvApplicantName = new TextView(this);
TextView tvToDate = new TextView(this);
TextView tvNoOfDays = new TextView(this);
TextView empty = new TextView(this);
TextView empty2 = new TextView(this);
if (applicantname.trim().length() >= 18) {
applicantname = applicantname.substring(0, 18);
}
tvApplicantName.setText(applicantname);
tvApplicantName.setTypeface(font2);
tvApplicantName.setWidth(70);
tvApplicantName.setTextColor(color);
tvApplicantName.setPadding(5, 0, 0, 0);
tvToDate.setText(toDate);
tvToDate.setTypeface(font2);
tvNoOfDays.setText(noofdays);
tvNoOfDays.setTypeface(font2);
tvNoOfDays.setGravity(Gravity.RIGHT);
Button ivDetails = new Button(this);
ivDetails.setText(" ");
ivDetails.setPadding(2, 0, 2, 0);
ivDetails.setBackgroundResource(R.drawable.detailsbutton_30x30);
ivDetails.setGravity(Gravity.CENTER);
ivDetails.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
leaveDetails = new PopupWindow(showLeaveDetails(id, leaveforwardcounts,data,leaveforwardchildcounts), (int) (width * 0.8), height / 2, true);
leaveDetails.showAtLocation(mainlayout, Gravity.CENTER, 0, 0);
}
});
TableRow row = new TableRow(this);
row.setPadding(0, 3, 0, 3);
row.setWeightSum(100);
row.addView(tvApplicantName, new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 55));
row.addView(tvNoOfDays, new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 5));
row.addView(empty2, new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 20));
row.addView(ivDetails, new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 5));
row.addView(empty, new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 5));
row.addView(gridbutton, new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 5));
table.addView(row);
}
scrollGrid.addView(table);
datarow.addView(scrollGrid, new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT, 100));
tableLayoutGrid.addView(datarow);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void getRecordsByCount(最终字符串数据){
试一试{
int color=color.BLACK;
tableLayoutGrid.removeAllViews();
最终字符串[]详细信息=data.split(“”);
SimpleDataFormat df=新的SimpleDataFormat(“MM/dd/yyyy”);
字符串formattedDate=df.format(new Date());
字符串url=ipaddress+“/GrantLeavesList?Companyid=“+user\u info.get(“Companyid”)+”&divisionid=“+details[3]+”&userid=“+user\u info.get(“userid”)+”&roleid=”
+user_info.get(“RoleId”)+”和Employeeid=“+user_info.get(“Employeeid”)+”和leavetypeid=“+staticDetails.get(details[0])+”&strStatus=“+staticDetails.get(details[1]”)
+“&type=“+staticDetails.get(详细信息[2])+”&Date=“+formattedDate;
字符串url2=ipaddress+“/GrantLeavesChildList?Companyid=“+user\u info.get”(“Companyid”)+”&divisionid=“+details[3]+”&userid=“+user\u info.get”(“userid”)+”&roleid=”
+user_info.get(“RoleId”)+”和Employeeid=“+user_info.get(“Employeeid”)+”和leavetypeid=“+staticDetails.get(details[0])+”&strStatus=“+staticDetails.get(details[1]”)
+“&type=“+staticDetails.get(详细信息[2])+”&Date=“+formattedDate;
JsonData jdata=新JsonData(上下文);
execute(url,null,null);
字符串jsonString=jdata.get();
JSONObject obj=新的JSONObject(jsonString);
JsonData jdataChild=新JsonData(上下文);
execute(url2,null,null);
字符串jsonChildString=jdataChild.get();
JSONObject objchild=新的JSONObject(jsonChildString);
btnGrantSubmit.setVisibility(View.GONE);
如果(obj!=null){
leaveforwardcounts=对象getJSONArray(“表1”);
leaveforwardchildcounts=objchild.getJSONArray(“表11”);
ScrollView scrollGrid=新的ScrollView(此);
TableRow datarow=新的TableRow(此);
datarow.setWeightSum(100);
TableLayout table=新的TableLayout(本);
对于(int i=0;i=18){
applicationName=applicationName.substring(0,18);
}
tvapplicationname.setText(applicationname);
TvAppliantName.setTypeface(font2);
TvAppliantName.setWidth(70);
runOnUiThread(new Runnable() {
public void run() {
mProgressDialog.show();
}
});
@Override
protected void onPreExecute()
{
mProgressDialog =ProgressDialog.show(GmailInbox.this, "", "Please Wait",true,false);
super.onPreExecute();
}
@Override
protected void onPreExecute() {
super.onPreExecute();
ProgressDialog mProgressDialog = ProgressDialog.show(ActivityName.this, "Wait", "Loading....");
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (mProgressDialog != null || mProgressDialog.isShowing())
mProgressDialog.dismiss();
}
//pseudo code
void exampleButtonClicked() {
new AsyncImageDownloader.execute();
}
class AsyncImageDownloader extends AsyncTask {
onPreExecute() {
show "downloading";
}
doInBackground() {
downloadImg();
}
onPostExecute() {
hide "downloading";
put downloaded img on ImageView;
}
}
//end of pseudo code
public JsonData(YourActivityClass activity)
{
this.activity=activity;
mProgressDialog = new ProgressDialog(activity);
}
protected void onPostExecute(String jsondata) {
if (mProgressDialog != null || mProgressDialog.isShowing()){
mProgressDialog.dismiss();
}
if(jsondata!=null) {
activity.yourcallback(jsondata)
}
}
private void yourcallback(String data) {
jsonRecordsData=data;
showRecordsFromJson();
}