每当过滤我的列表视图后没有结果,我希望它消失,文本视图出现在屏幕的中心。我知道这一切都需要进入if (results.count == 0) {}
但我得到一些与布局相关的错误,我不知道如何解决。 '?'应该表示列表视图片段的布局,但由于代码不在布局类中,我还没有得到什么需要替换它的线索(同样适用于{{1}在(this)
)。所有相关的帮助将不胜感激。
我想要实现的目标如下:
TextView tv = new TextView(this);
ItemListAdapter类
if (results.count == 0) {
1. Make the list invisible
2. Create a text view programmatically and show it
}
FilterListFragment类
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();
}
}
}
}
答案 0 :(得分:2)
你不必处理它,Android会为你做。在TextView
中添加代表空视图的fragment_filter_string_list.xml
,其ID为android:id="@android:id/empty"
。 Android将负责显示/隐藏它,具体取决于getCount()
的值。
修改,
要实现您的目标,您的publishResults
应该看起来像
mFilteredData = (ArrayList<Victoria>)results.values;
notifyDataSetChanged();
您也可能希望保留mFilteredData
的副本,以便在查询更改或重置时还原数据集