进度对话框未显示在异步任务中

时间:2019-02-15 05:09:50

标签: android sqlite android-recyclerview progressdialog

我的进度对话框未显示在异步任务中,如果我注释掉 progressDialog.dismiss(); ,则将显示进度对话框。我正在从服务器中检索响应,并且在后台将数据插入sqlite数据库中。插入数据后,我将从数据库中检索数据并显示在recyclerview中。

SubCategoryDe​​tailActivity.java

public class SubCategoryDetailActivity extends AppCompatActivity {

        RecyclerView recyclerViewSubCategoryDetail;
        ArrayList<SubCategoryDetailModelClass> subCategoryListDetailModelClassArray;
        SubCategoryDetailAdapter subCategoryDetailAdapter;
        ProductListDBHelper productListDBHelper;
        String subCategoryID;
        ProgressDialog progressDialog;
        ProgressBar progressBar;
        TextView view_cart,view_report;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_sub_category_detail);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);

            productListDBHelper= new ProductListDBHelper(this);

            view_cart=findViewById(R.id.txtv_view_cart);
            view_report=findViewById(R.id.txtv_view_report);
            recyclerViewSubCategoryDetail =findViewById(R.id.recyclerview_subcategory_detail);
            subCategoryListDetailModelClassArray =new ArrayList<>();

            Bundle mBundle = getIntent().getExtras();
            if (mBundle != null) {

                subCategoryID = String.valueOf(mBundle.getInt("SUBCATEGORY_ID"));
                Log.d("alliqwwww","" +subCategoryID);
            }


           /* subCategoryListDetailModelClassArray=productListDBHelper.getSubCategoryProductDetail(subCategoryID);

            recyclerViewSubCategoryDetail.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
            subCategoryDetailAdapter =new SubCategoryDetailAdapter(this, subCategoryListDetailModelClassArray);
            recyclerViewSubCategoryDetail.setAdapter(subCategoryDetailAdapter);*/

            SubCategoryDetailInsertionAsyncTask myAsyncTasks = new SubCategoryDetailInsertionAsyncTask();
            myAsyncTasks.execute();



        }


        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            onBackPressed();
            return super.onOptionsItemSelected(item);
        }


        private class SubCategoryDetailInsertionAsyncTask extends AsyncTask<StringRequest,Void,String> {
            int product_id,pcategory_id,subcategory_id;
            String product_name,product_desc,product_img,subcategory_name,subcategory_desc,subcategory_image,category_name;
            Integer insertedResult = -1;



            @Override
            protected void onPreExecute() {
                super.onPreExecute();

                progressDialog = new ProgressDialog(SubCategoryDetailActivity.this);
                progressDialog.setMessage("Please Wait");
                progressDialog.setCancelable(false);
                progressDialog.show();

                Log.d("progreeeesqwqw","ddd");


                progressBar.setVisibility(View.VISIBLE);*/
            }

            @Override
            protected String doInBackground(StringRequest... stringRequests) {



                StringRequest stringRequest = new StringRequest(Request.Method.GET, URLs.productURL,
                        new Response.Listener<String>() {
                            @Override
                            public void onResponse(String response) {
                                Log.d("zsded", response.toString());

                                try {

                                    JSONObject obj = new JSONObject(response);

                                    JSONArray productArray = obj.getJSONArray("products");


                                    for (int i = 0; i < productArray.length(); i++) {

                                        JSONObject productObject = productArray.getJSONObject(i);

                                        SubCategoryDetailModelClass subCategoryDetailModelClass = new SubCategoryDetailModelClass();

                                        subCategoryDetailModelClass.setProduct_name(productObject.getString("product-name"));
                                        subCategoryDetailModelClass.setProduct_id(productObject.getInt("product-id"));
                                        subCategoryDetailModelClass.setProduct_desc(productObject.getString("product-description"));
                                        subCategoryDetailModelClass.setProduct_imgURL(productObject.getString("product-image"));
                                        subCategoryDetailModelClass.setProduct_CategoryId(productObject.getInt("product-categoryid"));
                                        subCategoryDetailModelClass.setProduct_Category_Name(productObject.getString("category-name"));
                                        subCategoryDetailModelClass.setSubCategoryId(productObject.optInt("subcategory-id"));
                                        subCategoryDetailModelClass.setSubCategoryName(productObject.optString("subcategory-name"));
                                        subCategoryListDetailModelClassArray.add(subCategoryDetailModelClass);

                                        Log.d("subcatdetail", String.valueOf(subCategoryDetailModelClass));

                                    }

                                    for(int i = 0; i< subCategoryListDetailModelClassArray.size(); i++){
                                        product_name= subCategoryListDetailModelClassArray.get(i).getProduct_name();
                                        product_id= subCategoryListDetailModelClassArray.get(i).getProduct_id();
                                        product_desc= subCategoryListDetailModelClassArray.get(i).getProduct_desc();
                                        product_img= subCategoryListDetailModelClassArray.get(i).getProduct_imgURL();
                                        pcategory_id= subCategoryListDetailModelClassArray.get(i).getProduct_CategoryId();
                                        category_name= subCategoryListDetailModelClassArray.get(i).getProduct_Category_Name();
                                        subcategory_id= subCategoryListDetailModelClassArray.get(i).getSubCategoryId();
                                        subcategory_name= subCategoryListDetailModelClassArray.get(i).getSubCategoryName();
                                        Log.d("subcatdetaillllll", product_name);
                                        Log.d("subcatdetaillllll", String.valueOf(subcategory_id));
                                        Log.d("subcatdetaillllll",subcategory_name);

                                        ProductListDBHelper dbHelper = new ProductListDBHelper(getApplicationContext());
                                        SQLiteDatabase database = dbHelper.getWritableDatabase();
                                        dbHelper.insertSubCategoryProductDetail(product_name,product_id,product_desc,product_img,pcategory_id,category_name,subcategory_id,subcategory_name,database);
                                    }

                                    subCategoryListDetailModelClassArray = productListDBHelper.getSubCategoryProductDetail(subCategoryID);

                                    recyclerViewSubCategoryDetail.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
                                    subCategoryDetailAdapter = new SubCategoryDetailAdapter(getApplicationContext(), subCategoryListDetailModelClassArray);
                                    recyclerViewSubCategoryDetail.setAdapter(subCategoryDetailAdapter);
                                    subCategoryDetailAdapter.notifyDataSetChanged();


                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                error.printStackTrace();
                                //  Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show();

                            }
                        });

           MySingleton.getInstance(getApplicationContext()).addToRequestQueue(stringRequest);


                return  "Task Completed";
            }
            @Override
            protected void onPostExecute(String s) {


                 progressDialog.dismiss();
                Log.d("progreeeesqwqw","eee");

            }
        }



    }

2 个答案:

答案 0 :(得分:0)

您的代码存在问题,即截击请求是在后台线程上发出的。因此,doInBackground不会等到获得响应。

使用排球时不需要异步任务

showprogressbar进行请求,然后在onResponseonErrorResponse中将其关闭

答案 1 :(得分:0)

您正在0中使用if (!context.Product.Any()) { { context.Product.AddRange( new Product(id, "Samsung T3 Portable SSD - 500GB", "Superfast Read-Write Speeds of up to 450 MB/s; Shock Resistant & Secure Encryption", 5.50F, "Product1.jpg"), new Product(/* parameters */) ) } 请求。

第一个解决方案

删除async,并在async task中完成所有工作,并在齐响响应回调命中时关闭进度对话框。

第二个解决方案

只需使用Async Task删除批量请求即可。自行发送HTTP请求。那么这段代码就会起作用。

最佳解决方案是第一解决方案。

在您的onCreate方法中

删除这些行

activity

并粘贴

Async task