当前位置: 动力学知识库 > 问答 > 编程问答 >

Android - Change background color of specific item of ListView

问题描述:

I'm making a game which will use a Listview to display some text as a chat.

Some of these texts represents events, so I want to change their background color according to that specific event:

EX. Somebody gets killed, that specific Listview's item will have a red background color

How do I achieve this?

This is my JAVA code:

 //STARTING SETUP

ArrayList<String> arrayListMother;

ArrayAdapter<String> arrayAdapterMother;

ListView listViewMother;

int counter;

boolean introDone = false;

String[] night = {"Welcome!","This is Lupus In Tabula!","You're gonna experience the Hasbro Game in a total new way, have fun!"};

String[] day = {"Thank you!","Great","Let's do it!"};

String[] dead = {"Thank you!","Great","Let's do it!"};

String[] saved = {"Thank you!","Great","Let's do it!"};

Button buttonGo;

//ENDING SETUP

//CREATE - CREATE//

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_game);

setup();

}

//CREATE - CREATE//

public void setup(){

arrayListMother = new ArrayList<String>();

arrayAdapterMother = new ArrayAdapter<String>( this, android.R.layout.simple_list_item_1, arrayListMother);

listViewMother = (ListView) findViewById(R.id.list_mother);

listViewMother.setAdapter(arrayAdapterMother);

buttonGo = (Button)findViewById(R.id.buttonGo);

}

And this is my XML code:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical">

<ListView

android:id="@+id/list_mother"

android:text="@string/go"

android:layout_width="match_parent"

android:layout_height="0dp"

android:textAppearance="?attr/textAppearanceListItem"

android:divider="@null"

android:layout_weight="7"/>

<Button

android:id="@+id/buttonGo"

android:layout_width="match_parent"

android:layout_height="0dp"

android:layout_weight="1"

android:text="@string/go"

android:textColor="@color/white"

android:background="@color/colorPrimary"

android:enabled="true"

android:onClick="play" />

</LinearLayout>

网友答案:

Override getView for ArrayAdapter. Based on the condition change color or do what is required. You have already provided params to ArrayAdapter constructor. So there is no need to inflate the view again. Get the same and do what is required. Similarly getItem(position) get's the data at a specidifed position.

http://developer.android.com/reference/android/widget/ArrayAdapter.html#getItem(int)

listViewMother.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayListMother) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = super.getView(position, convertView, parent);

    if(getItem(position).equals("Somebody gets killed"))
    {
       // do something change color
       row.setBackgroundColor (Color.RED); // some color  
    }
    else
    {
       // default state
       row.setBackgroundColor (Color.WHITE); // default coloe
    }
    return row;
  }
});
网友答案:

use this code:

listViewMother.getChildAt(position).setBackgroundColor(
            Color.parseColor("#00743D"));
分享给朋友:
您可能感兴趣的文章:
随机阅读: