简体   繁体   中英

Data retrieved from web server cannot display due to special character

The data I retrieved from web server is not displaying in my listview due to the the special characters. For one of the example I retrieved, the data is: ****Two strategies to be shared <p> A) Company selection <p> ** In this case, it only display till "Two strategies to be shared". May I know any suggestions for me to do it? I tried regex but I'm not sure how should I implement it in my file. This is my coding:

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

            }
        });
    }
}

Any suggestions on how should I solve this problem?

Try spannable string builder. It will retain content and markup of your string and you will be able to display these special characters.

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

Example:

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

Now in your ListView text just put it like this

textView.setText(stringBuilder);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM