【问题标题】:Android replace html entities inside Listview from databaseAndroid从数据库中替换Listview中的html实体
【发布时间】:2012-03-19 23:42:15
【问题描述】:

我的第一篇文章,我目前正在创建一个简单的 android 应用程序,该应用程序从数据库中提取数据并将其显示在列表视图中。除了从中提取的数据包含 HTML 实体外,其中 90% 已完成。这是数据库和光标以及所有布局等的示例:

这只是一个简单的测试应用程序,因此我可以学习(最终会演变成一个合适的应用程序)。其背后的想法是,当您选择产品时,它会启动一个新的意图,提供有关该产品的详细信息(将产品名称或 ID 传递给下一个活动)。

这是我的 list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" 
    android:layout_alignParentLeft="true">

    <TextView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/product_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20sp" />

    <LinearLayout
        android:id="@+id/linearLayout4"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <TextView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/product_desc"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="15sp" />

    </LinearLayout>

</LinearLayout>
</RelativeLayout>

这里

这是代码

  final Cursor dataCursor = database.query("products", fields, null, null, null, null, "_id desc", null);

  dataAdapter = new SimpleCursorAdapter(this, R.layout.list_item, dataCursor, fields, 
                    new int[] { R.id.product_name, R.id.product_desc });

  this.setListAdapter(dataAdapter);

  mListView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

            Intent intent = new Intent(parent.getContext(), Filter.class);
            intent.putExtra("product_name",dataCursor.getString(0));

            startActivity(intent);

        }
      });

问题出在这里...数据库中包含一些 html 实体。

数据基本是这样的:

table products:
________________________________________________
|  product_name    |  product_desc |    _id    |
------------------------------------------------
|Product1 Name&reg;| Super special |     1     |
|Product1 Name&reg;| Super special |     2     |
|Product1 Name&reg;| Super special |     3     |
|Product1 Name&reg;| Super special |     4     |
________________________________________________

我想要完成的是 ® 实体实际上被转换为“已注册”符号并显示在列表项中。

我尝试应用我在这里找到的 Android Display HTML in a ListView 但这不起作用(可能是因为该人没有使用光标而只是一个字符串数组,我不知道)。 (显然我的经验不是那么好)。

当我进行测试时,在选择列表项并将其与 apache StringEscapeUtils 一起使用时显示一个简单的 toast,它工作得很好,® 和任何其他 html 实体都被转换为正确的 (r) 符号,它看起来太棒了。

Toast.makeText(getApplicationContext(), "产品名称是:" + StringEscapeUtils.unescapeHtml4(dataCursor.getString(0)), Toast.LENGTH_SHORT).show();

非常感谢任何帮助。

【问题讨论】:

    标签: android html listview


    【解决方案1】:

    在 Html Utility 类中使​​用 fromHtml 方法。它将返回一个 Spanned 对象,您可以使用它来填充列表中的 TextViews。

    http://developer.android.com/reference/android/text/Html.html#fromHtml(java.lang.String)

    【讨论】:

    • 这只是简单地覆盖光标的 getItem 方法吗?
    猜你喜欢
    • 1970-01-01
    • 2012-12-29
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    • 2012-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多