代码之家  ›  专栏  ›  技术社区  ›  g.geloso

Android SimpleAdapter更改元素样式

  •  0
  • g.geloso  · 技术社区  · 15 年前

    我得到了一个显示元素列表的SimpleAdapter,它获取要显示的元素列表。 object列表中的元素(我传递给SimpleAdapter的元素)有一个字段,我想用它来区分要显示的元素的样式(元素是TextView),但我不知道怎么做。

    杰克斯

    1 回复  |  直到 15 年前
        1
  •  2
  •   Community Mohan Dere    9 年前

    您需要将Adapter(或其子类型之一)子类化,并在 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 . 你也应该在谷歌I/O中明确地查看LoMist-Ge的ListVIEW演示文稿。 2009 2010