2011-03-04 35 views
0

我是一个Android新手,我已经构建了一个简单的基于免费的IBM机器人RSS教程的RSS阅读器应用程序。如果该行的类别等于特定的字符串,我想更改每行的背景颜色。基于我的RSS阅读器的ListView中的内容的行颜色

我写了下面的“for循环”,它发现每个项目的类别,并运行一个if语句,如果该类别等于“新闻”。目前,只要Feed提供新闻类别,整个列表视图的背景颜色就会发生变化。

有没有人觉得像帮助初学者?

for(int i = 0; i < feed.getItemCount(); i++) 
{ 
    if (feed.getItem(i).getCategory().equals("News")) 
    { 
     ListView.setBackgroundColor(0x77ee0044); 
    } 
} 
+0

你能否包含指南的链接,以便我们可以更具体? – 2011-03-04 17:25:33

+0

Hi Matthew,http://www.ibm.com/developerworks/cn/education/xml/x-androidrss/index.html – Mat 2011-03-04 17:27:47

+0

private void UpdateDisplay() {TextView feedtitle =(TextView)findViewById(R.id.feedtitle) ; TextView feedpubdate =(TextView)findViewById(R.id.feedpubdate); ListView itemlist =(ListView)findViewById(R.id.itemlist); ArrayAdapter adapter = new ArrayAdapter (this,android.R.layout.simple_list_item_1,feed.getAllItems()); itemlist.setAdapter(adapter); itemlist.setOnItemClickListener(this); itemlist.setSelection(0); \t \t return; – Mat 2011-03-04 17:32:39

回答

0

您将需要在适配器的getView()方法内执行此操作。

在返回getView()中的视图之前,您可以在视图上调用setBackgroundColor()或使用其他setBackgroundFoo()方法之一。

编辑 - 给你的代码,当你创建你的适配器,尝试:

ArrayAdapter<RSSItem> adapter = new ArrayAdapter<RSSItem>(
    this,android.R.layout.simple_list_item_1,feed.getAllItems(­)) { 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View view = super.getView(position, convertView, parent); 
     if (getItem(position).getCategory().equals("News")) { 
      view.setBackgroundColor(0xffff0000); // red 
     } 
     return view; 
    } 
}; 

这将覆盖getView()适当地调整你的列表项的看法的背景。

+0

非常感谢马修,我花了几个小时绕着圈子走。 – Mat 2011-03-04 18:37:14

+0

没问题。我很惊讶这可以用这么小的代码来完成。 – 2011-03-04 18:38:59