开发者

Why can't I set my custom Listview item's background dynamically?

开发者 https://www.devze.com 2023-03-13 16:14 出处:网络
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 objec

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 e开发者_如何转开发xtends 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;
    }

}

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


You cant use setBackgroundColor and then reference a resource. if you want to use setBackgroundColor() you need to use the Color class like:

setBackgroundColor(Color.BLACK);

Instead if you want to set a resource (R.drawable, R.color etc...) you need to do it like

v.setBackgroundResource(R.color.black);

EDIT:

The cache color hint is what is needed if the items start becoming gray while scrolling the list. You need to set it to a transparent color if you are adding custom backgrounds to items and lists.


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>

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


Have you switched fading off?

http://developer.android.com/resources/articles/listview-backgrounds.html

Add this to the ListView layout file:

android:cacheColorHint="#00000000"
0

精彩评论

暂无评论...
验证码 换一张
取 消