findInBackground中的进度对话框,用于解析数据库而不使用Thread或AsynchTaks

时间:2016-07-11 06:44:29

标签: java android mobile-application

任何人都可以指导我如何在从解析数据库中获取数据时显示进度对话框。谢谢

    public void fetch() {

    ParseQuery<ParseObject> query = ParseQuery.getQuery("Phrase");
    try {
        query.setLimit(1000);

        progress_Dialog.setTitle("Parse Library Download");

        progress_Dialog.setMessage("Downloading Please wait....");

        progress_Dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);

        progress_Dialog.show();

        query.findInBackground(new FindCallback<ParseObject>() {
                                   @Override
                                   public void done(List<ParseObject> greeting_list, ParseException e) {

                                       parse_count = greeting_list.size();

                                       if (e == null) {
                                           for (ParseObject obj : greeting_list) {


                                               image_file = obj.getParseFile("image");

                                               try {
                                                   bmp = BitmapFactory.decodeByteArray(image_file.getData(), 0, image_file.getData().length);

                                                   bmp = getResizedBitmap(bmp, 500, 500);

                                                   ByteArrayOutputStream stream = new ByteArrayOutputStream();

                                                   bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);

                                                   image_array = stream.toByteArray();
                                               } catch (ParseException e1) {
                                                   e1.printStackTrace();
                                               }

                                               audioFile = obj.getParseFile("audio");

                                               try {
                                                   counter = dBhelper.insert_TextData(obj.getString("english"), obj.getString("italian"), image_array, audioFile.getData(), obj.getString("category"));

                                                   System.out.println(counter);

                                                   progress_Dialog.setProgress(progressStatus);

                                                   progressStatus++;


                                               } catch (ParseException e1) {
                                                   e1.printStackTrace();
                                               }
                                           }
                                           progress_Dialog.dismiss();

                                           System.out.println("ALL DATA FETCHED");

                                       } else {

                                           Log.d("Error", "" + e.getMessage());
                                           e.printStackTrace();
                                       }
                                   }
                               }
        );
    } catch (Exception e) {
        e.printStackTrace();
    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用ParseQuery检索此数据。使用Queries

阅读Parse的Android指南

使用进度对话框使用asyntask检查以下示例:

private class RemoteDataTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // Create a progressdialog
        mProgressDialog = new ProgressDialog(MainActivity.this);
        // Set progressdialog title
        mProgressDialog.setTitle("Parse.com Custom ListView Tutorial");
        // Set progressdialog message
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        // Show progressdialog
        mProgressDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        // Create the array
        worldpopulationlist = new ArrayList<WorldPopulation>();
        try {
            // Locate the class table named "Country" in Parse.com
            ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(
                    "Country");
            // Locate the column named "ranknum" in Parse.com and order list
            // by ascending
            query.orderByAscending("ranknum");
            ob = query.find();
            for (ParseObject country : ob) {
                // Locate images in flag column
                ParseFile image = (ParseFile) country.get("flag");

                WorldPopulation map = new WorldPopulation();
                map.setRank((String) country.get("rank"));
                map.setCountry((String) country.get("country"));
                map.setPopulation((String) country.get("population"));
                map.setFlag(image.getUrl());
                worldpopulationlist.add(map);
            }
        } catch (ParseException e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // Locate the listview in listview_main.xml
        listview = (ListView) findViewById(R.id.listview);
        // Pass the results into ListViewAdapter.java
        adapter = new ListViewAdapter(MainActivity.this,
                worldpopulationlist);
        // Binds the Adapter to the ListView
        listview.setAdapter(adapter);
        // Close the progressdialog
        mProgressDialog.dismiss();
    }
}