2012-04-26 116 views
0

我正在創建一個遊戲,這裏是我用來顯示用戶名,分數,日期等遊戲列表的代碼。但是,如何獲取TextViews的值tv_playerScore和tv_opponentScore,所以我可以比較它們並更改它們的textColors?因爲我想要的是parseInt並查看哪個具有最高值,並將其textcolor設置爲綠色,其他textcolor爲紅色。更改ListView中的文本顏色

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); 

}

回答

0

如果你想獲得一個TextView的值必須使用findViewById功能的活動。

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

如果showGames()方法不是從活動繼承(或similiar),你應該做的視線,誰想要訪問的元素的setter注入的類。

比較:

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

最後,來改變顏色:

tv_playerScore.setTextColor(Color.CYAN); 

問候。

0

,我認爲你應該在仔細一看列表視圖工作(http://developer.android.com/resources/tutorials/views/hello-listview.html

總之怎麼我猜你必須寫自己的Adpater類(例如延長SimpleAdapater)和寫在其getView方法。在這裏,您可以根據相應的值設置文本視圖的顏色。 (我認爲將它們排序前是有意義的,而不是每次列出元素時都檢查它們...