简体   繁体   中英

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 . 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. If CheckBox is checked change background of 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. This is the method we're interested in the most. It will be called every time the ListView draws a new row. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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