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
410 views
in Technique[技术] by (71.8m points)

android - listview item background color change

I am working on an android application. I have created a listview by using

setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,arrayname));   
getListView().setTextFilterEnabled(true);   

Now i want to change the selected item's color. I could change the background of the selected item by placing

listviewobject.getChildAt(position).setBackgroundColor(Color.BLACK);

in onListItemClick()

this code is changing the background color but if I select any other list item then also the previously clicked list item's color is red.So I change the previously clicked listitem's color by

l.getChildAt(prevpos).setBackgroundColor(Color.BLACK);

now the problem is if i change the background of previously clicked listitems color to black.Then i can't see the text on that particular listitem.I i click again then only i can see the text on that item.So its look weired.please help me friends

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

After going through lots of posts and blogs i found this solution it works for me...

declare row variable global

public View row;

your_list.setOnItemClickListener(new OnItemClickListener() {

    public void onItemClick(AdapterView<?> a, View v,
                        int position, long id) {

    if (row != null) {
        row.setBackgroundResource(R.color.orange);
    }
    row = v;
    v.setBackgroundResource(R.color.transparent_green);
)};

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

...