由于特殊字符,无法显示从Web服务器检索的数据

时间:2013-07-31 01:16:53

标签: java android regex special-characters

由于特殊字符,我从网络服务器检索的数据未显示在我的列表视图中。对于我检索到的示例之一,数据为:****要分享的两种策略<p> A)公司选择<p> ** 在这种情况下,它只显示“两个要共享的策略”。我可以知道有什么建议吗?我试过正则表达式,但我不确定应该如何在我的文件中实现它。这是我的编码:

package com.example.fambond;

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 {

    static final String URL = "http://api.eventful.com/rest/events/search?app_key=XXXXXXXXXXXXXXXXXXX&location=singapore";
    // XML node keys
    static final String KEY_EVENT = "event"; // parent node
    static final String KEY_TITLE = "title";
    static final String KEY_URL = "url";
    static final String KEY_DESC = "description";
    static final String KEY_START_TIME = "start_time";
    static final String KEY_STOP_TIME = "stop_time";
    static final String KEY_VENUE_NAME = "venue_name";
    static final String KEY_COUNTRY_NAME = "country_name";

    String regex = "</{0,1}.+?>";
    String replacement = "";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

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

        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL); // getting XML
        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_EVENT);
        // 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_TITLE, parser.getValue(e, KEY_TITLE));
            map.put(KEY_URL, parser.getValue(e, KEY_URL));
            map.put(KEY_DESC, "Description: " + parser.getValue(e, KEY_DESC));
            map.put(KEY_START_TIME, parser.getValue(e, KEY_START_TIME));
            map.put(KEY_STOP_TIME, parser.getValue(e, KEY_STOP_TIME));
            map.put(KEY_VENUE_NAME, parser.getValue(e, KEY_VENUE_NAME));
            map.put(KEY_COUNTRY_NAME, parser.getValue(e, KEY_COUNTRY_NAME));
            // adding HashList to ArrayList

            menuItems.add(map);

        }

        // Adding menuItems to ListView
        ListAdapter adapter = new SimpleAdapter(this, menuItems,
                R.layout.list_item, new String[] { KEY_TITLE, KEY_DESC, KEY_COUNTRY_NAME,
                        KEY_VENUE_NAME, KEY_START_TIME }, new int[] {
                        R.id.title, R.id.description, R.id.countryName, R.id.venueName,
                        R.id.startTime });

        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 title = ((TextView) view.findViewById(R.id.title))
                        .getText().toString();

                HashMap<String, String> description = menuItems.get(position);

                // Starting new intent
                Intent in = new Intent(getApplicationContext(),
                        SingleMenuItemActivity.class);
                in.putExtra(KEY_TITLE, title);
                in.putExtra(KEY_DESC, description);

                startActivity(in);

            }
        });
    }
}

有关如何解决此问题的任何建议?

1 个答案:

答案 0 :(得分:0)

尝试spannable字符串构建器。它将保留字符串的内容和标记,您将能够显示这些特殊字符。

http://developer.android.com/reference/android/text/SpannableStringBuilder.html

示例:

SpannableStringBuilder stringBuilder = new SpannableStringBuilder();
stringBuilder.append(text);

现在在ListView文本中就像这样把它放在

textView.setText(stringBuilder);