如何根据字符串值禁用列表视图项?
Posted
技术标签:
【中文标题】如何根据字符串值禁用列表视图项?【英文标题】:How to disable list view item based on string value? 【发布时间】:2020-09-21 17:32:34 【问题描述】:我正在创建一个应用程序,它从 firebase 数据库中提取数据并在自定义列表视图中将其呈现给用户。我想让它在列表项中,如果文本视图包含“不可用”一词,用户不应该无法选择该列表项。我怎样才能动态地实现这一点?
Java 代码:
@Override
public void onStart()
super.onStart();
Query query = FirebaseDatabase.getInstance().getReference().child("TimeSlots").orderByChild("id");
FirebaseListOptions<BookingTimes> options = new FirebaseListOptions.Builder<BookingTimes>()
.setLayout(R.layout.time_list_layout_adapter)
.setLifecycleOwner(getActivity())
.setQuery(query, BookingTimes.class)
.build();
adapter = new FirebaseListAdapter(options)
@Override
protected void populateView(View v, Object model, int position)
timeTextV = v.findViewById(R.id.timeTV);
availabilityTextv = v.findViewById(R.id.availabilityTV);
BookingTimes bookingTimes = (BookingTimes) model;
timeTextV.setText(bookingTimes.getTime());
availabilityTextv.setText(bookingTimes.getAvailability());
/*if (availabilityTextv.getText().toString().equals("Unavailable"))
availabilityTextv.setEnabled(false);
*/ //<==== doesn't work
@Override
public boolean areAllItemsEnabled()
return false;
@Override
public boolean isEnabled(int position)
/*if(position == 3)
return false;
//<== this works, but how do i do it dynamically based on value?*/
/*if(availabilityTextv.getText().toString().equals("Unavailable"))
getItem(position);
return false;
*/ //<== doesn't work
return super.isEnabled(position);
;
listView.setAdapter(adapter);
自定义列表视图:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:layout_margin="5dp"
app:cardCornerRadius="5dp"
app:cardElevation="10dp"
app:cardBackgroundColor="@color/transparent">
<LinearLayout
android:layout_
android:layout_
android:orientation="horizontal">
<TextView
android:id="@+id/timeTV"
android:layout_
android:layout_
android:layout_weight="1"
android:paddingStart="8dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:textSize="25sp"
tools:ignore="RtlSymmetry" />
<TextView
android:id="@+id/availabilityTV"
android:layout_
android:layout_
android:paddingTop="5dp"
android:paddingEnd="8dp"
android:paddingBottom="5dp"
android:textSize="25sp"
tools:ignore="RtlSymmetry" />
</LinearLayout>
</androidx.cardview.widget.CardView>
BookingTimes 类:
public class BookingTimes
private String time;
private String availability;
public BookingTimes()
public BookingTimes(String Time, String Availability)
this.time = Time;
this.availability = Availability;
public String getTime()
return time;
public void setTime(String Time)
this.time = Time;
public String getAvailability()
return availability;
public void setAvailability(String Availability)
this.availability = Availability;
数据库:
应用:
【问题讨论】:
在isEnabled->adapter.getRef(position)中这样做获取项目,然后获取值并进行比较。 请同时添加你的BookingTimes
类的内容。
添加了BookingTimes类的内容
【参考方案1】:
您必须直接在 isEnabled(int)
中使用项目可用性。
new FirebaseListAdapter<BookingTimes>(options)
...
@Override
public boolean isEnabled(int position)
return getItem(position).getAvailability().equals("Unavailable");
还请记住,FirebaseListAdapter
具有通用参数类型。还要确保不要像 timeTextV
或 availabilityTextv
这样的视图,最好使用视图持有者模式。
【讨论】:
以上是关于如何根据字符串值禁用列表视图项?的主要内容,如果未能解决你的问题,请参考以下文章
点击 Android/Java 列表视图项时,如何禁用单击声音?