2010-11-22 47 views
0

我得到了一个使用SimpleAdapter显示的元素列表,该列表获取要显示的元素列表。 对象列表中的元素(我传递给SimpleAdapter的元素)有一个字段,我想用它来区分元素的样式(元素是TextView)以显示,但我不知道如何去做。Android SimpleAdapter更改元素样式

在此先感谢

Jaxer的

回答

2

你需要继承适配器(或其子之一),并沿做这个东西线在getView方法:

TextView row; 
// Recycle an already-inflated view if possible 
if (convertView == null) { 
    row = (TextView) View.inflate(android.R.layout.simple_list_item_1, getContext()); 
} else { 
    row = (TextView) convertView; 
} 
SomeModel myItem = getItem(pos); 
if (getItem(pos).displayFunky()) { //or whatever object condition you want to check 
    row.setTextColor(Color.RED) 
} else { 
    row.setTextColor(Color.GREEN) 
} 

row.setText(myItem.getDisplayText()); 

return row; 

你也可以有一个自定义适配器与多个视图类型,但这是一个足够简单的情况下,我可能不会打扰。另请参阅this SO post。您还应该查看适配器的示例代码(例如,one from NPR)和更复杂的多视图适配器tutorial。您还应该在Google I/O 20092010的ListViews上明确查看无与伦比的Romain Guy的演示文稿 - 它们将为您节省数小时的心痛,困惑,痛苦和性能调整。