Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...