我有一项搜索一组图片的活动。图像显示在图库中。有一个EditText,用户可以在其中输入搜索词。当用户在EditText中输入文本时,将根据输入的文本过滤库。
我做了SearchResultAdapter extends BaseAdapter implements Filterable
基本上没用。如果用户输入文本,则过滤适配器中的数据。我记录了结果:它到目前为止工作。然后我调用notifyAdapterSetChanged,以便库显示更新的数据集。实际上调用了notifyAdapterSetChanged,并且画廊在调用我的适配器getCount()时收到了请求(我记录了所有调用)。
将选择设置为0会导致应用程序由于IndexOutOfBoundsException而崩溃,因为我预期当库没有项目时。画廊仍然显示之前的所有图像。
问题是: 当数据集为空(即用户输入的搜索词不适合任何图像)时,图库不显示更新。该库接收notifyAdapterSetChanged,在项目计数(即0)上查询适配器并更新滚动限制。它不会更新库中显示的图像。我不能再滚动图像,因为基本上没有要显示的图像,但图像不会从图库中删除。它看起来好像画廊冻结了。
当删除无效搜索词(即数据集不为空)时,图库会显示正确的图像。此外,在某些情况下,图库会显示空列表,例如应用程序最小化然后重新打开时。我不能重现画廊清空的条件(除了最小化/重新打开应用程序):
我已尝试对图库进行无效和requestLayout(),但它没有帮助。有谁知道如何让画廊更新显示的图像列表?
此处用户输入与无图像匹配的文本时的日志消息:
[SearchResultAdapter.SearchFilter] Results: 0
[SearchResultAdapter.SearchFilter] called notifyDataSetChanged()
[SearchResultAdapter.getCount()] getCount called: dateset size 0
[SearchResultAdapter.getCount()] getCount called: dateset size 0
注意图库如何在数据集大小
上实际调用适配器活动中的初始化代码:
final Gallery gallery = (Gallery) findViewById(R.id.search_results);
final SearchResultAdapter adapter = new SearchResultAdapter(this, gallery);
gallery.setAdapter(adapter);
EditText searchField = (EditText) findViewById(R.id.search_searchfield);
searchField.addTextChangedListener(new TextWatcher()
{
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count)
{
// TODO unhardcode criterion
adapter.putFilterTerm(SearchCriteria.OFFERING, s.toString());
adapter.getFilter().filter(s);
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
// do nothing
}
@Override
public void afterTextChanged(Editable s)
{
// do nothing
}
});
SearchResultAdapter(无关紧要的行被遗漏):
public class SearchResultAdapter extends BaseAdapter implements Filterable
{
private Context context;
private Filter filter;
public void putFilterTerm(SearchCriteria key, String value)
{
filterTerms.put(key, value);
}
public boolean containsFilterTerm(SearchCriteria key)
{
return filterTerms.containsKey(key);
}
public String getFilterTerm(SearchCriteria key)
{
return filterTerms.get(key);
}
List<PageContainer> completeList = new ArrayList<PageContainer>();
List<PageContainer> currentList = new ArrayList<PageContainer>();
Map<SearchCriteria, String> filterTerms = new HashMap<SearchCriteria, String>();
View view;
public SearchResultAdapter(Context context, View gallery)
{
this.context = context;
this.view = gallery;
for (ContentPropertyContainer page : Model.getInstance()
.getRootContainer().getChildren())
{
completeList.add((PageContainer) page);
}
currentList.addAll(completeList);
}
@Override
public int getCount()
{
return currentList.size();
}
@Override
public Object getItem(int position)
{
return currentList.get(position);
}
@Override
public long getItemId(int position)
{
PageContainer item = currentList.get(position);
for (int i = 0; i < completeList.size(); ++i)
{
if (completeList.get(i) == item)
{
return i;
}
}
return -1;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
View listview = convertView;
// Code left out here tries to reuse the convertView
// like Romain Guy said in a Google I/O Talk
return listview;
}
private class ResultList
{
ResultList(List<PageContainer> results)
{
this.results = results;
}
List<PageContainer> results;
}
@Override
public Filter getFilter()
{
// implements Filterable
if (filter == null)
{
filter = new SearchFilter();
}
return filter;
}
private class SearchFilter extends Filter
{
SearchFilter()
{
// do nothing, needed for visibility
}
@Override
protected FilterResults performFiltering(CharSequence constraint)
{
FilterResults results = new FilterResults();
// Code left out here filters the list
return results;
}
@Override
protected void publishResults(CharSequence constraint,
FilterResults results)
{
if (results == null)
{
return;
}
if (results.values instanceof ResultList)
{
currentList = ((ResultList) (results.values)).results;
notifyDataSetChanged();
}
}
}
}
编辑: 将图库的可见性设置为不可见然后再显示可以解决问题。
view.post(new Runnable()
{
@Override
public void run()
{
view.setVisibility(View.INVISIBLE);
view.setVisibility(View.VISIBLE);
}
});
有更好的解决方案吗?这对我来说似乎很骇人听闻。