简体   繁体   English

动态更改ListView中TextView的文本颜色?

[英]Dynamically change text color of a TextView inside a ListView?

I'm trying to create a game lobby for a project, and I'd like the game's status text to be a different color: red for an "[IN PROGRESS]" game and green for a game that's "[Waiting for x players]". 我正在尝试为一个项目创建一个游戏大厅,我希望游戏的状态文本是一个不同的颜色:红色表示“[IN PROGRESS]”游戏,绿色表示游戏“[等待x玩家]”。 The ListView will be populated with data, and each ListView item will have a game ID and then immediately to the right of that the game's status. ListView将填充数据,每个ListView项目将具有游戏ID,然后立即在游戏的状态右侧。

Right now I'm essentially using the Hello ListView code to create my ListView. 现在我基本上使用Hello ListView代码来创建我的ListView。

(In constructor) (在构造函数中)

ArrayList<HashMap<String, String>> gameList = new ArrayList<HashMap<String, String>>();
private ListView gamesListView;
private SimpleAdapter gameItems.

(In onCreate()) (在onCreate()中)

gamesListView = (ListView) findViewById(R.id.listViewGames);
gameItems = new SimpleAdapter(this, gameList, R.layout.gamelistitem, 
    new String[] { "line1, "line2}, 
    new int[] { R.id.tvGameID, R.id.tvGameStatus });
gamesListView.setAdapter(gameItems);

Then, I have a refresh button that obtains data from our database. 然后,我有一个刷新按钮,从我们的数据库中获取数据。 It will get two Strings, one for the ID of the game, and the other for the status of the game. 它将获得两个字符串,一个用于游戏的ID,另一个用于游戏的状态。 If the game status is 0, then it's still waiting for players. 如果游戏状态为0,那么它仍在等待玩家。 If the game status is 1, then the game has started. 如果游戏状态为1,则游戏已开始。 So, I create a gameItem to add to the gameList: 所以,我创建了一个gameItem来添加到gameList:

HashMap<String, String> gameItem = new HashMap<String, String>();
gameItem.put("line1", gameIDString);
gameItem.put("line2", renderGameStatus(gameStatusString));
gameList.add(gameItem);
gameItems.notifyDataSetChanged();

private String renderGameStatus(String statusString) {
    if (statusString.equals("0")) {
        return "[Waiting for players]";
    } else {
        return "[IN PROGRESS]";
    }
}

This is where I'm stuck. 这就是我被困住的地方。 I don't really understand how I can alter the specific TextView when I create the gameItem or when I add that to the gameList. 在创建gameItem或将其添加到gameList时,我真的不明白如何更改特定的TextView。 There isn't a way I can see of accessing the TextView's properties. 我无法看到访问TextView属性的方法。 I see how the text of the view is set (through the mapping of the strings to "line1" and "line2", but I don't know how to change any of the properties. 我看到如何设置视图的文本(通过将字符串映射到“line1”和“line2”,但我不知道如何更改任何属性。

Thanks for any help! 谢谢你的帮助!

Do you create your textview in the XML file? 您是否在XML文件中创建了textview? if so try: 如果是这样尝试:

TextView t = (TextView) findViewById(R.layout.yourTextView);
t.setTextColor(color);
t.setBackgroundColor(color);

hope that helps 希望有所帮助

The correct answer to this question is to follow one of the many tutorials out there on custom ListView rows, and using a custom Adapter for your ListView. 这个问题的正确答案是在自定义ListView行上遵循许多教程之一,并为ListView使用自定义适配器。 Here's a good one. 这是一个很好的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM