我的应用程序中有一个twitter功能,显示特定用户的公共时间表。
我的目标是检测所有主题标签,提及,链接等,并将它们包装在html中以改变它们的外观。
到目前为止这已经奏效了,以上所有内容都包含在Html中,但它们的外观并未改变。见下文
推特课
public class Tweet {
@SerializedName("created_at")
private String DateCreated;
@SerializedName("text")
private String Text;
public String getDateCreated() {
SimpleDateFormat format = new SimpleDateFormat("MMM dd, hh:mm a");
String date = format.format(Date.parse(DateCreated));
return date;
}
public StringBuffer getText() {
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, "<font color=\"#437C17\">" + o.group(1) + "</font>");
}
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);
//textView.setText(Html.fromHtml(sb.toString()));
return sb;
}
public void setDateCreated(String dateCreated) {
DateCreated = dateCreated;
}
public void setText(String text) {
Text = text;
}
@Override
public String toString(){
return getText() + "\n"+ getDateCreated() ;
}
}
我的Twitter片段中的OnPostExecute方法
try{
Twitter twits = jsonToTwitter(result);
for (Tweet tweet : twits) {
listview = (ListView) getView().findViewById(R.id.listview);
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");
}
}
JSON数据到twitter对象方法
// converts a string of JSON data into a Twitter object
private Twitter jsonToTwitter(String result) {
Twitter twits = null;
if (result != null && result.length() > 0) {
try {
Gson gson = new Gson();
twits = gson.fromJson(result, Twitter.class);
} catch (IllegalStateException ex) {
// just eat the exception
}
}
return twits;
}
感谢您的帮助! :)