如何改变一个项目的背景下,从列表视图中的code [英] How to change background of one item from listview in code

查看:87
本文介绍了如何改变一个项目的背景下,从列表视图中的code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继承了ListActivity,我需要在某些行code背景颜色改变。如何做到这一点,例如第一行设置黄色背景?

 公共类AlarmsActivity扩展ListActivity {
    私人活动currentActivity;

    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);

        setListAdapter(新ArrayAdapter<字符串>(这一点,R.layout.alarms,DATA));

        currentActivity =这一点;

        ListView的LV = getListView();
        lv.setTextFilterEnabled(真正的);

        lv.setOnItemClickListener(新OnItemClickListener(){

            公共无效onItemClick(适配器视图<>为arg0,查看ARG1,INT ARG2,
                    长ARG3){
                Toast.makeText(getApplicationContext(),
                        ((TextView中)ARG1).getText(),Toast.LENGTH_SHORT).show();


            }
        });
    }

    静态最终的String []数据=新的String [] {姓名,姓氏,地址,城};


}
 

解决方案

取颜色一些数组,然后在getView方法设置视图颜色的POS机。例如:

 私人INT []颜色=新INT [] {0xfffff000,为0xffff0000,......};
 

getView()这样写:

  view.setBackgroundColor(颜色[POS]);
 

在这里pos是在颜色的索引值[]

I have inherited ListActivity and I need to change in code background color of some rows. How to do that, for example first row to set YELLOW background ?

public class AlarmsActivity extends ListActivity {
    private Activity currentActivity;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setListAdapter(new ArrayAdapter<String>(this, R.layout.alarms, DATA));

        currentActivity = this;

        ListView lv = getListView();
        lv.setTextFilterEnabled(true);

        lv.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                Toast.makeText(getApplicationContext(),
                        ((TextView) arg1).getText(), Toast.LENGTH_SHORT).show();


            }
        });
    }

    static final String[] DATA = new String[] {"Name","Last Name","Address","City" };


}

解决方案

Take some array of colors and then in getView method set the view color to the pos. for example :

private int[] colors=new int[]{0xfffff000, 0xffff0000, .....};

in getView() write this:

view.setBackgroundColor(colors[pos]);

here pos is the index value in the colors[]

这篇关于如何改变一个项目的背景下,从列表视图中的code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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