gpt4 book ai didi

java - 从操作栏中过滤 fragment 适配器

转载 作者:行者123 更新时间:2023-12-01 14:21:41 25 4
gpt4 key购买 nike

已编辑...

我正在尝试从搜索 View 中过滤适配器。我知道我所拥有的已经很接近了,但还不够。我收到此错误消息,代码如下。

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)

搜索 View

@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. 创建一个扩展 Filter 的新类(或者适配器中的静态内部类) 。实现 performFiltering()方法。
  2. 实现publishResults()方法来交换列表数据并在适配器上调用 notifyDataSetChanged()
  3. 让您的适配器实现 Filterable 接口(interface)并定义 getFilter() 方法。它应该返回您在步骤 1 中创建的新过滤器类的实例。

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

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

关于java - 从操作栏中过滤 fragment 适配器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17493485/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com