为什么我的列表视图没有更新?

时间:2016-03-25 07:53:15

标签: android listview

我遇到了我的过滤后的ListView无法更新的问题。

在我的应用程序中有一个自定义ListView,它有一个TextView和一个ImageView作为行元素。

过滤器工作正常但我的自定义ListView未在publishResults()中更新。

@Override
protected FilterResults performFiltering(CharSequence constraint)
{
    FilterResults results = new FilterResults();
    if (constraint == null || constraint.length() <= 0) {
        results.count = ListAdapter.this.mStringFilterList.size();
        results.values = ListAdapter.this.mStringFilterList;
    } else {
        ArrayList<Ezomart> filterList = new ArrayList();
        for (int i = 0; i < ListAdapter.this.mStringFilterList.size(); i++) {
            if (((Ezomart) ListAdapter.this.mStringFilterList.get(i)).getTitle().toUpperCase().contains(constraint.toString().toUpperCase())) {
                Ezomart country = new Ezomart();
                country.setTitle(((Ezomart)ListAdapter.this.mStringFilterList.get(i)).getTitle());
                Log.d("SRI",mStringFilterList.get(i).getTitle());
                country.setSubCategory(((Ezomart) ListAdapter.this.mStringFilterList.get(i)).getSubCategory());
                country.setArea(((Ezomart) ListAdapter.this.mStringFilterList.get(i)).getArea());
                country.setThumbnailUrl(((Ezomart) ListAdapter.this.mStringFilterList.get(i)).getThumbnailUrl());
                country.setNumber(((Ezomart) ListAdapter.this.mStringFilterList.get(i)).getNumber());
                filterList.add(country);
            }
        }
        results.count = filterList.size();
        results.values = filterList;
    }
    return results;
}

protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
    ListAdapter.this.ezoItems = (List<Ezomart>) filterResults.values;
    if (filterResults.count > 0) {
        notifyDataSetChanged();
    } else {
        notifyDataSetInvalidated();
    }
}

1 个答案:

答案 0 :(得分:-1)

I found the answer by following the link below. Thanks to MBH.

I had to add this code in my fragment.

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        // What i have added is this
        setHasOptionsMenu(true);
    }

For reference please follow the link:

Android search with Fragments