orafix
orafix

Reputation: 11

get position filtred item in ListView

How can i get a item position from filtred listview ? For example i have list view 10 items (Club 1.2.3...). After filter "4" i have Club 4 on my listview, int position return 0 but i need to return 3 (count from 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;

    }

EDIT:

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

}

Upvotes: 1

Views: 967

Answers (2)

orafix
orafix

Reputation: 11

I solved that in diffrent way. I used item at position, not position of item.

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

Upvotes: 0

Fernando Piedra
Fernando Piedra

Reputation: 11

I think that you need create your custom ArrayAdapter that extends of ArrayAdapter and implements Filterable then override the method getFilter with your own CustomFilter that extends of Filter.

You can preserve your original array, and use the normal array for filtering, so you can search for the value, I give you an example.

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
}

Override getFilter

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

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

}

Upvotes: 1

Related Questions