Question

I'm trying to target an ImageView in a list item to set its background color but am having no luck.

This is the route I've gone so far which isn't doing anything. Should I be looking to code it in to my custom listAdapter?

listView.getChildAt(0).findViewById(R.id.leftBar).setAlpha(0.3f);

The data for the listItems is being populated from an Object, I'd rather not add the code in to the object since I'm thinking this is the wrong way to go about it (thinking MVC) and that's why I thought putting the code in my listAdapter might be the correct thing to do?

Was it helpful?

Solution

In my ListAdapter I used the following to target the ImageView:

    public View getView(int position, View convertView, ViewGroup parent) {LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View rowView = inflater.inflate(R.layout.list_item, parent, false);

View iv = rowView.findViewById(R.id.leftBar);


   if (position == ListView.currentStage){
        iv.setBackgroundColor(getContext().getResources().getColor(R.color.Green));
    } else if (position < ListView.currentStage){
        iv.setBackgroundColor(getContext().getResources().getColor(R.color.Red));
    }else if (position > ListView.currentStage) {
        iv.setBackgroundColor(getContext().getResources().getColor(R.color.Peach));
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top