2012-04-26 14 views
0

私はゲームを作成しています。ユーザー名、スコア、日付などでゲームのリストを表示するために使用しているコードはありますが、TextViewsのtv_playerScoreとtv_opponentScoreので、私はそれらを比較し、それらのtextColorsを変更することができますか?私が欲しいのは、parseIntを見て、最も高い値を持つものを見て、そのテキストの色を緑に設定し、他はテキストの色を赤に設定することです。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の値はActivityからfindViewById機能を使用する必要があります取得したい場合。

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

showGames()メソッドは、活動(または似)を継承するクラスでない場合は、アクセスしたい人に視力の要素のセッター注入を行う必要があります。

最後に
tv_playerScore.getText().toString().compareTo(tv_opponentScore.getText().toString()); 

を、色変更するには:

tv_playerScore.setTextColor(Color.CYAN); 

よろしくを比較すること

0

私は私はあなたがあなた自身の特殊なアダプタクラスを記述(例えばSimpleAdapaterを拡張)し、そのGetViewメソッドを上書きする必要がありますね要するに

http://developer.android.com/resources/tutorials/views/hello-listview.html)あなたはリストビューがどのように動作するかでよく見るが必要だと思います。ここでは、値に応じてテキストビューの色を設定できます。 (リスト要素が描画されるたびにそれらをチェックするのではなく、それらを前もってソートしておくことが理にかなっていると思います...

関連する問題