列表行onclick从webview中的Parsed xml打开URL

时间:2014-03-22 18:33:10

标签: java android list webview xml-parsing

我有一个自定义列表视图,它在xml文件中解析,其中包含类似

的数据
</issue>
<article>
<id>Arts and Culture</id>
<title>Boycott the Boycott</title>
<author> LouLou Castonguay</author>
<description>There is a rumor going around that no one is going to pitch a theme for next years fashion show. I dont get it!</discription>
<thumb_url>http://graffiti.hostoi.com/00Graffiti00/Photos/Art/show.png</thumb_url>
<key>http://www.google.com</key>
</article>
</issue>

并且列表活动就像这样

public class CustomizedListView extends Fragment {  // All static variables
    static final String URL = "http://graffiti.hostoi.com/00Graffiti00/lists/00main00.xml";
    // 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";
    static final String KEY_LINK = "link";

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

        // Click event for single list row
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

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

                Intent intent = new Intent(getActivity(), Article.class);
                new Bundle();
                intent.putExtra( "b", songsList.get(position).get(KEY_LINK));
                startActivity(intent);



            }

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

最后,onclick打开的webview活动就像这样

public class Article extends Activity{
    private WebView webview;

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

        Bundle b = getIntent().getExtras();
        String KEY_LINK = b.getString("b");

        webview = (WebView) findViewById(R.id.webView);
        webview.setWebViewClient(new MyWebViewClient(this));
        webview.getSettings().setAppCacheEnabled(false);
        webview.getSettings().setBuiltInZoomControls(true);
        webview.setInitialScale(1);
        webview.loadUrl(KEY_LINK);



    }


    private class MyWebViewClient extends WebViewClient {
        public MyWebViewClient(Article article) {
        }
    }
}

并且一切运作完美,除了onclick打开网页视图的空白活动,而不是在google.com中加载任何关于我做错了什么或如何做到这一点的想法?感谢。

1 个答案:

答案 0 :(得分:0)

您已定义

 static final String KEY_LINK = "link";

并且您要将KEY_LINK发送到Article活动,您随后会设置WevView的网址。因为您没有在KEY_LINK的任何地方更新WevView的值{1}}结果如下,

webview.loadUrl("link"); 

呈现空白屏幕。

希望你现在清楚。

修改

我假设您需要xml中的以下网址,

   <key>http://www.google.com</key>

更改以下内容

static final String KEY_LINK = "link";

static final String KEY_LINK = "key";

并将以下内容添加到用于解析xml的循环的onPostExecute方法中,

 map.put(KEY_LINK, parser.getValue(e, KEY_LINK));