简体   繁体   English

根据项目的状态更改列表视图中的文本视图的背景

[英]changing the background of textview in listview depending on the status of item

I have a ListView and in each row a TextView and a CheckBox . 我有一个ListView ,在每一行中都有一个TextView和一个CheckBox Items in list are populated from database. 列表中的项目是从数据库填充的。 What I want to do is change the background of TextView in specific row depending on CheckBox status. 我要做的是根据CheckBox状态更改特定行中TextView的背景。 If CheckBox is checked change background of TextView . 如果CheckBox被选中,请更改TextView背景。 Is there a way to do this 有没有办法做到这一点

use getView() in your adapter to do this it returns the row that has to be drawn. 在适配器中使用getView()来执行此操作,它将返回必须绘制的行。 This is the method we're interested in the most. 这是我们最感兴趣的方法。 It will be called every time the ListView draws a new row. 每当ListView绘制新行时都会调用它。 Here, you can control what gets drawn in a particular row, by selecting a layout and setting data into it. 在这里,您可以通过选择布局并将数据设置到其中来控制在特定行中绘制的内容。

see this tutorial 看到这个教程

将适配器添加到函数getView()

You can try to set the onClickListener of the CheckBox while creating your Activity, and change the background inside the onClick mehod 您可以尝试在创建Activity时设置CheckBox的onClickListener,并在onClick方法中更改背景

CheckBox chk = (CheckBox) findViewById(R.id.chk);
chk.setOnClickListener(new OnClickListener(){
    public void onClick(View v){
        if (chk.isChecked()){
            //Checked bg
        } else {
            //Unchecked bg
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM