- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在使用架构组件 View 模型的 LiveData 来填充回收器 View ,并想添加一个搜索 View 过滤器,但找不到任何解决方案。我尝试在适配器中使用可过滤接口(interface),但它也不起作用,可能是因为 View 模型不允许它更改。
我的适配器是-
public class NetworkAdapter extends RecyclerView.Adapter<NetworkAdapter.NetworksViewHolder> implements Filterable {
// Member variable to handle item clicks
final private ItemClickListener mItemClickListener;
// Class variables for the List that holds task data and the Context
private List<NetworkEntry> mNetworkEntries;
private List<NetworkEntry> mFilteredNetworkEntries = new ArrayList<>();
private Context mContext;
/**
* Constructor for the TaskAdapter that initializes the Context.
*
* @param context the current Context
* @param listener the ItemClickListener
*/
public NetworkAdapter(Context context, ItemClickListener listener) {
mContext = context;
mItemClickListener = listener;
}
/**
* Called when ViewHolders are created to fill a RecyclerView.
*
* @return A new TaskViewHolder that holds the view for each task
*/
@Override
public NetworksViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// Inflate the task_layout to a view
View view = LayoutInflater.from(mContext)
.inflate(R.layout.network_list_item, parent, false);
return new NetworksViewHolder(view);
}
/**
* Called by the RecyclerView to display data at a specified position in the Cursor.
*
* @param holder The ViewHolder to bind Cursor data to
* @param position The position of the data in the Cursor
*/
@Override
public void onBindViewHolder(NetworksViewHolder holder, int position) {
// Determine the values of the wanted data
holder.onBindData(position);
}
/**
* Returns the number of items to display.
*/
@Override
public int getItemCount() {
if (mNetworkEntries == null) {
return 0;
}
return mNetworkEntries.size();
}
/**
* When data changes, this method updates the list of taskEntries
* and notifies the adapter to use the new values on it
*/
public void setNetworks(List<NetworkEntry> networkEntries) {
mNetworkEntries = networkEntries;
mFilteredNetworkEntries.addAll(mNetworkEntries);
notifyDataSetChanged();
}
public List<NetworkEntry> getNetworks() {
return mNetworkEntries;
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
if (charString.isEmpty()) {
mFilteredNetworkEntries = mNetworkEntries;
} else {
List<NetworkEntry> filteredList = new ArrayList<>();
for (NetworkEntry row : mNetworkEntries) {
// name match condition. this might differ depending on your requirement
// here we are looking for name or phone number match
if (row.getNetworkName().toLowerCase().contains(charString.toLowerCase())) {
filteredList.add(row);
}
}
mFilteredNetworkEntries = filteredList;
}
FilterResults filterResults = new FilterResults();
filterResults.values = mFilteredNetworkEntries;
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
mFilteredNetworkEntries = (ArrayList<NetworkEntry>) filterResults.values;
notifyDataSetChanged();
}
};
}
public interface ItemClickListener {
void onItemClickListener(NetworkData networkData);
}
public class NetworksViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView networkLogoImageView;
TextView networkNameTextView;
TextView joinTextView;
TextView commissionTextView;
TextView minPayTextView;
TextView payFrequencyTextView;
TextView offerTextView;
TextView likeTextView;
View containerView;
public NetworksViewHolder(View itemView) {
super(itemView);
networkLogoImageView = (ImageView) itemView.findViewById(R.id.iv_logo);
networkNameTextView = (TextView) itemView.findViewById(R.id.tv_network_name);
joinTextView = (TextView) itemView.findViewById(R.id.bt_join);
commissionTextView = (TextView) itemView.findViewById(R.id.iv_commission);
minPayTextView = (TextView) itemView.findViewById(R.id.tv_min_pay);
payFrequencyTextView = (TextView) itemView.findViewById(R.id.tv_pay_frequency);
offerTextView = (TextView) itemView.findViewById(R.id.iv_offers);
likeTextView = (TextView) itemView.findViewById(R.id.iv_likes);
containerView = itemView.findViewById(R.id.layoutContainer);
joinTextView.setOnClickListener(this);
containerView.setOnClickListener(this);
}
public void onBindData(int position) {
NetworkEntry networkEntry = mNetworkEntries.get(getAdapterPosition());
Picasso.with(mContext).load(networkEntry.getNetworkImageUrl()).into(networkLogoImageView);
networkNameTextView.setText(networkEntry.getNetworkName());
commissionTextView.setText(networkEntry.getNetworkCommission());
minPayTextView.setText(mContext.getString(R.string.tv_min_pay, networkEntry.getNetworkMinPay()));
payFrequencyTextView.setText(networkEntry.getNetworkPayFrequency());
offerTextView.setText(networkEntry.getNetworkOffers());
}
@Override
public void onClick(View view) {
int adapterPosition = getAdapterPosition();
NetworkEntry networkEntry = mNetworkEntries.get(adapterPosition);
if (view.getId() == R.id.bt_join) {
AppUtilities.openWebPage(mContext, networkEntry.getNetworkJoinUrl());
} else {
NetworkData networkData = new NetworkData(networkEntry);
Log.v(NetworkAdapter.class.getSimpleName(), "Network SNO : " + networkData.getSno());
mItemClickListener.onItemClickListener(networkData);
}
}
}
搜索 View OnQueryTextListener 为 -
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.search_menu, menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) searchItem.getActionView();
searchView.setOnQueryTextListener(this);
searchView.setQueryHint("Search");
super.onCreateOptionsMenu(menu, inflater);
}
@Override
public boolean onQueryTextSubmit(String query) {
adapter.getFilter().filter(query);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
adapter.getFilter().filter(newText);
return false;
}
最佳答案
出现问题是因为您应该在适配器中使用 FilteredList,而不是原始列表:
public class NetworkAdapter extends RecyclerView.Adapter<NetworkAdapter.NetworksViewHolder> implements Filterable {
// Member variable to handle item clicks
final private ItemClickListener mItemClickListener;
// Class variables for the List that holds task data and the Context
private List<NetworkEntry> mNetworkEntries;
private List<NetworkEntry> mFilteredNetworkEntries = new ArrayList<>();
private Context mContext;
/**
* Constructor for the TaskAdapter that initializes the Context.
*
* @param context the current Context
* @param listener the ItemClickListener
*/
public NetworkAdapter(Context context, ItemClickListener listener) {
mContext = context;
mItemClickListener = listener;
}
/**
* Called when ViewHolders are created to fill a RecyclerView.
*
* @return A new TaskViewHolder that holds the view for each task
*/
@Override
public NetworksViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// Inflate the task_layout to a view
View view = LayoutInflater.from(mContext)
.inflate(R.layout.network_list_item, parent, false);
return new NetworksViewHolder(view);
}
/**
* Called by the RecyclerView to display data at a specified position in the Cursor.
*
* @param holder The ViewHolder to bind Cursor data to
* @param position The position of the data in the Cursor
*/
@Override
public void onBindViewHolder(NetworksViewHolder holder, int position) {
// Determine the values of the wanted data
holder.onBindData(position);
}
/**
* Returns the number of items to display.
*/
@Override
public int getItemCount() {
if (mFilteredNetworkEntries == null) {
return 0;
}
return mFilteredNetworkEntries.size();
}
/**
* When data changes, this method updates the list of taskEntries
* and notifies the adapter to use the new values on it
*/
public void setNetworks(List<NetworkEntry> networkEntries) {
mNetworkEntries = networkEntries;
mFilteredNetworkEntries.addAll(mNetworkEntries);
notifyDataSetChanged();
}
public List<NetworkEntry> getNetworks() {
return mFilteredNetworkEntries;
}
@Override
public Filter getFilter() {
return new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
String charString = charSequence.toString();
List<NetworkEntry> filteredList = new ArrayList<>();
if (charString.isEmpty()) {
filteredList = mNetworkEntries;
} else {
for (NetworkEntry row : mNetworkEntries) {
// name match condition. this might differ depending on your requirement
// here we are looking for name or phone number match
if (row.getNetworkName().toLowerCase().contains(charString.toLowerCase())) {
filteredList.add(row);
}
}
}
FilterResults filterResults = new FilterResults();
filterResults.values = filteredList;
return filterResults;
}
@Override
protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
mFilteredNetworkEntries = (ArrayList<NetworkEntry>) filterResults.values;
notifyDataSetChanged();
}
};
}
public interface ItemClickListener {
void onItemClickListener(NetworkData networkData);
}
public class NetworksViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ImageView networkLogoImageView;
TextView networkNameTextView;
TextView joinTextView;
TextView commissionTextView;
TextView minPayTextView;
TextView payFrequencyTextView;
TextView offerTextView;
TextView likeTextView;
View containerView;
public NetworksViewHolder(View itemView) {
super(itemView);
networkLogoImageView = (ImageView) itemView.findViewById(R.id.iv_logo);
networkNameTextView = (TextView) itemView.findViewById(R.id.tv_network_name);
joinTextView = (TextView) itemView.findViewById(R.id.bt_join);
commissionTextView = (TextView) itemView.findViewById(R.id.iv_commission);
minPayTextView = (TextView) itemView.findViewById(R.id.tv_min_pay);
payFrequencyTextView = (TextView) itemView.findViewById(R.id.tv_pay_frequency);
offerTextView = (TextView) itemView.findViewById(R.id.iv_offers);
likeTextView = (TextView) itemView.findViewById(R.id.iv_likes);
containerView = itemView.findViewById(R.id.layoutContainer);
joinTextView.setOnClickListener(this);
containerView.setOnClickListener(this);
}
public void onBindData(int position) {
NetworkEntry networkEntry = mFilteredNetworkEntries.get(getAdapterPosition());
Picasso.with(mContext).load(networkEntry.getNetworkImageUrl()).into(networkLogoImageView);
networkNameTextView.setText(networkEntry.getNetworkName());
commissionTextView.setText(networkEntry.getNetworkCommission());
minPayTextView.setText(mContext.getString(R.string.tv_min_pay, networkEntry.getNetworkMinPay()));
payFrequencyTextView.setText(networkEntry.getNetworkPayFrequency());
offerTextView.setText(networkEntry.getNetworkOffers());
}
@Override
public void onClick(View view) {
int adapterPosition = getAdapterPosition();
NetworkEntry networkEntry = mFilteredNetworkEntries.get(adapterPosition);
if (view.getId() == R.id.bt_join) {
AppUtilities.openWebPage(mContext, networkEntry.getNetworkJoinUrl());
} else {
NetworkData networkData = new NetworkData(networkEntry);
Log.v(NetworkAdapter.class.getSimpleName(), "Network SNO : " + networkData.getSno());
mItemClickListener.onItemClickListener(networkData);
}
}
}
}
想法是在您的适配器中使用过滤列表,原始列表在适配器中仅用于通过 performFiltering
方法进行过滤的目的。
关于android - 过滤由 View 模型的 LiveData 条目填充的回收器 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50638839/
我正在试用 Realm 以及包括 LiveData 在内的 Android 架构组件。 我一直在关注 Google 的应用程序架构指南: https://developer.android.com/t
我有两个 DAO、两个存储库和两个 POJO。有什么方法可以创建两个的一个 Livedata?我需要它来为 Recyclerview 制作单个列表。POJO 是类似的对象。 费用库: public c
使用PagedList,这里没有数据库备份,而是内存中的数据列表(称其为CachedDataList),可以通过fetchMore()填充功能。 有了 PositionalDataSource、Dat
我正在创建具有MVVM架构的应用程序,但遇到了一个问题:获取要在View中显示的LiveData列表。 在我的ViewModel中,我有一个getAll()函数,该函数使用Room从数据库中检索字符串
我有一个包含用户列表的 LiveData 对象,我正在尝试将数据传输到另一个 LiveData 对象以在其他地方使用。 我在 Room 中使用 MVVM,所以我从数据库和 ViewModel 中获取
我是测试新手,我想学习如何使用 MVVM 模式测试协程。我刚刚关注了 https://github.com/android/architecture-samples 项目并做了一些更改(删除了远程源)
我在存储库类中有一个方法如下,它从本地数据库或网络返回国家列表作为 LiveData: fun loadCountries(): LiveData>> { return object : Ne
official Android developer docs 中的协程 LiveData 示例使用 emit() 给出以下示例: val user: LiveData = liveData {
在文字游戏应用中,我在 Activity 和 fragment 之间共享一个模型: public class MainViewModel extends AndroidViewModel {
我正在尝试对一个 View 进行数据绑定(bind),该 View 应该显示通过 View 模型中的 LiveData 属性公开的数据,但我发现无法将 LiveData 内的对象绑定(bind)到 V
使用 lifecycle-viewmodel-ktx和 lifecycle-livedata-ktx并给出以下示例: ViewModel 实现: class AutocompletionViewMod
我正在尝试合并 Android Architecture GitHub example与数据绑定(bind)。为此,我想我必须在 UserViewModel 中添加一个从 LiveData> 到 Li
我需要将域对象映射到 UI 对象并使用实时分页列表显示。 我试图映射 LiveData>至 LiveData>和 map PositionalDataSource至 PositionalDataSou
假设我们有两个 LiveData 对象: LiveData> fooList; LiveData> barList; 并且可以通过某种方法(或构造函数)将 Foo 转换为 Bar 对象。将具有 Foo
我想使用 MutableLiveData 观察来自 ViewModel 的一些数据。问题是我使用子类和父类,并且与 LiveData 存在一些不兼容。我想在 Kotlin 中做的事情的一个例子: im
在 Java 中,我们访问 Livedata> 的第一个元素书籍使用 books[0] ,如何在 Kotlin 中做到这一点? 我已经尝试了一切。 //Java代码 books[0]; // Kotl
我阅读了许多关于 viewmodel 和 livedata 的教程,但我并没有在 mvvm 模式的 viewmodel 类中实际使用 livedata。提前致谢。 最佳答案 Android 开发人员通
我正在创建一个 TODO 列表应用程序,用户可以在其中添加要执行的任务。任务有几类。我有以下情况: viewModelMainActivity = new ViewModelProvi
我正在使用 Kotlin 和 Android 架构组件(LiveData 和 Room)制作一个 Android 应用。 我有一个显示用户的 Activity (存储在数据库中并使用带有 ViewMo
我在我的存储库中使用 AsyncTask,它用于设置存储库中的 LiveData。我如何使用转换从我的 ViewModel 观察这个 LiveData? 最佳答案 你可以忽略我的另一个答案。解决方案是
我是一名优秀的程序员,十分优秀!