为什么我不能把我的自定义列表视图项的背景动态? [英] Why can't I set my custom Listview item's background dynamically?

查看:87
本文介绍了为什么我不能把我的自定义列表视图项的背景动态?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义列表视图,我使用自定义listadapter显示该列表。在我的自定义listadapter我想设置每个项目动态视对象中的值的颜色。但是每当我试图做到这一点的物品褪色,而不是让他们被设置为颜色。我申请了几个款式的项目,但是当我删除其效果仍然无法正常工作。这是我的code键更改每个项目的背景颜色:

I have a custom listview and I am using a custom listadapter to display that list. In my custom listadapter I am trying to set the colour of each item dynamically depending on a value within the object. However whenever I try to do this the items become faded rather than getting the colour they were set to. I am applying a few styles to the project but when I remove their effect it still doesn't work. This is my code to change the background colour of each item:

    private class stationAdapter extends ArrayAdapter<Station>{
    private ArrayList<Station> stations;

    public stationAdapter(Context context, int textViewResourceId, ArrayList<Station> stations) {
        super(context, textViewResourceId, stations);
        this.stations = stations;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.row, null);
        }
        Station temp = stations.get(position);
        if (temp != null) {
            TextView stationName = (TextView) v.findViewById(R.id.stationname);
            TextView serviced = (TextView) v.findViewById(R.id.inservice);

            try{
                if(temp.getLine().equals("red")){
                    v.setBackgroundColor(R.color.red);

                }
                else{
                    v.setBackgroundColor(R.color.green);
                }
            }catch(Exception e){
                Log.d(TAG, "Null pointer");
            }
            if (stationName != null) {
                stationName.setText("Station: "+temp.getName());                            }
            if(serviced != null){
                serviced.setText("In Service: "+ temp.getInServive());
            }
        }
        return v;
    }

}

如果任何人都可以指出我在做什么错我真的AP preciate它。

If anyone could point out what I am doing wrong I would really appreciate it.

推荐答案

像达科提到的,你要设置颜色,但是使用资源ID来代替。随着中说,使用固体颜色列表项的背景是一大禁忌,你一定要使用一个选择:

Like Darko mentioned, you're trying to set a color but using a resource ID instead. With that said, using a solid color for a list item background is a big no-no, you definitely want to use a selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_focused="false" android:state_pressed="false" android:state_selected="false"
        android:state_checked="false" android:state_enabled="true" android:state_checkable="false"
        android:drawable="@color/red" />
</selector>

假如把它放在一个 list_red_background.xml 您可绘制文件夹,使用 setBackgroundResource()代替。

Put that in a list_red_background.xml in your drawables folder and use setBackgroundResource() instead.

这篇关于为什么我不能把我的自定义列表视图项的背景动态?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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