d如何从数据库中获取数据时使用进度条?

时间:2015-01-16 10:26:22

标签: android database progress-bar

我在搜索我想要的东西时非常糟糕所以我决定在这里问一下。我的问题是,从数据库中获取数据时是否可以显示进度条?我在获取数据时使用典型代码(将值传递给php,php将执行查询并将其再次传递给android)

编辑(我已尝试添加proggressdialog,但现在的问题是加载的数据将首先出现在进度对话框之前,这是我的AsyncTask代码)

 public class getClass extends AsyncTask<String, Void, String> {
    public  getClass()
    {
       pDialog = new ProgressDialog(getActivity());
    }
    URLConnection connection = null;
    String command;
    Context context;
    String ip = new returnIP().getIpAddresss();
    String link = "http://" + ip + "/android/getClass.php";//ip   address/localhost


    public URLConnection getConnection(String link) {
        URL url = null;
        try//retrieves link from string
        {
            url = new URL(link);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        URLConnection connection = null;
        try//opens the url link provided from the "link" variable
        {
            connection = url.openConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
        connection.setDoOutput(true);
        return connection;
    }

    public String getResult(URLConnection connection, String logs) {
        //this is the functions that retrieves what the php file echoes
        //everything that php throws, the phone receives
        String result = "";
        OutputStreamWriter wr = null;
        try {
            wr = new OutputStreamWriter(connection.getOutputStream());//compiles data to be sent to the receiver
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            wr.write(logs);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            wr.flush();//clears the cache-esque thingy of the writer
        } catch (IOException e) {
            e.printStackTrace();
        }
        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        StringBuilder sb = new StringBuilder();
        String line = null;
        //Read server response

        try {
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        result = sb.toString();

        return result;
    }

    @Override
    protected void onPreExecute() {

        pDialog.setMessage("Loading...");
        pDialog.show();

    }
    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        String result = "";
        //Toast.makeText(View_Classes.this, "ako n una", Toast.LENGTH_LONG).show();
        try {

            //first data sent is sent in command
            command = (String) arg0[0];//it's in array, because everything you input here is placed in arrays

            //Toast.makeText(View_Classes.this, "andtio n me", Toast.LENGTH_LONG).show();
            if (command == "getCourses") {
                connection = getConnection(link);
                String logs = "";
                logs = "&command=" + URLEncoder.encode(command, "UTF-8");
                logs += "&username=" + URLEncoder.encode(username, "UTF-8");
                result = getResult(connection, logs);


            } else if (command == "getSections") {

                connection = getConnection(link);
                String logs = "";
                logs = "&command=" + URLEncoder.encode(command, "UTF-8");
                logs += "&username=" + URLEncoder.encode(username, "UTF-8");
                logs += "&course=" + URLEncoder.encode(course, "UTF-8");
                result = getResult(connection, logs);
            }
            return result;
        } catch (Exception e) {
            return result;
        }
    }

    @Override
    protected void onPostExecute(String result) {//this is going to be the next function to be done after the doInBackground function
        // TODO Auto-generated method stub

        if (pDialog.isShowing()) {
            pDialog.dismiss();
        }
        if (result.equalsIgnoreCase(""))//if there's nothing to return, the text "No records" are going to be thrown
        {

        } else //Array adapter is needed, to be a place holder of values before passing to spinner
        {

        }
    }
}

5 个答案:

答案 0 :(得分:0)

您是否尝试使用AsyncTask

您可以在preExecute方法上显示进度条,然后在postExecute上隐藏它。您可以在doInBackground方法中进行查询。

答案 1 :(得分:0)

除了@ torque203指出的,我建议你检查一下 http://developer.android.com/reference/android/os/AsyncTask.html#onProgressUpdate(Progress...) 此方法是为此目的创建的,向用户显示进度。

答案 2 :(得分:0)

来自开发者文档:

private class DownloadFilesTask extends AsyncTask<URL, Integer, Long> {

 @Override
    protected void onPreExecute() {
  //show progress bar here
 }

 protected Long doInBackground(URL... urls) {
     //Pass value to PHP here
     //get values from your PHP
 }



 protected void onPostExecute(Long result) {
    //Here you are ready with your PHP value. Dismiss progress bar here.
 }
}

答案 3 :(得分:0)

 public void onPreExecute() {
   Progress Dialog pDialog = new ProgressDialog(this);
        pDialog.setMessage("Loading...");
        pDialog.show();
}

  public void doInBackground() {
  //do your JSON Coding
 }

public void onPostExecute() {
   Progress Dialog pDialog = new ProgressDialog(this);
        pDialog.setMessage("Loading...");
        pDialog.show();
} 

答案 4 :(得分:0)

public URLConnection getConnection(String link) {
    URL url = null;
    try//retrieves link from string
    {
        url = new URL(link);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    }
    URLConnection connection = null;
    try//opens the url link provided from the "link" variable
    {
        connection = url.openConnection();
    } catch (IOException e) {
        e.printStackTrace();
    }
    connection.setDoOutput(true);
    return connection;
}

public String getResult(URLConnection connection, String logs) {
    //this is the functions that retrieves what the php file echoes
    //everything that php throws, the phone receives
    String result = "";
    OutputStreamWriter wr = null;
    try {
        wr = new OutputStreamWriter(connection.getOutputStream());//compiles data to be sent to the receiver
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        wr.write(logs);
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        wr.flush();//clears the cache-esque thingy of the writer
    } catch (IOException e) {
        e.printStackTrace();
    }
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
    } catch (IOException e) {
        e.printStackTrace();
    }
    StringBuilder sb = new StringBuilder();
    String line = null;
    //Read server response

    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    result = sb.toString();

    return result;
}


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



@Override
protected void onPreExecute() {

    pDialog.setMessage("Loading...");
    pDialog.show();

    URLConnection connection = null;
String command;
Context context;
String ip = new returnIP().getIpAddresss();
String link = "http://" + ip + "/android/getClass.php";//ip   address/localhost

}
@Override
protected String doInBackground(String... arg0) {
    // TODO Auto-generated method stub
    String result = "";
    //Toast.makeText(View_Classes.this, "ako n una", Toast.LENGTH_LONG).show();
    try {

        //first data sent is sent in command
        command = (String) arg0[0];//it's in array, because everything you input here is placed in arrays

        //Toast.makeText(View_Classes.this, "andtio n me", Toast.LENGTH_LONG).show();
        if (command == "getCourses") {
            connection = getConnection(link);
            String logs = "";
            logs = "&command=" + URLEncoder.encode(command, "UTF-8");
            logs += "&username=" + URLEncoder.encode(username, "UTF-8");
            result = getResult(connection, logs);


        } else if (command == "getSections") {

            connection = getConnection(link);
            String logs = "";
            logs = "&command=" + URLEncoder.encode(command, "UTF-8");
            logs += "&username=" + URLEncoder.encode(username, "UTF-8");
            logs += "&course=" + URLEncoder.encode(course, "UTF-8");
            result = getResult(connection, logs);
        }
        return result;
    } catch (Exception e) {
        return result;
    }
}

@Override
protected void onPostExecute(String result) {//this is going to be the next function to be done after the doInBackground function
    // TODO Auto-generated method stub

    if (pDialog.isShowing()) {
        pDialog.dismiss();
    }
    if (result.equalsIgnoreCase(""))//if there's nothing to return, the text "No records" are going to be thrown
    {

    } else //Array adapter is needed, to be a place holder of values before passing to spinner
    {

    }
}

}