gpt4 book ai didi

Android:在 ListAdapter 中设置 TextView 的可见性

转载 作者:行者123 更新时间:2023-11-29 18:03:32 24 4
gpt4 key购买 nike

我有一个包含两个 ListView 的布局(见下图),它们都是使用两个 SimpleAdapter 创建的;让我们将顶部的 ListView 称为 QLocations ListView,将底部的 ListView 称为 AllLocations ListView。

我想从我的 AllLocations ListView 中隐藏所有红色 TextView 元素。我不知道如何将 TextView 仅与 AllLocations ListView 隔离,而不是与 QLocations ListView 隔离。

所以,我的问题是,如何隐藏特定 ListView 中的所有 TextView 元素?

enter image description here

这是我的代码:

创建 ListView

    // 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 布局

<?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>

最佳答案

因此,在您设置适配器后,您应该能够运行下面的代码以产生所需的效果。我不确定它的效率如何,但它至少应该能完成工作。

    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);
}

然后当你想显示红框文字时,使用类似的方法将它们重新设置为可见。

关于Android:在 ListAdapter 中设置 TextView 的可见性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14553312/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com