gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 09:00:09 25 4
gpt4 key购买 nike

由于特殊字符,我从网络服务器检索到的数据没有显示在我的 ListView 中。对于我检索的其中一个示例,数据是: ****要共享的两种策略

A) 公司选择

**在这种情况下,它只显示到“要共享的两种策略”。我可以知道对我有什么建议吗?我尝试了正则表达式,但不确定如何在我的文件中实现它。这是我的编码:

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

}
});
}
}

关于如何解决这个问题有什么建议吗?

最佳答案

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

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

例子:

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

现在在你的 ListView 文本中就这样放

textView.setText(stringBuilder);

关于java - 由于特殊字符,从 Web 服务器检索到的数据无法显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17960030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com