0

我正在创建一个显示从 plist (XML) 文件解析的数据的视图。我的解析例程将数据作为对象存储在哈希图中,稍后我会使用键检索该哈希图中。我的 textview 确实显示了数据,但它不会将 \n 处理为换行符。相反,\n 与文本一起显示。这是我用来从哈希图中检索数据的代码:

String contactData = dict.get("Data").toString();

我尝试了变化但没有成功:

Object obj =  dict.get("Data");
String contactData = obj.toString();
and
contactData = (String)dict.get("Data");

我的文本很长,嵌入了 \n 以强制换行。我将我的文本设置如下:

TextView data = (TextView) getActivity().findViewById(R.id.data);
data.setText(contactData);

这是我的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/light_grey"
android:orientation="vertical" >

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/midnight_blue"
    android:gravity="center"
    android:text="Contacts"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="@color/white"
    android:textStyle="bold" />

<TextView
    android:id="@+id/country"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:gravity="center"
    android:textColor="@color/midnight_blue"
    android:textSize="16dp" />

<TextView
    android:id="@+id/name"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:background="@color/white"
    android:gravity="center"
    android:textColor="@color/midnight_blue"
    android:textSize="16dp" />

<TextView
    android:id="@+id/data"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:gravity="center"
    android:singleLine="false"
    android:textColor="@color/black"
    android:textSize="12dp" 
    android:text="Line one\nLine two\nLine3"/>

\n 如果我将文本硬编码到我的变量 (contactData) 或者我在布局 XML 文件中使用默认文本,则可以正常工作。当我将对象数据转换为字符串时它不起作用??

请不要建议转换为\r\n。\n 是行分隔符。我在上一个问题中已经走下这条线,但这不是解决方案。以下代码返回\n:

System.getProperty("line.separator");

我的问题与我如何从哈希图中检索文本并将其转换为字符串有关。任何想法或建议将不胜感激!!!

*更新这是我对 plist 文件的解析例程:

    public void parse(InputStream inputStream) throws XmlPullParserException, IOException {
    final XmlPullParserFactory xppf = XmlPullParserFactory.newInstance();
    final XmlPullParser parser = xppf.newPullParser();
    parser.setInput(inputStream, null);

    final Stack<List<Map<String, Object>>> arrayStack = new Stack<List<Map<String, Object>>>();
    final Stack<Map<String, Object>> dictStack = new Stack<Map<String, Object>>();
    final Stack<String> keyStack = new Stack<String>();

    int eventType = parser.getEventType();
    boolean done = false;
    while (!done) {
        final String name = parser.getName();
        switch (eventType) {
        case XmlPullParser.START_DOCUMENT:
            break;
        case XmlPullParser.START_TAG:
            if ("array".equalsIgnoreCase(name)) {
                final List<Map<String, Object>> array = new ArrayList<Map<String,Object>>();
                arrayStack.push(array);
            } else if ("dict".equalsIgnoreCase(name)) {
                final Map<String, Object> dict = new HashMap<String, Object>();
                dictStack.push(dict);
            } else if ("key".equalsIgnoreCase(name)){
                keyStack.push(parser.nextText()); // assign current key
            } else if ("string".equalsIgnoreCase(name)) {
                final Map<String, Object> dict = dictStack.peek();
                final String string = parser.nextText();
                final String key = keyStack.pop();
                dict.put(key, string);
            } else if ("integer".equalsIgnoreCase(name)) {
                final Map<String, Object> dict = dictStack.peek();
                final String integerStr = parser.nextText();
                final Integer integer = new Integer(integerStr);
                final String key = keyStack.pop();
                dict.put(key, integer);
            }  else if ("false".equalsIgnoreCase(name)) {
                final Map<String, Object> dict = dictStack.peek();
                final Boolean booleanValue = new Boolean(false);
                final String key = keyStack.pop();
                dict.put(key, booleanValue);
            } else if ("true".equalsIgnoreCase(name)) {
                final Map<String, Object> dict = dictStack.peek();
                final Boolean booleanValue = new Boolean(true);
                final String key = keyStack.pop();
                dict.put(key, booleanValue);
            }

            break;
        case XmlPullParser.END_TAG:
            if ("array".equalsIgnoreCase(name)) {
                final List<Map<String, Object>> array = arrayStack.pop();
                if (arrayStack.isEmpty()) {
                    // return array;
                    mPlistHashMap.put("array",array);
                    break;
                }
                // If not end of array, means it's an array within a dict
                final String key = keyStack.pop();
                dictStack.peek().put(key, array);
            } else if ("dict".equalsIgnoreCase(name)) {
                final Map<String, Object> dict = dictStack.pop();
                if (!arrayStack.empty())
                    arrayStack.peek().add(dict);
                else
                    mPlistHashMap = dict;
            }
            break;
        case XmlPullParser.END_DOCUMENT:
            done = true;
            break;
        }
        eventType = parser.next();
    }

}

我的 plist 包含一个包含三个字符串的字典项数组。这是我的 plist XML 文件的片段:

<array>
    <dict>
        <key>Country</key>
        <string>Mexico</string>
        <key>Name</key>
        <string>Mexico City</string>
        <key>Data</key>
        <string>Line one\nLine two\nLine three</string>
    </dict>
4

2 回答 2

1

将您的文本换行并使用实际的换行符。然后解析器应该为您提供与写入完全相同的数据。就像是:

   <array>
        <dict>
            <key>Country</key>
            <string>Mexico</string>
            <key>Name</key>
            <string>Mexico City</string>
            <key>Data</key>
            <string>
<![CDATA[
Line one
Line two
Line three
]]>
            </string>
        </dict>
    </array>
于 2012-07-18T05:47:03.267 回答
0

多亏了 Nikolay 的建议,我发现我的文本以某种方式嵌入了换行符并增加了额外的反斜杠。所以我的文字有“\n”而不是“\n”。我曾尝试使用 .replaceAll("\n","\n"),但由于某种原因,它不起作用。这是对我有用的代码:

contactData = dict.get("Data").toString().replace("\\n", "\n");
于 2012-07-19T01:44:03.337 回答