从操作栏过滤片段适配器

时间:2013-07-05 16:41:59

标签: java android filter adapter searchview

编辑...

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

logcat的

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;
}

有人能够指出我做错了什么吗?我想我只需要第二双眼睛来看一下。我应该采取哪些不同的方式,如何以最好的方式做到这一点?

1 个答案:

答案 0 :(得分:1)

三个步骤:

  1. 创建一个扩展Filter的新类(或者可能是适配器中的静态内部类)。实施performFiltering()方法。
  2. 实施publishResults()方法以交换列表数据并在适配器上调用notifyDataSetChanged()
  3. 使您的适配器实现Filterable接口并定义getFilter()方法。它应该返回您在步骤1中创建的新过滤器类的实例。
  4. performFiltering将在后台线程上处理,publishResults将从UI线程调用。

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