scientiffic
scientiffic

Reputation: 9415

Android: Set Visibility of TextView in ListAdapter

I have a layout that contains two ListViews (see image below), which are both created using two SimpleAdapters; let's call the ListView on top the QLocations ListView and the one on bottom the AllLocations ListView.

I would like to hide all red TextView elements from my AllLocations ListView. I can't figure out how to isolate the TextViews only from the AllLocations List View and not from the QLocations ListView as well.

So, my question is, how do I hide all TextView elements from a particular ListView?

enter image description here

Here is my code:

Creating the ListViews

    // Getting qListView: List View for locations with Qs
    qLV = (ListView) activity.findViewById(R.id.qListView);

    // list adapter
    ListAdapter qAdapter = new SimpleAdapter(activity, qListItems,
            R.layout.google_places_item,
            new String[] { KEY_REFERENCE, KEY_NAME}, new int[] {
                    R.id.reference, R.id.name });

    // Adding data into listview
    qLV.setAdapter(qAdapter);

    // Getting allListView: List View for locations without Qs
    allLV = (ListView) activity.findViewById(R.id.allListView);

    // list adapter
    ListAdapter allAdapter = new SimpleAdapter(activity, placesListItems,
            R.layout.google_places_item,
            new String[] { KEY_REFERENCE, KEY_NAME}, new int[] {
                    R.id.reference, R.id.name });

    // Adding data into listview
    allLV.setAdapter(allAdapter);

google_places_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView android:id="@+id/reference"
        android:visibility="gone"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

    <!-- Row for each location -->
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/qLocationRow"
        android:orientation="horizontal"
        >

    <!-- Q Name -->
    <TextView android:id="@+id/name"
        style="@style/qName"
    />

    <!-- Q WaitTime -->
    <TextView android:id="@+id/qName.qWaitTime"
        style="@style/qName.qWaitTime"
        android:text="14"
        />

    </LinearLayout>
</LinearLayout>

Activity Layout

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     style="@style/fullWidth"> 

    <!--  Label for List View of QLocations -->

    <TextView
        android:id="@+id/qListLabel"
        style="@style/widthMargins.Label"
        android:text="@string/qListLabel" />

    <!-- Horizontal Rule -->

    <View
        android:id="@+id/horizontalRule1"
        style="@style/widthMargins.horizontalRule"
        android:layout_below="@+id/qListLabel"
         />

    <!-- Linear Layout containing progress bar + text description -->
    <LinearLayout
        android:layout_width="200dp"
        android:layout_height="60dp"
        android:id="@+id/lin_progressBar"
        android:visibility="visible"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/horizontalRule1"
        android:layout_marginTop="10dp"
        android:orientation="horizontal">

        <ProgressBar
            android:id="@+id/loadPlacesProgress"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <TextView 
            android:id="@+id/loadPlacesText"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginLeft="5dp"
            android:text="Loading..."       
            android:gravity="center_vertical"
            android:textSize="15sp"    
            />

        </LinearLayout>

    <!-- ListView for Q Locations -->    
        <ListView
            android:id="@+id/qListView"
            style="@style/widthMargins"
            android:layout_height="140dp"
            android:layout_below="@+id/horizontalRule1" />


    <!--  Label for List View of All Locations -->

    <TextView
        android:id="@+id/allListLabel"
        style="@style/widthMargins.Label"
        android:layout_below="@+id/qListView"
        android:text="@string/allListLabel" />

    <!-- Horizontal Rule -->

    <View
        android:id="@+id/horizontalRule2"
        style="@style/widthMargins.horizontalRule"
        android:layout_below="@+id/allListLabel"
         />

        <!-- Linear Layout containing progress bar + text description -->
        <LinearLayout
            android:layout_width="200dp"
            android:layout_height="60dp"
            android:id="@+id/lin_progressBar2"
            android:visibility="visible"
            android:layout_centerHorizontal="true"
            android:layout_below="@id/horizontalRule2"
            android:layout_marginTop="10dp"
            android:orientation="horizontal">

        <ProgressBar
            android:id="@+id/loadPlacesProgress2"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            />
        <TextView 
            android:id="@+id/loadPlacesText2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginLeft="5dp"
            android:text="Loading..."       
            android:gravity="center_vertical"
            android:textSize="15sp"    
            />

        </LinearLayout>

    <!-- ListView for All Locations -->    

        <ListView
            android:id="@+id/allListView"
            style="@style/widthMargins"
            android:layout_height="140dp"
            android:layout_below="@+id/horizontalRule2"
            />

    <!-- Show on Map button -->
    <Button android:id="@+id/btn_show_map"
        style="@style/fullWidth"
        android:layout_centerHorizontal="true"
        android:text="Map"
        android:padding="20dp"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="10dip"/>

</RelativeLayout>

Upvotes: 1

Views: 3816

Answers (2)

knennigtri
knennigtri

Reputation: 197

So after you set your Adapters, you should be able to run the code below to give the the desired affect. I'm not sure how efficient it is, but it should at least get the job done.

    int numChildren = allLV.getListView().getChildCount();
    for(int i = 0; i < numChildren; i++)
    {
        LinearLayout ll = (LinearLayout) allLV.getListView().getChildAt(i);
        TextView tv = (TextView) ll.findViewById(R.id.qName.qWaitTime);
        tv.setVisibility(View.INVISIBLE);
    }

Then when you want to show the red boxed text, use a similar method to set them to visible again.

Upvotes: 2

Nathan
Nathan

Reputation: 1767

First off know that there is not a 1 to 1 mapping between your listview items and the data. If you have many items the views get reused as you scoll through the list.

First you want to update the of all items in the adapter to have the correct values. Once you have updated the data. Then notify the adapter that its data has changed and it will rebuild itself adapter.notifyDataSetChanged(). Make rebuilding the list fast by caching data as appropriate.

You might read up on MVC (Model-View-Controller) to gain a better understanding of what is going on here.

Upvotes: 1

Related Questions