Android-更改ListView特定项目的背景颜色 [英] Android - Change background color of specific item of ListView

查看:61
本文介绍了Android-更改ListView特定项目的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一款游戏,该游戏将使用Listview来显示一些文本作为聊天记录.

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

其中一些文本表示事件,因此我想根据特定事件更改其背景颜色: 前任.有人被杀,特定的Listview物品将具有红色背景颜色

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

我该如何实现?

这是我的JAVA代码:

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

这是我的XML代码:

<?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>

推荐答案

ArrayAdapter覆盖getView.根据情况更改颜色或执行所需的操作.您已经为ArrayAdapter构造函数提供了参数.因此,无需再次扩大视图.获取相同的内容并执行所需的操作.同样,getItem(position)获取指定位置的数据.

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

这篇关于Android-更改ListView特定项目的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆