【问题标题】:Android: Line break not working in text from plist(XML) fileAndroid:换行符不适用于 plist(XML)文件中的文本
【发布时间】:2012-07-18 02:22:51
【问题描述】:

我正在创建一个显示从 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 文件的 sn-p:

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

【问题讨论】:

  • 而且 XML 文件绝对不是 plist 文件 :) 显示“数据”类代码以及它是如何定义的 toString()
  • 感谢您的回复。仅供参考,在 Mac 和 iOS 上,plist 存储为 XML 文件。我使用 XMLPullParser 从 plist 中检索我的数据。我已经发布了我的解析例程。检索我的数据没有任何问题。我只是在文本中嵌入 \n 代码时遇到问题。
  • 啊,对不起,我误会了。我以为您正在使用 Android XML 资源文件并将其称为“plist”。造成这种情况的原因是您的换行符以某种方式被逃脱了。在调试器中看到的contactData 的值是多少?还有相关的 XML 文件部分是什么样子的。
  • 我知道,在谈论 XML 数据文件时会让人感到困惑,而且人们正在考虑布局 XML 文件! ;-D 无论如何,在调试中查看我的 String 变量,文本中似乎有一个额外的 \。它不在原始数据中,但在解析过程中以某种方式被插入。它看起来像这样:“第一行\\n第二行\\n第三行”
  • &lt;![CDATA[ ]]&gt; 包装你的文本并使用实际的换行符。

标签: android string android-layout xml-parsing textview


【解决方案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>

【讨论】:

  • 修复了换行问题,但标签正在显示。
  • 您需要处理CDSECT 并使用getText() 来获取里面的实际文本。见developer.android.com/reference/org/xmlpull/v1/…
  • 感谢您的帮助。我能够通过将 \\n 替换为 \n 来解决我的问题。
  • 当然可以,但它本质上是一种 hack。 CDATA 是执行此操作的正确方法,并且性能也会更好,尤其是对于较大的文本。
  • 我正在将我的应用程序从 iOS 移植到 android,并希望两个应用程序共享相同的数据文件以保持连续性。主要数据存储在 SQLite 数据库中,没有任何问题。在开发 iOS 版本时,我有一些数据似乎与 Property List 结构非常吻合。一个文件有大约 130 条记录,每条记录有 6-8 个带有嵌入换行符的长文本字段。我的最终解决方案很可能是 hack 或 kludge,但我得到了我需要的结果,而不会破坏与我的 iOS 版本共享数据的连续性。
【解决方案2】:

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

contactData = dict.get("Data").toString().replace("\\n", "\n");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-25
    相关资源
    最近更新 更多