开发者

How to show Progress bar in onPreExecute and onProgressUpdate?

开发者 https://www.devze.com 2023-04-01 13:08 出处:网络
If i have the following code.. thanks you for your help loading.xml <?xml version=\"1.0\" encoding=\"utf-8\"?>

If i have the following code.. thanks you for your help


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<Ha开发者_JS百科shMap<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);


        }




    }


If i understand correctly, you want to show the ProgressBar you have defined in your layout. I think the only problem is that you are referencing the progress bar incorrectly. Try:

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


By the way, i find some solutions... it might not be a clean code..however, i hope it useful for some purpose

---From the above code

I delete loading.xml and add some code into listplaceholder.xml instead

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>

*---Add a couple lines to 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);

        }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号