【问题标题】:How to show Progress bar in onPreExecute and onProgressUpdate?如何在 onPreExecute 和 onProgressUpdate 中显示进度条?
【发布时间】:2011-09-01 00:28:37
【问题描述】:

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


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


        }




    }

【问题讨论】:

    标签: android progress-bar android-asynctask


    【解决方案1】:

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

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

    【讨论】:

    • 是的,你说得对。我已经找到并修复了它。顺便说一句,谢谢你的帮助
    【解决方案2】:

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

    ---来自上面的代码

    我删除了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);
    
            }
    

    【讨论】:

      猜你喜欢
      • 2014-10-23
      • 2014-06-14
      • 2013-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多