进度对话框不显示从单独的线程类,同时获得数据线程、对话框、进度、数据

2023-09-07 15:28:46 作者:做沵世界d王

我使用单独的线程来从URL的JSON对象。但问题是,我想显示进度对话框而获得的结果。我创建进度对话框,并呼吁展前和线程执行后解散。但进度对话框不显示。 这里是我打电话给我的线程的方式。

 私人无效getRecordsByCount(最终字符串数据){
        尝试 {
            // btnSelectall.setText();
            INT颜色= Color.BLACK;
            showProgressDialog();
            tableLayoutGrid.removeAllViews();
            最终的String []细节= data.split(_);
            SimpleDateFormat的DF =新的SimpleDateFormat(MM / DD / YYYY);
            字符串formattedDate = df.format(新日期());
            字符串URL = ip地址+/ GrantLeavesList Companyid =?+ user_info.get(CompanyId)+&放大器; divisionid =+细节[3] +&放大器;用户id =+ user_info.get(用户id)+与& roleid =
                    + user_info.get(RoleId)+&安培; EMPLOYEEID =+ user_info.get(雇员)+&安培; leavetypeid =+ staticDetails.get(详情[0])+与& strStatus =+ staticDetails.get(详细[1])
                    +&放大器;类型=+ staticDetails.get(详情[2])+&放大器;日期=+ formattedDate;
            JsonThread线程=新JsonThread(URL);
            thread.start();
            // thread.setPriority(Thread.MAX_PRIORITY);
            的Thread.join();
            JSONObject的OBJ = thread.returnResult();
            btnGrantSubmit.setVisibility(View.GONE);
            如果(OBJ!= NULL){

                leaveforwardcounts = obj.getJSONArray(表1);

                滚动型scrollGrid =新的滚动型(本);
                数据行的TableRow =新的TableRow(本);
                datarow.setWeightSum(100);
                TableLayout表=新TableLayout(本);
                的for(int i = 0; I< leaveforwardcounts.length();我++){
                    btnGrantSubmit.setVisibility(View.VISIBLE);
                    JSONObject的纪录= leaveforwardcounts.getJSONObject(我);
                    字符串applicantname = record.getString(申请人);
                    串TODATE = record.getString(TODATE);
                    串noofdays = record.getString(NUMBEROFDAYS);
                    字符串LOP = record.getString(LOP);
                    如果(LOP =空&安培;!&安培;!LOP.trim()长度()= 0)
                    {
                        颜色= Color.RED;
                    }
                    最终诠释ID =我;
                    最终按钮gridbutton =新的按钮(这一点);
                    gridbutton.setText(状态);
                    gridbutton.setTextColor(Color.BLACK);
                    gridbutton.setBackgroundResource(R.drawable.grdbutton_30x30);
                    gridbutton.setGravity(Gravity.CENTER);
                    gridbutton.setPadding(2,0,2,0);
                    gridbutton.setOnClickListener(新View.OnClickListener(){
                        @覆盖
                        公共无效的onClick(视图v){
                            changeRadioButtonState(gridbutton,ID,数据);
                        }
                    });
                    gridbutton.setOnLongClickListener(新OnLongClickListener(){

                        @覆盖
                        公共布尔onLongClick(视图v){
                            setSelection(gridbutton);
                            返回true;
                        }
                    });

                    TextView的tvApplicantName =新的TextView(本);

                    TextView的tvToDate =新的TextView(本);
                    TextView的tvNoOfDays =新的TextView(本);
                    TextView的空=新的TextView(本);
                    TextView的empty2 =新的TextView(本);
                    如果(applicantname.trim()长度()> = 18){
                        applicantname = applicantname.substring(0,18);
                    }

                    tvApplicantName.setText(applicantname);
                    tvApplicantName.setTypeface(font2);
                    tvApplicantName.setWidth(70);
                    tvApplicantName.setTextColor(颜色);
                    tvApplicantName.setPadding(5,0,0,0);

                    tvToDate.setText(TODATE);
                    tvToDate.setTypeface(font2);
                    tvNoOfDays.setText(noofdays);
                    tvNoOfDays.setTypeface(font2);
                    tvNoOfDays.setGravity(Gravity.RIGHT);

                    按钮ivDetails =新的按钮(这一点);
                    ivDetails.setText();
                    ivDetails.setPadding(2,0,2,0);
                    ivDetails.setBackgroundResource(R.drawable.detailsbutton_30x30);
                    ivDetails.setGravity(Gravity.CENTER);

                    ivDetails.setOnClickListener(新View.OnClickListener(){

                        @覆盖
                        公共无效的onClick(视图v){
                            leaveDetails =新PopupWindow(showLeaveDetails(ID,leaveforwardcounts,数据),(INT)(宽* 0.8),身高/ 2,真正的);
                            leaveDetails.showAtLocation(mainlayout,Gravity.CENTER,0,0);
                        }
                    });

                    的TableRow行=新的TableRow(本);

                    row.setPadding(0,3,0,3);
                    row.setWeightSum(100);
                    row.addView(tvApplicantName,新TableRow.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,55));
                    row.addView(tvNoOfDays,新TableRow.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,5));
                    row.addView(empty2,新TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,20));
                    row.addView(ivDetails,新TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,5));
                    row.addView(空,新TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,5));
                    row.addView(gridbutton,新TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,5));
                    table.addView(行);
                }
                scrollGrid.addView(表);
                datarow.addView(scrollGrid,新TableRow.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,100));
                tableLayoutGrid.addView(数据行);
                dialog.dismiss();
            }
        }赶上(例外五){
            e.printStackTrace();
        }
    }



包com.MobJax.dashboard;

进口java.io.BufferedInputStream中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.net.MalformedURLException;
进口的java.net.URL;

进口org.apache.http.util.ByteArrayBuffer;
进口org.json.JSONObject;


公共类JsonThread继承Thread {
    布尔运行= FALSE;
    字符串URL;
    JSONObject的jobj = NULL;

    公共JsonThread(字符串URL)
    {
        this.url =网址;
    }
    @覆盖
    公共无效的run(){
        INT K = 0;

        URL URL1;
        尝试 {

            为url1 =新的URL(网址);
            输入的InputStream = url1.openStream();
            的BufferedInputStream双=新的BufferedInputStream(输入);
            ByteArrayBuffer BAF =新ByteArrayBuffer(1000);
            而((K = bis.read())!=  -  1)
            {
            baf.append((字节)K);

            }
            字符串结果=新的String(baf.toByteArray());
              jobj =新的JSONObject的(结果);
            }
        赶上(MalformedURLException的E1){
            // TODO自动生成的catch块
            e1.printStackTrace();
        }
        赶上(IOException异常E){
            // TODO自动生成的catch块
            e.printStackTrace();
        }
        赶上(例外五)
        {
            e.printStackTrace();
        }

    }
    公众的JSONObject returnResult()
    {
        返回jobj;
    }

}
 

解决方案

您可以使用的AsyncTask ..by增加螺纹部分进入doInBackground method..show的进展preExecute对话框中,并关闭它在onPostExecute ..

例如:

 公共类MyTask的扩展AsyncTask的<字符串,太虚,字符串> {

私人上下文的背景下;
私人ProgressDialog progressDialog;

公共MyTask的(上下文的背景下){
    this.context =背景;
}

@覆盖
在preExecute保护无效(){
        progressDialog = ProgressDialog.show(背景下,,载入中...,真正的);
}

/ *
 * @see android.os.AsyncTask#doInBackground(PARAMS [])
 * /
@覆盖
保护字符串doInBackground(字符串...为arg0){
    //你的json的螺纹部分
}

@覆盖
保护无效onPostExecute(最后弦乐结果){
        progressDialog.dismiss();
}
}
 
英雄联盟在显示完游戏进度后然后出来个对话框就不能玩了 求借

I am using separate thread to get the json object from the url. but the problem is I would like to show the progress dialog while getting the result. I have create a progress dialog and called show and dismiss before and after thread execution. but progress dialog is not displaying. here is the way I have called my thread.

    private void getRecordsByCount(final String data) {
        try {
            // btnSelectall.setText(" ");
            int color=Color.BLACK;
            showProgressDialog();
            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;
            JsonThread thread = new JsonThread(url);
            thread.start();
            // thread.setPriority(Thread.MAX_PRIORITY);
            thread.join();
            JSONObject obj = thread.returnResult();
            btnGrantSubmit.setVisibility(View.GONE);
            if (obj != null) {

                leaveforwardcounts = obj.getJSONArray("Table1");

                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), (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);
                dialog.dismiss();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



package com.MobJax.dashboard;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import org.apache.http.util.ByteArrayBuffer;
import org.json.JSONObject;


public class JsonThread extends Thread{
    boolean running=false;
    String url;
    JSONObject jobj=null;

    public JsonThread(String url)
    {
        this.url=url;
    }
    @Override
    public void run() {
        int k=0;

        URL url1;
        try {

            url1 = new URL(url);
            InputStream input=url1.openStream();
            BufferedInputStream bis=new BufferedInputStream(input);
            ByteArrayBuffer baf=new ByteArrayBuffer(1000);
            while((k=bis.read())!=-1)
            {
            baf.append((byte)k);

            }
            String results=new String(baf.toByteArray());
              jobj=new JSONObject(results);
            } 
        catch (MalformedURLException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } 
        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }

    }
    public JSONObject returnResult()
    {
        return jobj;
    }

}

解决方案

You can do it using AsyncTask..by adding the thread part into the doInBackground method..show the progress dialog in onPreExecute and dismiss it in onPostExecute..

ex:

public class MyTask extends AsyncTask<String, Void, String> {

private Context context;
private ProgressDialog progressDialog;

public MyTask (Context context) {
    this.context = context;
}

@Override
protected void onPreExecute() {
        progressDialog = ProgressDialog.show(context, "", "Loading...", true);
}

/* 
 * @see android.os.AsyncTask#doInBackground(Params[])
 */
@Override
protected String doInBackground(String... arg0) {
    //your json thread part
}

@Override
protected void onPostExecute(final String result) {
        progressDialog.dismiss();
}
}