P
P
Perkovec2014-12-28 19:56:56
Java
Perkovec, 2014-12-28 19:56:56

Changing the view of a custom item in a ListView?

I make custom items for the ListView in this way:

adapter = new ArrayAdapter<String>(this, R.layout.list_item, R.id.product_name, constants);
lv.setAdapter(adapter);

Here is the list_item markup:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/listitem"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#4d73ff"
    android:orientation="vertical" >
 
    <TextView
        android:id="@+id/product_name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:minHeight="48dp"
        android:padding="10dp"
        android:textColor="#fff"
        android:textSize="16sp" />
 
</LinearLayout>

But how can I change for example the background color of a custom item with code?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
one pavel, 2014-12-28
@onepavel

With such an adapter, you can programmatically change it only when you click on item.
Implement the getView(int position, View convertView, ViewGroup parent) method and you
have full control.

M
Max, 2015-12-29
@mbelskiy

Write your own implementation of the adapter, with getView overridden, as one pavel said , and you will be happy

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question