Android Change background from inside listview adapter

659 views Asked by At

I am trying to change the border around the listview item, unfortunately I am getting null reference errors when I try and reference the layout to change the border. layout.setBackgroundResource(R.drawable.border_purchased); causes the null reference. Any idea where I am missing a method/concept?

Part of adapter

    @Override
    public View getItemView(Item object, View v, ViewGroup parent) {
        if (v == null)
        {
            v = View.inflate(getContext(), R.layout.listview_itempage, null);
        }

        super.getItemView(object, v, parent);

        TextView itemTextView = (TextView) v.findViewById(R.id.textViewItemName);
        itemTextView.setText(object.getItemName());

        if(object.getPurchasedStatus() == true)
        {
            RelativeLayout layout = (RelativeLayout) v.findViewById(R.id.itemListBorder);

            layout.setBackgroundResource(R.drawable.border_purchased);
        }

        if(object.getRejectReason() != null)
        {
            RelativeLayout layout = (RelativeLayout) v.findViewById(R.id.itemListBorder);

            layout.setBackgroundResource(R.drawable.border_reject);
        }

        return v;
    }

Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    android:gravity="center_vertical">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="90dp"
        android:orientation="vertical"
        android:id="@+id/itemListBorder"
        android:background="@drawable/border_main"
        >

        <com.parse.ParseImageView
            android:id="@+id/imgStore"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"/>

        <TextView
            android:id="@+id/textViewStoreTitle"
            android:textSize="30dp"
            android:textColor="#FFFFFF"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:layout_alignParentRight="true"/>
        <TextView
            android:id="@+id/textViewStoreItemCount"
            android:textSize="15dp"
            android:textColor="#FFFFFF"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:text="0 items"
            android:layout_below="@id/textViewStoreTitle"/>
    </RelativeLayout>
</LinearLayout>
2

There are 2 answers

3
aschattney On

you don't pass the parent when inflating the view:

 if (v == null)
 {
   v = View.inflate(getContext(), R.layout.listview_itempage, null);
 }

 if (v == null)
 {
   v = View.inflate(getContext(), R.layout.listview_itempage, parent);
 }
2
Dacotah On

You mention that this is a listview adapter but I don't see a listview in your XML... In order to use the listview adapter, you must have a list with no id in your XML. This creates a list that can be referenced by android.R.id.list

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
</ListView>

From there you should just be able to say

v.setBackgroundResource(R.drawable.border_purchased);

to change the background drawable.

In the case that I am wrong and you don't actually need a listview for this situation, you should still try v.setBackgroundResource instead of referencing your layout.