0

如何编辑代码以使用 AsyncTask。ListFragment 的代码使用。我尝试在 OnCreateView 应用程序上使用此代码,结果发现我使用 AsyncTask。用于从 url 解析 xml。我使用自定义列表视图。网址=

代码在这里

import java.util.ArrayList;
import java.util.HashMap;

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

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

public class AndroidXMLParsingActivity extends ListActivity {

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

        ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();

        final 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);
        // looping through all item nodes <item>
        for (int i = 0; i < nl.getLength(); i++) {
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            Element e = (Element) nl.item(i);
            // adding each child node to HashMap key => value

            map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
            map.put(KEY_COST, "Datum: " + parser.getValue(e, KEY_COST));
            map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
            map.put(KEY_LINK, parser.getValue(e, KEY_LINK));
            map.put(KEY_LINK1, parser.getValue(e, KEY_LINK1));

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

        }

        // Adding menuItems to ListView
        ListAdapter adapter = new SimpleAdapter(this, menuItems,
                R.layout.list_row,
                new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int[] {
                        R.id.naslov, R.id.novost, R.id.datum });

        setListAdapter(adapter);

        // selecting single ListView item
        ListView lv = getListView();

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // getting values from selected ListItem
                String name = ((TextView) view.findViewById(R.id.naslov)).getText().toString();
                String cost = ((TextView) view.findViewById(R.id.datum)).getText().toString();
                String description = ((TextView) view.findViewById(R.id.novost)).getText().toString();
                String link=KEY_LINK;
                // Starting new intent
                Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
                in.putExtra(KEY_NAME, name);
                in.putExtra(KEY_COST, cost);
                in.putExtra(KEY_DESC, description);
                in.putExtra(KEY_LINK, link);
                in.putExtra("link1", KEY_LINK1);
                startActivity(in);

            }
        });
    }
}
4

0 回答 0