如何解析xml数据以在Web视图上显示

时间:2013-12-12 07:36:01

标签: android android-layout android-intent android-listview

我希望将列表视图中的数据用于webview。如何在Web视图上显示xml解析? 这是我在列表视图中的代码意图:

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


    // Getting adapter by passing xml data ArrayList
    adapter=new LazyAdapter(this, songsList);           
    list.setAdapter(adapter);

    list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
            // getting values from selected ListItem
            String Headline = ((TextView) v.findViewById(R.id.title)).getText().toString();
            String ArticleDate = ((TextView) v.findViewById(R.id.date)).getText().toString();
            String Body = ((TextView) v.findViewById(R.id.body)).getText().toString();

            // Starting new intent
            Intent in =new Intent ();
            in.setClass(news.this.getApplicationContext(), SingleMenuItemActivity.class);
            Log.i(KEY_HEADLINE, Headline);
            Log.i(KEY_ARTICLEDATE, ArticleDate);

                in.putExtra(KEY_HEADLINE, Headline);                
                in.putExtra(KEY_ARTICLEDATE, ArticleDate);
                in.putExtra(KEY_BODY, Body);
            startActivity(in);

        } 
    });

1 个答案:

答案 0 :(得分:0)

使用此代码,这里前三行是从另一个活动获取数据。然后,您将使用这些字符串以HTML格式创建字符串。最后一行代码将在webView中显示。 (希望这有帮助)

String  Headline = getIntent().getExtras().getString(KEY_HEADLINE);
String  ArticleDate = getIntent().getExtras().getString(KEY_ARTICLEDATE);
String  Body = getIntent().getExtras().getString(KEY_BODY);


 String html="<html><body><h1>"+Headline+"</h1><h4> Date:"+ArticleDate+"</hr><p>"+
            Body+"</p></body></html>"; // use HEadline ,Article date and body here
WebView webview = null;
webview.loadData(html, "text/html", "utf-8");