如何使解析的列表项打开新类

时间:2014-03-14 01:23:48

标签: android list parsing

我有一个自定义列表适配器,它从在线托管的xml文件中读取数据,每个文件都用于另一篇文章。我的问题是如何让每个列表项打开一个不同的相应文章?这些文章将在线托管。

提前谢谢。

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import java.util.ArrayList;
import java.util.HashMap;


public class CustomizedListView extends Fragment {  // All static variables
    // XML node keys
    static final String KEY_SONG = "song"; // parent node
    static final String KEY_ID = "id";
    static final String KEY_ARTIST = "artist";
    static final String KEY_DURATION = "duration";
    static final String KEY_THUMB_URL = "thumb_url";

    ListView list;
    LazyAdapter adapter;
    ArrayList<HashMap<String, String>> songsList;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View rootView = inflater.inflate(R.layout.news, container, false);



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


    list=(ListView)  rootView.findViewById(R.id.list);

        new RetrieveXML().execute(URL);

        // Getting adapter by passing xml data ArrayList


        // Click event for single list row                XMLParser parser = new XMLParser();

        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {


            }
        });
        return rootView;
    }

    class RetrieveXML extends AsyncTask<String, Void, String> {

        private Exception exception;
        XMLParser parser = new XMLParser();


        protected String doInBackground(String... urls) {
            try {

                return parser.getXmlFromUrl(urls[0]);
            } catch (Exception e) {
                this.exception = e;
                return null;
            }
        }

        protected void onPostExecute(String xml) {
            Document doc = parser.getDomElement(xml); // getting DOM element

            NodeList nl = doc.getElementsByTagName(KEY_SONG);
            // looping through all song nodes <song>
            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_ID, parser.getValue(e, KEY_ID));
                map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
                map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
                map.put(KEY_THUMB_URL, parser.getValue(e, KEY_ID));
                map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));

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

            }

            adapter=new LazyAdapter(getActivity(), songsList);

            list.setAdapter(adapter);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您已实施:

onItemClick()

在此处,您将获得列表中项目的位置作为参数。在这里你可以写一个开关盒来完成你的任务。单击任何列表项时会触发此函数。

你可以这样做:

    private string[] mArticles={
            "abc",
            "def",
            "ghi",
            "jkl"
    };

@Override
public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {

openArticle(mArticles[position]);

}

创建字符串数组时,请为每个项目位置保留适当的artile链接。