如何使用异步任务进行分页

时间:2012-10-08 10:14:07

标签: android android-asynctask android-parser

public class AndroidXMLParsingActivity extends Activity {

    public int currentPage = 1;
    public ListView lisView1;
    static final String KEY_ITEM = "docdetails";
    static final String KEY_NAME = "heading";
    public Button btnNext;
    public Button btnPre;
    public static String url = "http://dev.taxmann.com/TaxmannService/TaxmannService.asmx/GetNotificationList?YearID=&CircularNo=33&SearchText=tax&pagenumber=1";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // listView1
        lisView1 = (ListView) findViewById(R.id.listView1);

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

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



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

        int displayPerPage = 5; // Per Page
        int TotalRows = nl.getLength();
        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
        }
        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);
            // adding each child node to HashMap key => value
            map = new HashMap<String, String>();
            map.put("RowID", String.valueOf(RowID));
            map.put(KEY_NAME, parser.getValue(e, KEY_NAME));

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

            RowID = RowID + 1;

        }

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

}

这个我的源代码我想使用Async任务来解析数据,但我不能编写代码,因为这段代码能够在模拟器上打印数据,但是当我在设备上运行它然后意外关闭我想设置解析在背景中起作用

1 个答案:

答案 0 :(得分:0)

调整原始代码以使用AsyncTask非常简单。以下工作

public class AndroidXMLParsingActivity extends Activity {

    public int currentPage = 1;
    ...

    @Override
    public void onCreate(Bundle savedInstanceState) {
        ...
        btnNext.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                currentPage = currentPage + 1;
                //we pass in the page number every time so it knows what fetch
                new AsyncPaginator().execute(currentPage);
            }
        });
        ...
        btnPre.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                currentPage = currentPage - 1;
                new AsyncPaginator().execute(currentPage);
            }
        });
        new AsyncPaginator().execute(currentPage);
    }

    private class AsyncPaginator extends AsyncTask<Integer, Void, ArrayList<HashMap<String, String>>> {

        int TotalPage = 0;

        @Override
        protected ArrayList<HashMap<String, String>> doInBackground(Integer... params) {
            //the busy work happens here on another thread
            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);
            int displayPerPage = 5; // Per Page
            int TotalRows = nl.getLength();
            int indexRowStart = ((displayPerPage * currentPage) - displayPerPage);
            if (TotalRows <= displayPerPage) {
                TotalPage = 1;
            } else if ((TotalRows % displayPerPage) == 0) {
                TotalPage = (TotalRows / displayPerPage);
            } else {
                TotalPage = (TotalRows / displayPerPage) + 1; // 7
            }
            int indexRowEnd = displayPerPage * currentPage; // 5
            if (indexRowEnd > TotalRows) {
                indexRowEnd = TotalRows;
            }
            int RowID = 1;
            ArrayList<HashMap<String, String>> menuItems = new ArrayList<>();
            HashMap<String, String> map;
            if (currentPage > 1) {
                RowID = (displayPerPage * (currentPage - 1)) + 1;
            }
            for (int i = indexRowStart; i < indexRowEnd; i++) {
                Element e = (Element) nl.item(i);
                map = new HashMap<>();
                map.put("RowID", String.valueOf(RowID));
                map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
                menuItems.add(map);
                RowID = RowID + 1;
            }
            return menuItems;
        }

        @Override
        protected void onPostExecute(ArrayList<HashMap<String, String>> menuItems) {
            //when everything finishes we display the updates from here
            super.onPostExecute(menuItems);
            if (currentPage >= TotalPage) {
                btnNext.setEnabled(false);
            } else {
                btnNext.setEnabled(true);
            }
            if (currentPage <= 1) {
                btnPre.setEnabled(false);
            } else {
                btnPre.setEnabled(true);
            }
            lisView1.setAdapter(new SimpleAdapter(AndroidXMLParsingActivity.this, menuItems,
                R.layout.list_item, new String[]{"RowID", KEY_NAME},
                new int[]{R.id.ColRowID, R.id.ColName}));
        }
    }
}