gpt4 book ai didi

android - 在 Android Listview 中显示 Html

转载 作者:行者123 更新时间:2023-11-30 03:03:25 26 4
gpt4 key购买 nike

我的应用程序中有一个 Twitter 提要,我试图通过将它们包装在 html 中来更改主题标签、提及、链接等的外观。

这是目前的样子:

enter image description here

我的问题是,如何让我的 ListView 识别 Html 标签?

这是我的 ListView 代码:

try{
Twitter twits = jsonToTwitter(result);

// lets write the results to the console as well
for (Tweet tweet : twits) {

// send the tweets to the adapter for rendering
listview = (ListView) getView().findViewById(R.id.listview);

textview = (TextView) getView().findViewById(R.id.text);
ArrayAdapter<Tweet> adapter = new ArrayAdapter<Tweet>(getActivity(), R.layout.listview_item, twits);

listview.setAdapter(adapter);


mProgressDialog.hide();
}

}catch(NullPointerException e){

mProgressDialog.hide();
System.out.println("No Network Available");

}
}

在 html 中包装主题标签等:

@Override
public String toString(){


Pattern mentionPattern = Pattern.compile("(@[A-Za-z0-9_-]+)");
Pattern hashtagPattern = Pattern.compile("(#[A-Za-z0-9_-]+)");
Pattern urlPattern = Patterns.WEB_URL;

StringBuffer sb = new StringBuffer(Text.length());
Matcher o = hashtagPattern.matcher(Text);

while (o.find()) {
o.appendReplacement(sb, "<b>" + o.group(1) + "</b>");
}
o.appendTail(sb);

Matcher n = mentionPattern.matcher(sb.toString());
sb = new StringBuffer(sb.length());

while (n.find()) {
n.appendReplacement(sb, "<font color=\"#657383\">" + n.group(1) + "</font>");
}
n.appendTail(sb);

Matcher m = urlPattern.matcher(sb.toString());
sb = new StringBuffer(sb.length());

while (m.find()) {
m.appendReplacement(sb, "<font color=\"#EDDA74\">" + m.group(1) + "</font>");
}
m.appendTail(sb);




return sb + "\n"+ getDateCreated() ;

}

最佳答案

在适配器的 getView() 方法中(实际填充 View 的地方),您可以将 TextView 的文本设置为 HTML:

mTextView.setText(Html.fromHtml(yourHtmlString));

重要的部分是:

Html.fromHtml(String)

还注意到了一些其他的东西:

for (Tweet tweet : twits) {
// send the tweets to the adapter for rendering
listview = (ListView) getView().findViewById(R.id.listview);
textview = (TextView) getView().findViewById(R.id.text);
ArrayAdapter<Tweet> adapter = new ArrayAdapter<Tweet>(getActivity(), R.layout.listview_item, twits);
listview.setAdapter(adapter);
mProgressDialog.hide();
}

为什么每次循环浏览推文时都要创建一个新的适配器?您应该创建一次并在需要时将数据添加到其中(您不需要这样做,因为您正在为适配器提供数据)。

关于android - 在 Android Listview 中显示 Html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22201346/

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