2014-03-05 47 views
1

我在我的应用程序中有一个twitter feed,我试图通过在html中包装它们来改变hashtags,提及,链接等的外观。在Android ListView中显示Html

继承人是什么样子至今:

enter image description here

我的问题是,如何让我的列表视图识别的HTML标记?

这里是我的列表视图代码:

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() ; 

} 

回答

1

在适配器中的getView()方法(在这里你居然填充你的视图),可以将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(); 
    } 

你为什么要创建一个新的适配器,每一次你遍历你的微博?如果需要,您应该创建一次并向其中添加数据(因为您要为数据提供适配器,所以您不需要)。

+0

不适用于我的实施,已经尝试过。 –

+0

那么你的实现是什么?因为这就是你怎么做的。 – Guardanis

+0

使用列表视图时,这种方式不正确。 –