在ListView中获取位置过滤项

时间:2016-04-07 19:42:40

标签: android listview filter position

如何从filtred 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;
        }
    });

}

2 个答案:

答案 0 :(得分:0)

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

您可以保留原始数组,并使用普通数组进行过滤,以便搜索该值,我举一个例子。

ArrayAdapter

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 

CustomFilter

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

}

答案 1 :(得分:0)

我以不同的方式解决了这个问题。我在位置使用了物品,而不是物品的位置。

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

    FragmentManager fragmentManager = getFragmentManager();

    Object item = adapter.getItem(position);

    if(item.equals(datasource[0])) fragmentManager.beginTransaction().replace(R.id.content_frame, new Club1Fragment()).commit();
    if(item.equals(datasource[1])) fragmentManager.beginTransaction().replace(R.id.content_frame, new Club2Fragment()).commit();

    Toast.makeText(getActivity(),item.toString(), Toast.LENGTH_SHORT).show();
}