如何在onPreExecute和onProgressUpdate中显示进度条?

时间:2011-09-01 00:28:38

标签: android progress-bar android-asynctask

如果我有以下代码...感谢您的帮助


loading.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:gravity="center_horizontal" 
    android:minHeight="?android:attr/listPreferredItemHeight">

    <ProgressBar 
        android:id="@+id/progressbar"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerVertical="true" 
        />

    <TextView 
        android:text="Loading Events…" 
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"
        android:layout_toRightOf="@+id/progressbar" 
        android:layout_centerVertical="true" 
        android:gravity="center"
        android:padding="10dip"
        android:textColor="#FFFFFF" />
</RelativeLayout>

listplaceholder.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">    

    <ListView
        android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:drawSelectorOnTop="false"
        />

</LinearLayout>

EventsActivity.class

public class EventsActivity extends ListActivity {


    syncEvent lastsyncEvent = null;
    EventDataSet sitesList = null;
    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listplaceholder);
        //refreshEvents call new syncEvent().execute();
        refreshEvents();

private class syncEvent extends AsyncTask<String, Integer, String>{

        private ProgressBar progressBar;

        @Override
        protected String doInBackground(String... arg0) {
            .....
            return null;
        }

        @Override
        protected void onPostExecute(String result) {

            ListAdapter adapter = new SimpleAdapter(EventsActivity.this, mylist , R.layout.eventitem, 
                    new String[] { "name", "createat" }, 
                    new int[] { R.id.item_title, R.id.item_subtitle });

                    setListAdapter(adapter);

        }



        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);

        }

        @Override
        protected void onPreExecute() {

            progressBar = new ProgressBar(EventsActivity.this, null, R.layout.loading );
            progressBar.setVisibility(View.VISIBLE);


        }




    }

2 个答案:

答案 0 :(得分:3)

如果我理解正确,您希望显示您在布局中定义的ProgressBar。我认为唯一的问题是你错误地引用了进度条。尝试:

    @Override
    protected void onPreExecute() {
        progressBar = (ProgressBar)findViewById(R.id.progressbar);
        progressBar.setVisibility(View.VISIBLE);
    }

答案 1 :(得分:0)

顺便说一句,我找到了一些解决方案......它可能不是一个干净的代码......但是,我希望它对某些用途有用

---来自上面的代码

我删除loading.xml并将一些代码添加到listplaceholder.xml而不是

listplaceholder.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">    

   <RelativeLayout
   android:id="@+id/loadingevent" 
   android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal">  
    <ProgressBar 
        android:id="@+id/progressbar"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_centerVertical="true" />

    <TextView 
        android:id="@+id/loadingeventtext" 
        android:text="Loading Events…" 
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content"
        android:layout_toRightOf="@+id/progressbar" 
        android:layout_centerVertical="true" 
        android:gravity="center"
        android:padding="10dip"
        android:textColor="#FFFFFF"/>

        </RelativeLayout>
    <ListView
        android:id="@id/android:list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:drawSelectorOnTop="false"/>

</LinearLayout>

* ---添加几行到AsyncTask *

    private class syncEvent extends AsyncTask<String, Integer, String>{

        //Add 1. Declare RelativeLaout
        private RelativeLayout relativeView;


        @Override
        protected String doInBackground(String... arg0) {
               //your operation task here
               ..........
               ........
               return null;
        }



        @Override
        protected void onPostExecute(String result) {

            ListAdapter adapter = new SimpleAdapter(EventsActivity.this, mylist , R.layout.eventitem, 
                    new String[] { "name", "createat" }, 
                    new int[] { R.id.item_title, R.id.item_subtitle });

                    setListAdapter(adapter);

                    //Add 2. To setVisibility GONE when finish loading
                    relativeView.setVisibility(View.GONE);
        }

        @Override
        protected void onPreExecute() {

            //Add 3. add 2 lines of code here
            relativeView = (RelativeLayout)findViewById(R.id.loadingevent);
            relativeView.setVisibility(View.VISIBLE);

        }

        // For this method, i don't need to use it yet,so, i left it here
        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);

        }