Question

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]". 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.

Right now I'm essentially using the Hello ListView code to create my ListView.

(In constructor)

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

(In 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. If the game status is 0, then it's still waiting for players. If the game status is 1, then the game has started. So, I create a gameItem to add to the 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. There isn't a way I can see of accessing the TextView's properties. 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.

Thanks for any help!

Was it helpful?

Solution 2

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. Here's a good one.

OTHER TIPS

Do you create your textview in the XML file? if so try:

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

hope that helps

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top