android - 如何将xml加载到webview android

标签 android webview

我有一个 xml 文件,我想将 xml 内容加载到一个 web View 中,该 web View 将被扩展到一个画廊。我已经解析并能够获取解析值,但如何将 xml 内容加载到 Web View ?

这是我用来加载 xml 的代码:

public class ReadContent extends Activity implements GalleryListener {
private GalleryView myGallery;
Context mContext = null;
private ContentAdapter contentAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.readcontent);
    myGallery = (GalleryView) findViewById(R.id.myGallery);
    myGallery.setGalleryListener(ReadContent.this);
    myGallery.setVerticalFadingEdgeEnabled(false);
    contentAdapter = new ContentAdapter();
    myGallery.setAdapter(contentAdapter);
}

class ContentAdapter extends BaseAdapter {

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return Intro.book.getsecretList().size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View view = convertView;
        if (convertView == null) {
            // view = inflater.inflate(resourceid, null);
            view = getLayoutInflater().inflate(R.layout.webviewitem, null);
        }
        synchronized (view) {
            TextView txtTopic = (TextView) view
                    .findViewById(R.id.txtChapter);
            TextView txtSubTopic = (TextView) view
                    .findViewById(R.id.txtSubChapter);
            WebView wv = (WebView) view.findViewById(R.id.contentWebView);
            txtTopic.setText(Intro.book.getsecretList().get((int) position)
                    .getId().toString());
            txtSubTopic.setText(Intro.book.getsecretList()
                    .get((int) position).getId().toString());
            // webview content
            WebSettings settings = wv.getSettings();
            settings.setUseWideViewPort(true);
            settings.setLoadWithOverviewMode(true);
            settings.setJavaScriptEnabled(true);
            settings.setDefaultZoom(ZoomDensity.FAR);
            wv.setBackgroundColor(0);
            wv.setVerticalScrollBarEnabled(false);
            wv.setHorizontalScrollBarEnabled(false);
            
            wv.loadUrl("file:///android_asset/xxx.xml");
        }
        return view;
    }
}}

请帮我修改代码,我将不胜感激。

<prelim>
    <prelimTitle>Managing people is hard but rewarding</prelimTitle>
    <prelimIntro>As you go through life, you will increasingly find that you need to manage people. A parent has to manage their family; a supervisor or team leader has to manage a small team; an entrepreneur may have to manage staff, customers and suppliers.</prelimIntro>
    <prelimInsight>Knowing how to manage people well is one of the most important skills in life.</prelimInsight>
    <prelimContent>

        <![CDATA[
        <p>Early in my career I took responsibility for managing people. I managed up to 250 highly trained professionals who worked as a tight-knit team. It didn’t matter that I was the youngest person in the team! For over 20 years I’ve been working with individuals and organizations to help them improve their management of people. This has ranged from military personnel to entrepreneurs, from charities to government departments. I’ve learned many secrets and tricks over these years. Some I’ve discovered for myself, but many I’ve learned from others. Humans are wonderfully inventive!</p>
        <p>This book aims to help you improve your skills at managing people – to help you find ways in which everybody benefits. It contains 50 secrets, grouped into seven themed chapters.</p> 
        <p>
        <ul>
        <li>
        <span class='pointHeading'>Build on a strong foundation.</span><span class='normalText'>You must understand what type of leader or manager you want to be. Your employer may give guidelines, but you must exert control over your day-to-day behaviour.</span>   
        </li>
        <li>
        <span class='pointHeading'>Create a great team.</span><span class='normalText'>This shows how to choose the right people and quickly build a functioning team.</span>   
        </li>
        <li>
        <span class='pointHeading'>Set goals and targets.</span><span class='normalText'>By setting people effective targets and goals, you can monitor progress and offer appropriate rewards.</span>   
        </li>
        <li>
        <span class='pointHeading'>Motivate yourself and your people.</span><span class='normalText'>Implementing ways to motivate people is ultimately much easier than having to cajole and constantly monitor unmotivated people.</span>   
        </li>
        <li>
        <span class='pointHeading'>Manage good performance.</span><span class='normalText'>You need to recognize good performance – reward it, develop it, perpetuate it and spread it to others.  Otherwise you will lose your good performers and be left only with the poor ones.</span>   
        </li>
        <li>
        <span class='pointHeading'>Manage poor performance.</span><span class='normalText'>Some managers find ways of managing around poor performance without tackling the poor performance itself. However, this encourages more poor performance, from both the original perpetrator and everyone else. Know how to tackle the problems head on.</span>   
        </li>
        <li>
        <span class='pointHeading'>Develop your people.</span><span class='normalText'>Though often overlooked by managers, another fundamental task is developing people. You need to improve the less able, stretch and reward the able, plan succession for the future and mentor your people’s changing needs. </span>   
        </li>
        </ul>
        </p>
        <p>Managing people is a hugely complex area in which you never stop learning. The secrets contained in this book will help you make massive strides towards succeeding in this fascinating role.</p>
        ]]>
    </prelimContent>
</prelim>

最佳答案

我认为你需要在标签中显示内容,

然后解析您的 xml,并将 webview 显示为:

myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadDataWithBaseURL(null, myPreviewContents, mime, encoding, null);

myPreviewContents 是:

<p>Early in my career I took responsibility for managing people. I managed up to 250 highly trained professionals who worked as a tight-knit team. It didn’t matter that I was the youngest person in the team! For over 20 years I’ve been working with individuals and organizations to help them improve their management of people. This has ranged from military personnel to entrepreneurs, from charities to government departments. I’ve learned many secrets and tricks over these years. Some I’ve discovered for myself, but many I’ve learned from others. Humans are wonderfully inventive!</p>
        <p>This book aims to help you improve your skills at managing people – to help you find ways in which everybody benefits. It contains 50 secrets, grouped into seven themed chapters.</p> 
    <p>
    <ul>
    <li>
    <span class='pointHeading'>Build on a strong foundation.</span><span class='normalText'>You must understand what type of leader or manager you want to be. Your employer may give guidelines, but you must exert control over your day-to-day behaviour.</span>   
    </li>
    <li>
    <span class='pointHeading'>Create a great team.</span><span class='normalText'>This shows how to choose the right people and quickly build a functioning team.</span>   
    </li>
    <li>
    <span class='pointHeading'>Set goals and targets.</span><span class='normalText'>By setting people effective targets and goals, you can monitor progress and offer appropriate rewards.</span>   
    </li>
    <li>
    <span class='pointHeading'>Motivate yourself and your people.</span><span class='normalText'>Implementing ways to motivate people is ultimately much easier than having to cajole and constantly monitor unmotivated people.</span>   
    </li>
    <li>
    <span class='pointHeading'>Manage good performance.</span><span class='normalText'>You need to recognize good performance – reward it, develop it, perpetuate it and spread it to others.  Otherwise you will lose your good performers and be left only with the poor ones.</span>   
    </li>
    <li>
    <span class='pointHeading'>Manage poor performance.</span><span class='normalText'>Some managers find ways of managing around poor performance without tackling the poor performance itself. However, this encourages more poor performance, from both the original perpetrator and everyone else. Know how to tackle the problems head on.</span>   
    </li>
    <li>
    <span class='pointHeading'>Develop your people.</span><span class='normalText'>Though often overlooked by managers, another fundamental task is developing people. You need to improve the less able, stretch and reward the able, plan succession for the future and mentor your people’s changing needs. </span>   
    </li>
    </ul>
    </p>
    <p>Managing people is a hugely complex area in which you never stop learning. The secrets contained in this book will help you make massive strides towards succeeding in this fascinating role.</p>
    ]]>

关于android - 如何将xml加载到webview android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8928100/

相关文章:

java - 如何使用 Joda-Time 计算两个日期之间的年数、月数和天数

Android Load Cache Only 不工作。离线显示 WebView

java - BufferedOutputStream.close() 会让 BufferedInputStream 关​​闭?

objective-c - 打开一个 .webarchive 修改它并保存它

android - Admob 广告未显示 - Android

javascript - 如何在 WebView 中处理此浏览器提示?

c++ - QML 文本滚动

android - 单击后 onListItemClick 停止工作

android 模拟器 ==> 改变 heapSize?

java - Android 中的希伯来语短信