gpt4 book ai didi

android - 在ListView中获取位置过滤项

转载 作者:行者123 更新时间:2023-11-30 01:25:25 25 4
gpt4 key购买 nike

如何从筛选 ListView 中获取项目位置?例如,我有 ListView 10 个项目(俱乐部 1.2.3 ...)。在过滤器“4”之后我的 ListView 中有 Club 4,int position 返回 0 但我需要返回 3(从 0 开始计数)

public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);

FragmentManager fragmentManager = getFragmentManager();

// dont't work
int pos = (Integer) getListAdapter().getItem(position);

switch (pos){
case 0:
fragmentManager.beginTransaction().replace(R.id.content_frame, new Club0Fragment()).commit();
break;
case 1:
fragmentManager.beginTransaction().replace(R.id.content_frame, new Club1Fragment()).commit();
break;

}

编辑:

public class NavigationSecondFragment extends ListFragment {


public NavigationSecondFragment() {
// Required empty public constructor
}
ArrayAdapter adapter;
String[] datasource = {"Club 1","Club 2","Club 3","Club 4","Club 5","Club 6","Club 7","Club 8","Club 9","Club 10","Club 11","Club 12", "Club 13"};


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_navigation_second, container, false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
adapter = new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1,datasource);
setListAdapter(adapter);
}


@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.menu_search,menu);
MenuItem searchItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView)MenuItemCompat.getActionView(searchItem);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
if (adapter != null) {
adapter.getFilter().filter(newText);
}

return true;
}
});

}

最佳答案

我认为您需要创建自己的 ArrayAdapter 扩展 ArrayAdapter 并实现 Filterable,然后使用您自己的扩展 Filter 的 CustomFilter 覆盖方法 getFilter。

你可以保留你原来的数组,使用普通数组进行过滤,这样你就可以搜索值了,我给你举个例子。

数组适配器

public PickerAdapter(Context context, ArrayList<String>) {
super(context, R.layout.picker_detail_row, alItemsPicker);
this.alItemsPicker = alItemsPicker; //This array is variable, and modified by CustomFilter
this.alItemsPickerInicial = alItemsPicker; //This array is final, and its value is set in your constructor
}

覆盖 getFilter

@Override
public Filter getFilter() {
if (filter == null) {
filter = new PickerAdapterFilter();
}
return filter;
}

在你的 onListItemClick 中

public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);

int realposition = alItemsPickerInicial.indexOf(alItemsPicker.get(position))

....your custom code

自定义过滤器

    private class PickerAdapterFilter extends Filter {

@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults resultados = new FilterResults();
if (constraint==null||constraint.length()==0) {
resultados.count = alItemsPickerInicial.size();
resultados.values = alItemsPickerInicial;
} else {
ArrayList<String> tmpAlItemsPicker = new ArrayList<String>();
for (String sItemPicker : alItemsPickerInicial) {
if (sItemPicker.contains(constraint)) {
tmpAlItemsPicker.add(htItemPicker);
}
}
resultados.count = tmpAlItemsPicker.size();
resultados.values = tmpAlItemsPicker;
}
return resultados;
}

@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
alItemsPicker = (ArrayList<String>)results.values;
if (results.count==0) {
notifyDataSetInvalidated();
} else {
notifyDataSetChanged();
}
}

}

关于android - 在ListView中获取位置过滤项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36485725/

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