Stop ProgressBar



  • There's an app that downloads the data from the server base at the first launch. Use it for loading Volley♪ I added ProgressBar so the user wouldn't look into an empty window.

    ProgressBar is barely moving until Volley requests are met. It's just moving away from the original position and everything. As I understand, Volley asynchronen.

    What's the problem?

    UPD. Adding the code.

    public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    

    public LinearLayout cartSticker;

    DbHelper dbHelper;
    Cursor c;

    ListView itemsList;
    GridView itemsGrid;
    ListView menu;

    DrawerLayout drawer;
    Toolbar toolbar;
    ActionBarDrawerToggle toggle;

    public Core core;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    setProgressBarIndeterminateVisibility(true);

    super.onCreate(savedInstanceState);
    LayoutInflater lInflater = getLayoutInflater();
    
    dbHelper = new DbHelper(this);
    
    core = Core.getInstance();
    core.setContext(this);
    core.setHelper(dbHelper);
    
    setContentView(R.layout.activity_main);
    toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    
    cartSticker = (LinearLayout) findViewById(R.id.cart_items_sticker_wrapper);
    
    drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    toggle = new android.support.v7.app.ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    
    drawer.setDrawerListener(toggle);
    toggle.syncState();
    
    menu = (ListView) findViewById(R.id.cats_list);
    menu.addHeaderView(lInflater.inflate(R.layout.cat_list_header, null), null, true);
    menu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            VHolder holder = (VHolder) view.getTag();
            if (holder == null) {
                showMainPage();
                drawer.closeDrawers();
            } else {
                int catId = holder.id;
                openCat(catId);
            }
        }
    });
    
    updateCartSticker(dbHelper.getCartItemsCount());
    
    
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            drawer.openDrawer(Gravity.LEFT);
        }
    });
    drawer.closeDrawers();
    pB = new ProgressBar(getApplicationContext());
    

    }

    ProgressBar pB;
    public void showLoader() {
    AbsListView.LayoutParams lP = new AbsListView.LayoutParams(70, 70);
    ((LinearLayout) findViewById(R.id.loader_wrapper)).addView(pB, lP);
    }
    public void hideLoader() {
    ((LinearLayout) findViewById(R.id.loader_wrapper)).removeAllViews();
    }

    @Override
    protected void onStart() {
    super.onStart();
    new DataLoader().execute("");
    }

    private void buildMenu() {
    String[] from = {"name"};
    int[] to = {R.id.name};
    c = dbHelper.getCat();

    MenuCursorAdapter mca = new MenuCursorAdapter(this, c, 1);
    menu.setAdapter(mca);
    

    }

    public void showMainPage() {
    Fragment fr = new MainFragment();

    FragmentManager fM = getFragmentManager();
    
    FragmentTransaction fTr = fM.beginTransaction();
    fTr.replace(R.id.main_content, fr);
    fTr.commit();
    

    }

    public void buildProducts(int catId, int type) {

    Fragment fragment = new ListFragment();
    Bundle args = new Bundle();
    args.putInt("catId", catId);
    args.putInt("type", type);
    fragment.setArguments(args);
    
    FragmentManager fragmentManager = getFragmentManager();
    
    FragmentTransaction fTr = fragmentManager.beginTransaction();
    fTr.replace(R.id.main_content, fragment);
    fTr.addToBackStack(null);
    fTr.commit();
    

    }

    public void openCat(int id) {
    Category cat = dbHelper.getCategory(id);
    toolbar.setTitle(cat.name);
    buildProducts(id, 0);
    drawer.closeDrawers();
    }

    public void openCart() {
    {

        Fragment fragment = new CartFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        FragmentManager fragmentManager = getFragmentManager();
    
        FragmentTransaction fTr = fragmentManager.beginTransaction();
        fTr.replace(R.id.main_content, fragment);
        fTr.addToBackStack(null);
        fTr.commit();
    
        toolbar.setTitle(getResources().getText(R.string.app_cart));
    }
    

    }

    public void openProduct(int id) {
    Fragment fragment = new ProductFragment();
    Bundle args = new Bundle();
    args.putInt("productId", id);
    fragment.setArguments(args);
    FragmentManager fragmentManager = getFragmentManager();

    FragmentTransaction fTr = fragmentManager.beginTransaction();
    fTr.replace(R.id.main_content, fragment);
    fTr.addToBackStack(null);
    fTr.commit();
    

    }

    @Override
    public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
    drawer.closeDrawer(GravityCompat.START);
    } else {
    if (getFragmentManager().getBackStackEntryCount() > 0) {
    getFragmentManager().popBackStack();
    } else {
    super.onBackPressed();
    }
    }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    if (toggle.onOptionsItemSelected(item)) {
    return true;
    }

    int id = item.getItemId();
    
    if (id == R.id.action_cart) {
        openCart();
        return true;
    }
    
    return super.onOptionsItemSelected(item);
    

    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {
        // Handle the camera action
    } else if (id == R.id.nav_gallery) {
    
    } else if (id == R.id.nav_slideshow) {
    
    } else if (id == R.id.nav_manage) {
    
    } else if (id == R.id.nav_share) {
    
    } else if (id == R.id.nav_send) {
    
    }
    
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
    

    }

    public void loadFromServer() {
    showLoader();
    RequestQueue rQ = Volley.newRequestQueue(this);

    // Request a string response from the provided URL.
    StringRequest catsRequest = new StringRequest(Request.Method.GET, Core.catsUrl,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    dbHelper.updateCategories(response);
                    buildMenu();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
    
        }
    });
    StringRequest prodsRequest = new StringRequest(Request.Method.GET, Core.prodsUrl,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    dbHelper.updateProducts(response);
                    buildMenu();
                    showMainPage();
    
    
                    SharedPreferences sp = getSharedPreferences("gorillagym_update_data_flag", Context.MODE_PRIVATE);
                    SharedPreferences.Editor spe = sp.edit();
    
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
    
        }
    });
    
    rQ.add(catsRequest);
    rQ.add(prodsRequest);
    

    }

    public void updateFromServer() {

    String date = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss").format(new Date());
    SharedPreferences sp = getSharedPreferences("gorillagym_update_data_flag", Context.MODE_PRIVATE);
    String lastUpdateDate = sp.getString("last_update_date", date);
    
    
    RequestQueue rQ = Volley.newRequestQueue(this);
    StringRequest updateRequest = new StringRequest(Request.Method.GET, Core.updateUrl + "&date=" + lastUpdateDate,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
    
                        JSONObject jObject = new JSONObject(response);
                        boolean status = jObject.getBoolean("status");
                        String lastTime = jObject.getString("lastTime");
                        if (status) {
                            dbHelper.updateData(response);
    
                            SharedPreferences sp = getSharedPreferences("gorillagym_update_data_flag", Context.MODE_PRIVATE);
                            SharedPreferences.Editor spe = sp.edit();
    
                            spe.putString("last_update_date", lastTime);
                            spe.apply();
                        }
                    } catch (Exception e) {
    
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
    
        }
    });
    rQ.add(updateRequest);
    

    }

    public void updateData() {
    SharedPreferences sp = getSharedPreferences("gorillagym_update_data_flag", Context.MODE_PRIVATE);

    if (sp.getBoolean("first_install", true)) {
         loadFromServer();
    } else {
        showMainPage();
        updateFromServer();
    }
    

    }

    public void updateCartSticker(int count) {
    if (count <= 0) {
    cartSticker.setVisibility(View.GONE);
    ((TextView) cartSticker.findViewById(R.id.cart_items_sticker)).setText("");
    } else {
    cartSticker.setVisibility(View.VISIBLE);
    ((TextView) cartSticker.findViewById(R.id.cart_items_sticker)).setText(String.valueOf(count));
    }
    }

    public class DataLoader extends AsyncTask<String, Void, Bitmap> {
    public DataLoader () {

    }
    
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    
    }
    
    protected Bitmap doInBackground(String... urls) {
        updateData();
        return null;
    }
    
    protected void onPostExecute(Bitmap result) {
    }
    

    }



  • I can assume your Volley is not sufficiently synchronized.

    Try the test. AsyncTaskTo use his bad idea-- problems with turn.

    In it doInBackground You're doing the surgery you need and through. publishProgress()Updated status progressBar

    If everything goes smoothly, then you've had everything in the main flow, and you need to think about asynchronity yourself.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2