0

已编辑...

我正在尝试从搜索视图中过滤适配器。我知道我所拥有的很接近,但并不完全在那里。我收到此错误消息,代码如下。

日志猫

W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x40f0f2a0)
E/AndroidRuntime: FATAL EXCEPTION: main java.lang.NullPointerException at ---$FreeAgentPagerAdapter.getCount(---.java:403)
    at android.support.v4.view.ViewPager.dataSetChanged(ViewPager.java:813)
    at android.support.v4.view.ViewPager$PagerObserver.onChanged(ViewPager.java:2688)
    at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:37)
    at android.support.v4.view.PagerAdapter.notifyDataSetChanged(PagerAdapter.java:276)
    at ---$FreeAgentPagerAdapter$1.publishResults(---.java:434)

搜索视图

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.in_game, menu);

    MenuItem searchItem = menu.findItem(R.id.action_search);
    mSearchView = (SearchView) searchItem.getActionView();
    setupSearchView(searchItem);

    if (mState == 0) {
        menu.findItem(R.id.action_search).setVisible(false);
    }

    return true;
}

private void setupSearchView(MenuItem searchItem) {

    if (isAlwaysExpanded()) {
        mSearchView.setIconifiedByDefault(false);
    } else {
        searchItem.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
    }

    mSearchView.setOnQueryTextListener(this);
}

String newText = "";
public boolean onQueryTextChange(String newText) {
    Toast.makeText(getBaseContext(), "Query = " + newText, Toast.LENGTH_SHORT).show();
    return false;
}

public boolean onQueryTextSubmit(String query) {
    Adapter.getFilter().filter(newText.toString());
    return false;
}

protected boolean isAlwaysExpanded() {
    return false;
}

适配器

public class AdapterFragment extends Fragment {

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment, container, false);
        ViewPager = (ViewPager) rootView.findViewById(R.id.Pager);
        Adapter = new PagerAdapter(getChildFragmentManager());
        ViewPager.setAdapter(Adapter);
        return rootView;
    }
}

public class PagerAdapter extends FragmentStatePagerAdapter {

    private List TITLES = new ArrayList();

    private Map ReferenceMap;

    public PagerAdapter(FragmentManager fm) {
        super(fm);

        ReferenceMap = new HashMap();

        TEST.add("TESTING");
        for (Test test : test.getTest()) {
            TEST.add(test.getTest());
        }
    }

    @Override
    public CharSequence getTitle(int i) {
        return TEST.get(i);
    }

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

    @Override
    public Fragment getItem(int i) {
        Fragment Fragment = Fragment.newInstance(test.getId(), TEST.get(i));
        ReferenceMap.put(i, (Fragment) Fragment);
        return Fragment;
    }

    public FiltaddDropFragmentlter() {
        return new Filter() {
            @SuppressWarnings("unchecked")
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                Toast. makeText(getBaseContext(), "Publishing " + constraint, Toast.LENGTH_LONG).show();
                TEST = (List<String>) results.values;
                Adapter.notifyDataSetChanged();
            }

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                Toast.makeText(getBaseContext(), "Filtering " + constraint, Toast.LENGTH_LONG).show();
                List<String> filteredResults = getFilteredResults(constraint);

                FilterResults results = new FilterResults();
                results.values = filteredResults;

                return results;
            }
        };
    }

}

private List<String> getFilteredResults(CharSequence constraint) {
    return null;
}

谁能指出我做错了什么?我想我只需要第二双眼睛来看看这个。我应该做些什么不同的事情,我怎样才能以最好的方式做到这一点?

4

1 回答 1

1

三个步骤:

  1. 创建一个扩展的新类(或者可能是适配器中的静态内部类)Filter。实现performFiltering()方法。
  2. 实现publishResults()交换列表数据并调用notifyDataSetChanged()适配器的方法
  3. 让你的适配器实现Filterable接口并定义getFilter()方法。它应该返回您在步骤 1 中创建的新过滤器类的实例。

performFiltering将在后台线程上处理,并将publishResults从 UI 线程调用。

我正在寻找帮助我学习列表过滤的示例代码。如果我找到它,我会发布一个链接。

于 2013-07-05T18:42:01.427 回答