V
V
Vanes Ri_Lax2015-07-29 14:15:24
Java
Vanes Ri_Lax, 2015-07-29 14:15:24

How to change the background of the ListView element in my case?

Hello, please help me figure it out!
I did everything according to the example here
In general, I have a main activity in which there is a ListView list:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/racelist" />

</RelativeLayout>

For each list item I use item.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:id="@+id/index_item"
    android:shrinkColumns="*"
    android:stretchColumns="*"
    >


    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#bfbfbf"
        android:id="@+id/title_zag">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="МОСКВА (ВНУКОГО) - ОРЕНБУРГ"
            android:id="@+id/textView7"
            android:layout_span="3"
            android:layout_gravity="center_horizontal"
            android:textColor="#000000"
            android:textStyle="bold" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="план взлета"
            android:id="@+id/textView"
            android:textColor="#000000"
            android:textStyle="bold"
            android:layout_column="1" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="план посадки"
            android:id="@+id/textView2"
            android:textColor="#000000"
            android:textStyle="bold"
            android:layout_column="2" />

    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:id="@+id/race">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="1235"
            android:id="@+id/textView4"
            android:layout_column="0"
            android:theme="@style/TextColor"
            android:layout_marginLeft="10dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="30.07.15 09:00"
            android:id="@+id/textView5"
            android:layout_column="1"
            android:theme="@style/TextColor" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="29.07.15 15:00"
            android:id="@+id/textView6"
            android:layout_column="2"
            android:theme="@style/TextColor" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:text="приземлился"
            android:id="@+id/textView13"
            android:layout_column="0"
            android:theme="@style/TextColor"
            android:layout_marginLeft="10dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="факт взлета"
            android:id="@+id/textView8"
            android:layout_column="1"
            android:textColor="#000000"
            android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="факт посадки"
            android:id="@+id/textView9"
            android:layout_column="2"
            android:textColor="#000000"
            android:textStyle="bold" />
    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="30.07.15 09:15"
            android:id="@+id/textView10"
            android:layout_column="1"
            android:theme="@style/TextColor" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="30.07.15:00"
            android:id="@+id/textView11"
            android:layout_column="2"
            android:theme="@style/TextColor" />
    </TableRow>

</TableLayout>

Accordingly, I fill the list like this:
// имена атрибутов для Map
    final String ATTRIBUTE_NAME_UP = "up";
    //final String ATTRIBUTE_NAME_DOWN = "down";
    final String ATTRIBUTE_DATE_UP = "date_up";
    final String ATTRIBUTE_DATE_DOWN = "date_down";
    final String ATTRIBUTE_DATE_UP_FACT = "date_up_fact";
    final String ATTRIBUTE_DATE_DOWN_FACT = "date_DOWN_fact";
    final String ATTRIBUTE_RACE = "race";
    final String ATTRIBUTE_STATUS = "status";

 protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

// массивы данных
        String[] up = { "МОСКВА (ВНУКОГО)", "МОСКВА (ВНУКОГО)", "МОСКВА (ВНУКОГО)",
                "МОСКВА (ВНУКОГО)", "МОСКВА (ВНУКОГО)" };
        String[] down = {"ОРЕНБУРГ", "ОРЕНБУРГ", "ОРЕНБУРГ", "ОРЕНБУРГ", "ОРЕНБУРГ"};

        String[] date = {"28.07.15", "29.07.15", "31.07.15", "02.08.15", "05.08.15"};

        String[] date_up = {"09:00", "09:00", "09:00", "09:00", "09:00"};
        String[] date_down = {"12:00 +", "12:00" ,"12:00" ,"12:00" ,"12:00 +"};


        String[] date_up_fact = {"09:00", "09:00", "09:00", "09:00", "09:00"};
        String[] date_down_fact = {"12:00 +", "12:00" ,"12:00" ,"12:00" ,"12:00 +"};

        String[] race = {"5821", "5823", "5820", "5826", "5937"};
        String[] status = {"регистрация", "посадка", "задержка", "вылетел", "приземлился"};
 // упаковываем данные в понятную для адаптера структуру
        ArrayList<Map<String, Object>> data = new ArrayList<Map<String, Object>>(
                up.length);
        Map<String, Object> m;
        for (int i = 0; i < up.length; i++) {
            m = new HashMap<String, Object>();

            m.put(ATTRIBUTE_NAME_UP, up[i]+" - "+ down[i]+"  "+date[i]);
            m.put(ATTRIBUTE_DATE_UP, date_up[i]);
            m.put(ATTRIBUTE_DATE_DOWN, date_down[i]);

            m.put(ATTRIBUTE_DATE_UP_FACT, date_up_fact[i]);
            m.put(ATTRIBUTE_DATE_DOWN_FACT, date_down_fact[i]);

            m.put(ATTRIBUTE_RACE, race[i]);
            m.put(ATTRIBUTE_STATUS, status[i]);





            data.add(m);
        }

        // массив имен атрибутов, из которых будут читаться данные
        String[] from = {
                ATTRIBUTE_NAME_UP,
                ATTRIBUTE_DATE_UP, ATTRIBUTE_DATE_DOWN,
                ATTRIBUTE_DATE_UP_FACT, ATTRIBUTE_DATE_DOWN_FACT,
                ATTRIBUTE_RACE, ATTRIBUTE_STATUS,


        };
        // массив ID View-компонентов, в которые будут вставлять данные
        int[] to = { R.id.textView7,
                R.id.textView5, R.id.textView6,
                R.id.textView10, R.id.textView11,
                R.id.textView4, R.id.textView13

        };

        // создаем адаптер
        SimpleAdapter sAdapter = new SimpleAdapter(this, data, R.layout.item,
                from, to);

        // определяем список и присваиваем ему адаптер
        ListView lvSimple = (ListView) findViewById(R.id.racelist);
        lvSimple.setAdapter(sAdapter);
}

The list is filled, everything works, but in each item.xml there is a TableRow that has the id title_zag.
I need to somehow change its background depending on certain conditions. As a result, it should turn out like this:
snapshot.jpg
How can I implement this?
Thank you very much in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
one pavel, 2015-07-29
@vanesxl

Background color changing examples
stackoverflow.com/questions/7787503/android-listvi...
eureka.ykyuen.info/2010/03/15/android-%E2%80%93-ap...
amitandroid.blogspot.ru/2013 /03/android-listview-w...
Examples with headers
stackoverflow.com/questions/13590627/android-listv...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question