简体   繁体   中英

android - programatically change listview background color

I created a custom listview and and created an adapter for it. I'd like to be able to programatically change different style aspects of different views within my custom listview. My custom listview is a RelativeLayout that has TextViews inside of it. i tried the following with no luck. No errors or anything.

View myView = adapter.getView(position, null, null);

myView.setBackgroundColor(Color.parseColor("#93E6CD"));       
myView.findViewById(R.id.layout).setBackgroundColor(Color.parseColor("#93E6CD"));
adapter.notifyDataSetChanged();

here is my custom listview

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dip"
    android:id="@+id/layout">


    <!-- Title-->
    <TextView
        android:id="@+id/primaryTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Loading..."
        android:textColor="#040404"
        android:typeface="sans"
        android:textSize="18dip"
        android:textStyle="bold"/>

    <!-- Secondary title -->
    <TextView
        android:id="@+id/secondaryTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/primaryTitle"
        android:textColor="#343434"
        android:textSize="12dip"
        android:layout_marginTop="1dip"
        android:text="" />

    <!-- Rightend Data -->
    <TextView
        android:id="@+id/rightData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@id/primaryTitle"
        android:gravity="right"
        android:text=""
        android:layout_marginRight="5dip"
        android:textSize="12dip"
        android:textColor="#B53021"
        android:textStyle="bold"/>

</RelativeLayout>

We won't be able to proceed until you provide more information.

I suspect you're not setting up your ListView correctly. Are you really trying to display a list of similar items, such as rows from a database? If so, I would like to see the definition of the ListView object, either in code or in XML. I'd also like to see the definition of adapter. That looks like you've defined a ListAdapter for the ListView, but I also want to know what is backing the ListAdapter.

The line

myView.findViewById(R.id.layout).setBackgroundColor()

looks suspicious. You seem to be looking for a child view of myView, but I don't see where you've defined this child view.

Usually, you define a ListView as part of your entire UI layout file, a separate item layout that defines one item in the ListView, and then adapt a data source to the ListView.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM