简体   繁体   English

在ListView中更改文本颜色

[英]Change text color inside a ListView

I'm creating a game and here's the code I'm using to show a list of games with the user names, score, date etc. But how do I get the values of the TextViews tv_playerScore and tv_opponentScore so I can compare them and change the textColors of them? 我正在创建一个游戏,这是我用来显示游戏列表的代码,其中包含用户名,得分,日期等。但是如何获取TextViews tv_playerScore和tv_opponentScore的值,以便可以对其进行比较和更改他们的textColors吗? Because what I want is to parseInt and see which has the highest value and set its textcolor to green, and the others textcolor to red. 因为我想要的是parseInt,看看哪个具有最高的值,并将其textcolor设置为绿色,其他的textcolor设置为红色。

private void showGames(JSONArray games) throws JSONException {
    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
    HashMap<String, String> map = new HashMap<String, String>();
    for (int i = 0; i < games.length(); i++) {

        map.put("challenger", games.getJSONObject(i).getString("challengerName"));
        map.put("active", games.getJSONObject(i).getString("active"));
        map.put("opponent", games.getJSONObject(i).getString("opponentName"));
        map.put("date", games.getJSONObject(i).getString("date"));
        map.put("gameID", games.getJSONObject(i).getString("gameID"));
        map.put("amount", games.getJSONObject(i).getString("amount"));
        map.put("playerScore", games.getJSONObject(i).getString("challengerScore"));
        map.put("opponentScore", games.getJSONObject(i).getString("opponentScore"));


        if (Integer.parseInt(games.getJSONObject(i).getString("active")) == 2) {

            mylist.add(map);
        }

        map = new HashMap<String, String>();
    }

    SimpleAdapter sadapter = new SimpleAdapter(this, mylist, R.layout.list, new String[] 
            {"amount", "active", "gameID", "challenger", "opponent", "date", "playerScore", "opponentScore"},
            new int[] {R.id.tv_amount, R.id.tv_activte, R.id.tv_gameID, R.id.tv_player, R.id.tv_opponent, R.id.tv_date, R.id.tv_playerScore, R.id.tv_opponentScore}); 


    listView.setAdapter(sadapter);

} }

If you want to get the values ​​of a textview must use findViewById function from Activity. 如果要获取文本视图的值,必须使用Activity中的findViewById函数。

TextView tv_playerScore = (TextView) findViewById (R.id.tv_playerScore);

If the showGames() method is not a class that inherits from Activity (or similiar), you should make a setter injection of the elements of sight to those who want to access. 如果showGames()方法不是从Activity(或类似类)继承的类,则应向希望访问的人进行视觉注入。

To compare: 比较:

tv_playerScore.getText().toString().compareTo(tv_opponentScore.getText().toString());

Finally, to change the color: 最后,更改颜色:

tv_playerScore.setTextColor(Color.CYAN);

Regards. 问候。

I think you should have a closer look in how ListViews work ( http://developer.android.com/resources/tutorials/views/hello-listview.html ) 我认为您应该仔细研究ListViews的工作方式( http://developer.android.com/resources/tutorials/views/hello-listview.html

In short I guess you'll have to write your own Adpater class (eg extend SimpleAdapater) and write over its getView method. 简而言之,我猜您将不得不编写自己的Adpater类(例如,扩展SimpleAdapater)并覆盖其getView方法。 Here you can set the color of the textviews depending on the according value. 在这里,您可以根据相应的值设置文本视图的颜色。 (I think it would make sense to have them sorted before instead of checking them every time a list element is drawn... (我认为将它们排序之前而不是每次绘制列表元素时都对其进行检查是有意义的...

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

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