gpt4 book ai didi

java - 过滤器列表在空结果时不会改变

转载 作者:行者123 更新时间:2023-12-02 04:34:44 26 4
gpt4 key购买 nike

每当过滤 ListView 后没有结果时,我希望它消失并且 TextView 出现在屏幕中央。我知道这一切都需要在 if (results.count == 0) {} 内进行,但我遇到了一些与布局相关的错误,我不知道如何解决。这 '?'应该表示 ListView 的 fragment 布局,但由于代码不在布局类中,我不知道需要用什么来替换它(这同样适用于 ( this)TextView tv = new TextView(this); 中。所有相关帮助将不胜感激。

我想要实现的目标如下:

if (results.count == 0) {

1. Make the list invisible
2. Create a text view programmatically and show it

}

ItemListAdapter 类

public class ItemListAdapter extends BaseAdapter implements Filterable {

private List<Victoria> mData;
private List<Victoria> mFilteredData;
private LayoutInflater mInflater;
private ItemFilter mFilter;
private ListView mListView;

public ItemListAdapter (List<Victoria> data, Context context) {
mData = data;
mFilteredData = data;
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
return mFilteredData.size();
}

@Override
public String getItem(int position) {
return mFilteredData.get(position).getItem();
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_row, parent, false);
holder = new ViewHolder();

holder.title = (TextView) convertView.findViewById(R.id.item_title);
holder.description = (TextView) convertView.findViewById(R.id.item_description);

convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

holder.title.setText(mFilteredData.get(position).getItem());
holder.description.setText(mFilteredData.get(position).getItemDescription());

return convertView;
}

@Override
public Filter getFilter() {
if (mFilter == null) {
mFilter = new ItemFilter();
}
return mFilter;
}

/**
* View holder
*/
static class ViewHolder {
private TextView title;
private TextView description;
}

/**
* Filter for filtering list items
*/
private class ItemFilter extends Filter {

/**
* Invoked on a background thread. This is where all the filter logic should go
* @param constraint the constraint to filter on
* @return the resulting list after applying the constraint
*/
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();

if (TextUtils.isEmpty(constraint)) {
results.count = mData.size();
results.values = mData;
} else {
//Create a new list to filter on
List<Victoria> resultList = new ArrayList<Victoria>();
for (Victoria str : mData) {
if (str.getItemDescription().toLowerCase().contains(constraint.toString().toLowerCase())) {
resultList.add(str);
}
}
results.count = resultList.size();
results.values = resultList;
}
return results;
}

/**
* Runs on ui thread
* @param constraint the constraint used for the result
* @param results the results to display
*/
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results.count == 0) {
//Make list invisible
//Make text view visible
notifyDataSetInvalidated();
} else {
mFilteredData = (ArrayList<Victoria>)results.values;
notifyDataSetChanged();
}
}
}
}

FilterListFragment 类

public class FilterListFragment extends ListFragment implements SearchView.OnQueryTextListener {



private ItemListAdapter mAdapter;

public FilterListFragment() {
//Required empty constructor
}

public static FilterListFragment newInstance() {
return new FilterListFragment();
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_filter_string_list, container, false);
//Tell the system to call onCreateOptionsMenu
setHasOptionsMenu(true);
initialize(view);
return view;
}

List<Victoria> list = new ArrayList<Victoria>();
private void initialize(View view) {
String[] items = getActivity().getResources().getStringArray(R.array.items);
String[] itemDescriptions = getActivity().getResources().getStringArray(R.array.itemDescriptions);
for (int n = 0; n < items.length; n++){
Victoria victoria = new Victoria();
victoria.setID();
victoria.setItem(items[n]);
victoria.setItemDescription(itemDescriptions[n]);
list.add(victoria);
}

mAdapter = new ItemListAdapter(list, getActivity());
setListAdapter(mAdapter);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Set up search view
inflater.inflate(R.menu.menu_search, menu);
MenuItem item = menu.findItem(R.id.search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);
searchView.setOnQueryTextListener(this);
searchView.setQueryHint(getResources().getString(R.string.search_query));
}

@Override
public boolean onQueryTextSubmit(String newText) {
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
mAdapter.getFilter().filter(newText);
return false;
}
}

enter image description here

最佳答案

您不必处理它,Android 会为您处理。在 fragment_filter_string_list.xml 中添加代表空 View 的 TextView,其 id 为 android:id="@android:id/empty" 。 Android 将根据 getCount() 的值来处理显示/隐藏它。

编辑

要实现您想要的目标,您的 publishResults 应该如下所示

mFilteredData = (ArrayList<Victoria>)results.values;
notifyDataSetChanged();

您可能还想保留 mFilteredData 的副本,以便在查询更改或重置时恢复数据集

关于java - 过滤器列表在空结果时不会改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30989145/

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