0

私はゲームを作成していますが、ユーザー名、スコア、日付などを含むゲームのリストを表示するために使用しているコードを次に示します。それらのtextColors?私が望むのは、 parseInt を使用して、どれが最も高い値を持ち、そのテキストカラーを緑に設定し、他のテキストカラーを赤に設定することだからです。

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

}

4

2 に答える 2

0

テキストビューの値を取得したい場合は、Activity の findViewById 関数を使用する必要があります。

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

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

比べる:

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

最後に、色を変更するには:

tv_playerScore.setTextColor(Color.CYAN);

よろしく。

于 2012-04-26T14:01:00.163 に答える
0

ListViews がどのように機能するかを詳しく調べる必要があると思います ( http://developer.android.com/resources/tutorials/views/hello-listview.html )

要するに、独自の Adpater クラスを記述し (SimpleAdapter を拡張するなど)、その getView メソッドを上書きする必要があると思います。ここでは、値に応じてテキストビューの色を設定できます。(リスト要素が描画されるたびにそれらをチェックするのではなく、それらをソートすることは理にかなっていると思います...

于 2012-04-26T14:11:17.223 に答える