Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
188 views
in Technique[技术] by (71.8m points)

android - put color in Listview dymically

I have show Array in listview. with black text color.

if i using android.R.color.black it will give runtime error.

this is my code.

list1 =(ListView)findViewById(R.id.scoreView1);
        list2 = (ListView)findViewById(R.id.scoreView2);

    for (int i = 1; i <= 3; i++) {
        datalist1.add("Qustion "+ i + "   Correct");
    }
    for (int i = 4; i <= 6; i++) {
        datalist2.add("Qusetion "+ i + "  Correct");
    }



    list1.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,R.color.TextColor,datalist1));
    list2.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,android.R.id.text1,datalist2));

my XML:

<resources>

<color name="Background">#E2E2E2</color>
<color name="TextColor">#040404</color>
<color name="White">#FFFFFF</color>
<color name="Black">#000000</color>
<color name="Red">#FF0000</color>
<color name="Green">#4AA02C</color>

</resources>

Any suggestion?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

list1.setAdapter(new ArrayAdapterthis,android.R.layout.simple_list_item_1,R.color.TextColor,datalist1));

You are using this constructor for the array adapter. The third argument is

textViewResourceId The id of the TextView within the layout resource to be populated

and you are giving TextColor. You should provide layout id.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...