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

}

This my source code i want to use Async task for parsing data but i m not able to write code for that this code is able to Print data on emulator but when i run it on devices then its unexpectedly closed i want to set parsing function in back ground to

4

1 回答 1

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}));
        }
    }
}
于 2016-03-02T11:34:46.253 回答