如何更改hashmap项目的颜色

时间:2013-12-30 09:07:53

标签: android colors hashmap

我想更改hashmap中第一个和第二个项目的颜色。 我知道我可以使用getView但是有没有针对hashmaps的解决方案? 我有两个字符串数组,分别是array1和array2。

ListView listview1=(ListView) findViewById(R.id.listView2);

    List<Map<String, String>> data = new ArrayList<Map<String, String>>();
        for (int i=0; i<array2.length; i++) {
            Map<String, String> datum = new HashMap<String, String>(2);
            datum.put("title",array1[i]);
            datum.put("subtitle", String.valueOf(array2[i]));
            data.add(datum);


        }
        SimpleAdapter adapter3 = new SimpleAdapter(this, data,
                                              android.R.layout.simple_list_item_2,
                                              new String[] {"title", "subtitle"},
                                              new int[] {android.R.id.text1,
                                                         android.R.id.text2});
       listview1.setAdapter(adapter3);

XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/gta6"

    >

    <ListView
        android:id="@+id/listView2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" 
        android:scrollingCache="false"
        >
    </ListView>

</RelativeLayout>

1 个答案:

答案 0 :(得分:-1)

将您的HashMap值类型更改为CharSequence并使用Spannable ...

    List<Map<String, CharSequence>> data = new ArrayList<Map<String, CharSequence>>();
    for (int i = 0; i < array2.length; i++) {
        Map<String, CharSequence> datum = new HashMap<String, CharSequence>(2);
        String s1 = array1[i];
        String s2 = String.valueOf(array2[i]);
        if(i == 0 || i == 1) {
            SpannableString spannableString = new SpannableString(s1);
            spannableString.setSpan(new ForegroundColorSpan(Color.RED), 0, s1.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            datum.put("title", spannableString);
            spannableString = new SpannableString(s2);
            spannableString.setSpan(new ForegroundColorSpan(Color.RED), 0, s2.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            datum.put("subtitle", spannableString);
        } else {
            datum.put("title", s1);
            datum.put("subtitle", s2);
        }
        data.add(datum);
    }