如何设置进度条

时间:2012-10-12 10:53:13

标签: android

package com.example.tabactivity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.net.ParseException;
import android.os.AsyncTask;
import android.os.Bundle;

import android.app.ActivityGroup;
import android.app.ProgressDialog;
import android.content.Intent;

import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class circularlistparsing extends ActivityGroup {

    public int currentPage = 1;
    public ListView lisView1;
    static final String KEY_ITEM = "docdetails";
    static final String KEY_ITEM2 = "info";
    static final String KEY_NAME1 = "";
    static final String KEY_NAME = "heading";
    static final String KEY_DATE = "date";
    public Button btnNext;
    public Button btnPre;
    public static String url = "http://dev.taxmann.com/TaxmannService/TaxmannService.asmx/GetCircularList";
    TextView txtreord;
    TextView totalpage;
    TextView pagenumber;
    ProgressDialog dialog;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        txtreord = (TextView) findViewById(R.id.recored);
        totalpage = (TextView) findViewById(R.id.totalpage);
        pagenumber = (TextView) findViewById(R.id.pagenumber);

        // listView1
        lisView1 = (ListView) findViewById(R.id.listView1);
    //  new YourTask().execute();
        // Next
        btnNext = (Button) findViewById(R.id.btnNext);
        // Perform action on click
        btnNext.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                currentPage = currentPage + 1;

    //          new YourTask().execute();
                ShowData();
                pagenumber.setText("Of" + currentPage+"]");
            }
        });

        // Previous
        btnPre = (Button) findViewById(R.id.btnPre);
        // Perform action on click
        btnPre.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                currentPage = currentPage - 1;

        //      new YourTask().execute();
                ShowData();
                pagenumber.setText("Of" + currentPage+"]");
            }
        });

    //  new YourTask().execute();
        ShowData();
    }

    public void ShowData() {
        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(url); // getting XML

        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_ITEM);

        NodeList n2 = doc.getElementsByTagName(KEY_ITEM2);

        int displayPerPage = 10; // Per Page
        int TotalRows = nl.getLength();

        txtreord.setText( TotalRows+"Records|"); // number of records

        int indexRowStart = ((displayPerPage * currentPage) - displayPerPage);
        int TotalPage = 0;
        if (TotalRows <= displayPerPage) {
            TotalPage = 1;
        } else if ((TotalRows % displayPerPage) == 0) {
            TotalPage = (TotalRows / displayPerPage);
        } else {
            TotalPage = (TotalRows / displayPerPage) + 1; // 7
            TotalPage = (int) TotalPage; // 7
        }

        totalpage.setText("Page[" + TotalPage);

        int indexRowEnd = displayPerPage * currentPage; // 5
        if (indexRowEnd > TotalRows) {
            indexRowEnd = TotalRows;
        }

        // Disabled Button Next
        if (currentPage >= TotalPage) {
            btnNext.setEnabled(false);
        } else {
            btnNext.setEnabled(true);
        }

        // Disabled Button Previos
        if (currentPage <= 1) {
            btnPre.setEnabled(false);
        } else {
            btnPre.setEnabled(true);
        }

        // Load Data from Index
        int RowID = 1;
        ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map;

        // RowID
        if (currentPage > 1) {
            RowID = (displayPerPage * (currentPage - 1)) + 1;
        }

        for (int i = indexRowStart; i < indexRowEnd; i++) {
            Element e = (Element) nl.item(i);

            Element e2 = (Element) n2.item(i);

            String date = e2.getAttribute(KEY_DATE);
            // adding each child node to HashMap key => value

            map = new HashMap<String, String>();
            map.put("RowID", String.valueOf(RowID));

            map.put(KEY_DATE, date);

            // String Heading = parser.getValue(e, KEY_NAME).replace("|", "|\n")
            // .replace("|", "");
            //

            String mytime = date;
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmdd");
            Date myDate = null;
            try {
                myDate = dateFormat.parse(mytime);

            } catch (ParseException t) {
                t.printStackTrace();
            } catch (java.text.ParseException t) {
                // TODO Auto-generated catch block
                t.printStackTrace();
            }

            SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd");
            String finalDate = timeFormat.format(myDate);

            // System.out.println("rrrrrrrrrrrrr"+finalDate);

            String Heading = parser.getValue(e, KEY_NAME);

            int a = Heading.indexOf("|");
            String beforeSubString = Heading.substring(0, a);
            String afterSubString = Heading.substring(a, Heading.length())
                    .replace("|", "") + "[" + finalDate + "]";
            // String
            // final1="<b>"+beforeSubString+"<b>"+"|"+afterSubString.replace("|",
            // "|\n")
            // .replace("|", "");
            // String k=Html.fromHtml(final1).toString();
            //
            // Html.fromHtml(final1);

            map.put(KEY_NAME, beforeSubString);
            map.put(KEY_NAME1, afterSubString);

            // adding HashList to ArrayList
            menuItems.add(map);

            RowID = RowID + 1;

        }

        SimpleAdapter sAdap;
        sAdap = new SimpleAdapter(circularlistparsing.this, menuItems,
                R.layout.list_item,
                new String[] { "RowID", KEY_NAME1, KEY_NAME }, new int[] {
                        R.id.ColRowID, R.id.ColName, R.id.textView1 });
        lisView1.setAdapter(sAdap);

        lisView1.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub

                Intent i = new Intent(circularlistparsing.this, detail.class);
                // sending data to new activity
                // i.putExtra("product", product);
                startActivity(i);

            }
        });




        }





    class YourTask extends AsyncTask<Void, Void, Void> {
        protected void onPreExecute() {
                   dialog = ProgressDialog.show(circularlistparsing.this, "", "Please wait..");
        }

        protected Void doInBackground(Void... unused) {
            try {
               // doSomethingHeavy();
                   // publishProgress(...);

                ShowData();
            } catch(Exception e) {
                //...
            } 

            return null;
        }

        protected void onProgressUpdate(Void... unused) {
        }

        protected void onPostExecute(Void unused) {
            dialog.dismiss();

        }
    }




}
请将此代码显示数据函数用于在使用新的YourTask()时显示数据.execute();代替显示数据然后它显示进度条但不显示数据但能够读取数据,而当我们用户showdata()函数时它显示数据请告诉我如何实现进度条。

2 个答案:

答案 0 :(得分:0)

实现此代码

private class YourTask extends AsyncTask<Object, Integer, Object> {

    @Override
    protected void onPreExecute() {

         ProgressDialog dialog = ProgressDialog.show(Activity.this, "",
                "Loading...");

        super.onPreExecute();
    }

    @Override
    protected Object doInBackground(Object... params) {
       try {
       // doSomethingHeavy();
           // publishProgress(...);

        ShowData();
    } catch(Exception e) {
        //...
    } 

    return null;
}

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

    }

    @Override
    protected void onPostExecute(Object result) {

        dialog .dismiss();


        super.onPostExecute(result);
    }
} 

答案 1 :(得分:0)

private class YourTask extends AsyncTask<Object, Integer, Object> {

ProgressDialog dialog = new ProgressDialog(Currentactivity.this);
    @Override
    protected void onPreExecute() {
        this.dialog.setMessage("Loading...");
        this.dialog.show();
        this.dialog.setCancelable(false);

        super.onPreExecute();
    }

    @Override
    protected Object doInBackground(Object... params) {
        //do hard work here
                            return params;
    }

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

    }

    @Override
    protected void onPostExecute(Object result) {

        progressBar.dismiss();


        super.onPostExecute(result);
    }
}