0

我通常在.Net上工作,但我需要开发一个Android应用程序。所以我是Android新手,提前对错误抱歉! :)Android runOnUiThread显示进度对话框并填充视图

这是我的故事, 我正在填充客户列表(在代码后面创建ui元素),并在按钮单击中显示视图。我正在从数据库中提取数据。所以需要一些时间来拉取数据并创建视图。我想要做的是在客户列表正在填充时显示进度对话框。目前我能够使其运行。但问题是它不会立即显示进度对话框,则会同时显示客户列表和进度对话框。

这是我点击按钮;

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(); 
    } 

并填充客户;

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); 
     } 

    } 

经过我的研究,我明白runonuithread是在looper之后调用的。我的问题是我怎样才能立即显示进度对话框,然后我可以填充客户列表(创建UI元素)。顺便说一句,我试图用asynctask做到这一点,但我无法做到。

Thans提前!

回答

1

但问题是它不立即显示进度对话框, 然后客户列表和进度对话框同时显示。

这是因为您在主UI线程上执行了长操作(并依次关闭对话框)。相反,你应该在检索数据(需要时间)和构建视图(以及关闭对话框)之间分开。

//... 
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(); 
+0

工程就像一个魅力!我花了一天的时间!非常感谢!!! @Lugsprog – sekercie 2015-02-09 19:29:34