0

我是意大利人,我是 android 语言的新手。请帮助我并原谅我非常糟糕的英语。我需要在我的 sqlite db 上使用 html 标签(如)。我插入了单词,但它们的显示方式就像写的一样(例如“047017661
X”)。我的页面代码是:

public class MainActivity extends Activity {
/** Called when the activity is first created. */

//Android listview object
//  ListView listViewPhoneBook;

    DBAdapter books;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    openDB();
    //---add 2 titles---
    /* if(books.getAllTitles().getCount()==0)*/ {//inserimento dati, solo se il db è vuoto
    long id;
    id = books.insertTitle(
            "047028   <B5818>",
            "C# 2008 Programmer's Reference",
            "Wrox");        
    id = books.insertTitle(
            "0470</br>17661<br/>X",
            "Professional Windows </br>Vista Gadgets Programming",
            "Wrox");}


populateListViewFromDB();}



private void openDB(){
    books = new DBAdapter(this);
    books.open();
}
private void closeDB(){
    books.close();}



    private void populateListViewFromDB() {
        Cursor cursor = books.getAllTitles();


        startManagingCursor(cursor);


        String[] fromFieldNames = new String[]
                {DBAdapter.KEY_ISBN};
        int[] toViewIDs = new int []
                {R.id.textView1};


    SimpleCursorAdapter myCursorAdapter =
            new SimpleCursorAdapter(
                    this, 
                    R.layout.item_layout,
                    cursor,
                    fromFieldNames,
                    toViewIDs);

    ListView myList = (ListView) findViewById(R.id.listViewFromDB);
    myList.setAdapter(myCursorAdapter);
        // TODO Auto-generated method stub

    }
}

我必须在哪里插入一些东西来解码 html 标签?我搜索了类似“textView.setText(Html.fromhtml”)的内容,但我不明白该指令放在哪里。放入文件 xml?放入文件 Java?你能帮我吗?在这个网站上,我读了这篇文章:如果你的数据仅包含简单的 HTML 标记,它们实际上可以由 TextView 使用 Html.fromHtml(yourString) 处理。该静态方法返回一个 Spanned,它可以由 TextView 显示,其开销远低于 WebView。回答 Jan 11 ' 11 时 4:53 伊恩·克利夫顿

对不起,但我不知道我可以把这段代码放在哪里。我尝试扩展 TextView 但 setText 方法设置为最终方法。– 基兰 2011 年 1 月 12 日在 3:28

您不需要扩展 TextView,只需将 Html.fromHtml() 返回的 Spanned 传递给它,而不是 Cursor 中的 String。您可以扩展 SimpleCursorAdapter 并覆盖 setViewText 方法。

这对我上面的代码意味着什么?

4

1 回答 1

1

更新:

检查这个。它可能会让您更好地了解它是如何工作的,并且您可能更愿意重新组织您的代码(尽管我知道您只是在测试)。

END_OF_UPDATE

改变:

id = books.insertTitle(
        "0470</br>17661<br/>X",
        "Professional Windows </br>Vista Gadgets Programming",
        "Wrox");}

至:

id = books.insertTitle(
        Html.escapeHtml("0470</br>17661<br/>X"),
        Html.escapeHtml("Professional Windows </br>Vista Gadgets Programming"),
        Html.escapeHtml("Wrox"));}

更新:

此时,您的标题在数据库中已正确转义,因此当您决定显示它们时需要取消转义。您正在使用带有 SimpleCursorAdapter 的 ListView,因此我建议您为 ListView 创建一个自定义适配器并使用 Html.fromHTML:

textView.setText(Html.fromHtml(StringFromTheCursor), TextView.BufferType.SPANNABLE);

查看:

END_OF_UPDATE

顺便说一下,检查一下(TextView 接受的旧的非官方标签列表)。

另一种选择(取决于您的应用程序的方法)可能是使用 webview 而不是 textview(和 ListView)并忘记以前的转义/取消转义解决方案:

WebView mywebview = new WebView(context);
String content = StringFromTheCursor;
mywebview.loadData(content,"text/html","utf-8");

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

public static String escapeHtml (CharSequence text)

    Added in API level 16
    Returns an HTML escaped representation of the given plain text.

public static Spanned fromHtml (String source)

    Added in API level 1
    Returns displayable styled text from the provided HTML string. Any <img> tags in the HTML will display as a generic replacement image which your program can then go through and replace with real images.

    This uses TagSoup to handle real HTML, including all of the brokenness found in the wild.

public static Spanned fromHtml (String source, Html.ImageGetter imageGetter, Html.TagHandler tagHandler)

    Added in API level 1
    Returns displayable styled text from the provided HTML string. Any <img> tags in the HTML will use the specified ImageGetter to request a representation of the image (use null if you don't want this) and the specified TagHandler to handle unknown tags (specify null if you don't want this).

    This uses TagSoup to handle real HTML, including all of the brokenness found in the wild.

public static String toHtml (Spanned text)

    Added in API level 1
    Returns an HTML representation of the provided Spanned text.
于 2013-05-20T22:48:20.857 回答