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

发布于 2024-12-02 15:58:39 字数 3116 浏览 0 评论 0原文

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


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


        }




    }

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


        }




    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

给不了的爱 2024-12-09 15:58:39

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

    @Override
    protected void onPreExecute() {
        progressBar = (ProgressBar)findViewById(R.id.progressbar);
        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);
    }
白色秋天 2024-12-09 15:58:39

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

---来自上面的代码

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

        }

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

        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文