简体   繁体   中英

Android: ListView background color anomaly

I have a problem on a ListView, practically the background color of the rows changes at random without being set by code.. I tried to invalidate the ListView, change visibility, remove all the children and recreate it, but nothing.

This is a screen of right color: right color

And this the wrong one: wrong color

The colors of the rows are set within the method getView according to their position, the blue color is given to specific rows and the same code that sets this color also changes the picture on the right.

I tried to debug the code and there are no errors in the assignment of color..

The piece of code that assigns the background color:

View v = _vi.inflate(R.layout.row_prodotto_generale, parent, false);

LinearLayout la = (LinearLayout) v.findViewById(R.id.linearLayoutProdottoGenerale);
la.setBackgroundResource( ((position % 2) == 0) ? R.color.color_row_odd : R.color.color_row_even  );

final Prodotti c = (Prodotti)_data.get( position ); // Item in the row
if( c != null ) {
    // Set text label
    // Set text color to black
    // Set the plus image on the right

    if(Float.compare(c.getProdottoQta(), +0.0f) > 0) {
        // If the item has a specific field bigger than 0
        la.setBackgroundColor(getResources().getColor(R.color.toolbar_blue));
        // Set text color to white
        // Set the other image on the right
    }
}

This is the layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayoutProdottoGenerale"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:layout_gravity="right"
    android:orientation="horizontal" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="center_vertical"
        android:layout_marginBottom="2dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="2dp"
        android:gravity="center_vertical"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/rowProdottoGeneraleNome"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:lines="1"
            android:maxLines="1"
            android:maxWidth="180dp"
            android:scrollHorizontally="true"
            android:text="@string/_nome_"
            android:textColor="@color/color_black"
            android:textSize="16sp"
            android:textStyle="bold" >
        </TextView>

        <TextView
            android:id="@+id/rowProdottoGeneraleMarca"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:lines="1"
            android:maxLines="1"
            android:maxWidth="180dp"
            android:scrollHorizontally="true"
            android:text="@string/_marca_"
            android:textColor="@color/color_dark_gray"
            android:textSize="14sp" >
        </TextView>
    </LinearLayout>

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent" >

        <TextView
            android:layout_width="1dp"
            android:layout_height="match_parent"
            android:layout_toLeftOf="@+id/addProdotto"
            android:background="@color/color_white" />

        <ImageView
            android:id="@id/addProdotto"
            android:layout_width="48dp"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:contentDescription="@string/plus"
            android:src="@drawable/piu_blu" >
        </ImageView>
    </RelativeLayout>

</LinearLayout>

Normally the blue background color is on the entire row, when the problem occurs only the background of the first child of "linearLayoutProdottoGenerale" change in blue.

The problem occurs randomly when i try to search an item, or when i click the image on the right of a row, or when i click on the top view the one above the search box (btw is a collapse view). Also the problem occurs only on my Galaxy Tab 2, i tried also some smartphone (Nexus S, Galaxy Ace) and on emulators but here all works good.

Add an else clause:

if (Float.compare(c.getProdottoQta(), +0.0f) > 0) {
    // If the item has a specific field bigger than 0
    la.setBackgroundColor(getResources().getColor(R.color.toolbar_blue));
    // Set text color to white
    // Set the other image on the right
}
else {
    la.setBackgroundResource( ((position % 2) == 0) ? R.color.color_row_odd : R.color.color_row_even  );
}

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