0

I usually work on .Net but I need to develop an Android app. So I am new on Android, sorry for mistakes in advance! :)

Here is my story, I am populating a customer list(creating ui elements in code behind) with views in a button click. I am doing that pulling datas from database. So it takes some time to pull the data and creating views. What I wanna do is showing a progress dialog while customer list is being populated. Currently I am able to make it run. But the problem is it doesn't show the progress dialog immediately, then customerlist and progress dialog are displayed at the same time.

Here is my button click;

public void ShowCustomers(View view){
        final ProgressDialog dialog = new ProgressDialog(MainActivity.this);
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                dialog.setTitle("Title");
                dialog.setMessage("Loading...");
                if(!dialog.isShowing()){
                    dialog.show();
                }
            }
        });
        new Thread() {
            public void run() {
                try{
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            PopulateRecentGuests(); //Creates customer list dynamically
                            dialog.dismiss();
                        }
                    });
                } catch (Exception e) {
                    Log.e("tag", e.getMessage());
                }
            }
        }.start();
    }

And populating the customers;

public void PopulateRecentGuests(){
        LinearLayout customers = (LinearLayout)findViewById(R.id.customers);
        String query = "SELECT * from Table";
        ModelCustomer customerModel = new ModelCustomer();
        ArrayList<HashMap<String, String>> recentGuests = customerModel.RetrievingQuery(query);
        customersCount = recentGuests.size();
        Context context = getApplicationContext();
        if(customersCount < 1)
            Toast.makeText(context, "There is no available customer in database!", Toast.LENGTH_LONG);
        else if(!recentGuests.get(0).containsKey("err")) {
            for(int i = 0; i < recentGuests.size(); i++){
                HashMap<String, String> guest = recentGuests.get(i);
                Button button = new Button(context);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                params.setMargins(0,5,0,5);
                button.setLayoutParams(params);
                button.setWidth(800);
                button.setHeight(93);
                button.setTag(guest.get("id"));
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        System.out.println("New button clicked! ID: " + v.getTag());
                        Intent intent = new Intent(getApplicationContext(), ProductPageActivity.class);
                        intent.putExtra("CustomerID", v.getTag().toString());
                        startActivity(intent);
                    }
                });
                button.setBackgroundColor(Color.parseColor("#EFEFEF"));
                button.setText(guest.get("FirstName") + " " + guest.get("LastName") + "             " + guest.get("GuideName"));
                button.setTextColor(Color.BLACK);
                button.setTextSize(20);
                button.setEnabled(false);
                customers.addView(button); // customers is a linear layout and button is being added to customers
                Button guest_list_btn = (Button)findViewById(R.id.guest_list_btn);
                guest_list_btn.setEnabled(true);
            }
        }
        else{
            CharSequence text = recentGuests.get(0).get("err");
            int duration = Toast.LENGTH_LONG;
            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
        }

        Button guest_list_btn = (Button)findViewById(R.id.guest_list_btn);
        Button guest_list_close_btn = (Button)findViewById(R.id.guest_list_close_btn);

        customers.setVisibility(View.VISIBLE);
        AnimationSet aset = new AnimationSet(true);
        aset.setFillEnabled(true);
        aset.setInterpolator(new LinearInterpolator());

        AlphaAnimation alpha = new AlphaAnimation(0.0F, 1.0F);
        alpha.setDuration(400);
        aset.addAnimation(alpha);

        TranslateAnimation trans = new TranslateAnimation(200, 0, 0, 0);
        trans.setDuration(400);
        aset.addAnimation(trans);
        customers.startAnimation(aset);
        guest_list_btn.setEnabled(false);
        guest_list_close_btn.setEnabled(true);
        for(int i = 0; i < customers.getChildCount(); i++){
            View child = customers.getChildAt(i);
            child.setEnabled(true);
        }

    }

After my research, I understand that runonuithread is called after looper. My question is How can I display the progress dialog immediately and then I can populate the customer list (creating ui elements). By the way, I tried to do that with asynctask first but I wasn't able to.

Thans in advance!

sekercie
  • 271
  • 4
  • 12

1 Answers1

1

But the problem is it doesn't show the progress dialog immediately, then customerlist and progress dialog are displayed at the same time.

That's because you do the long operation(and closing the dialog sequentially) on the main UI thread. Instead you should separate things between retrieving the data(which takes time) and building the views(along with closing the dialog).

//...
new Thread() {
            public void run() {
                // do the long operation on this thread
                final ArrayList<HashMap<String, String>> recentGuests = customerModel.RetrievingQuery(query);
                // after retrieving the data then use it to build the views and close the dialog on the main UI thread
                try{
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            // remove the retrieving of data from this method and let it just build the views
                            PopulateRecentGuests(recentGuests); 
                            dialog.dismiss();
                        }
                    });
                } catch (Exception e) {
                    Log.e("tag", e.getMessage());
                }
            }
        }.start();
user
  • 86,916
  • 18
  • 197
  • 190