无法过滤片段中的listview

时间:2016-09-28 05:19:05

标签: android listview android-fragments android-adapter searchview

我正在开发一个只有一个活动和多个片段的项目。其中一个片段有一个使用自定义适配器和模型填充的lisView。

我使用了searchview(工具栏菜单)来获取查询并过滤listview中的内容。

由于某种原因,过滤器无效。

    public class CustomAdapater extends BaseAdapter implements Filterable {
private final String taglaunch = "TAGLAUNCH";
    ArrayList<SingleRow> list;
    Context context;
    String[] menuTitles;
    ArrayList<SingleRow>filteredData;
    Filter filter;



    CustomAdapater(Context context){
        this.context=context;
        list = new ArrayList<SingleRow>();
        Resources resources = context.getResources();
        menuTitles  = resources.getStringArray(R.array.Banks);


        int[] menuIcons = {R.mipmap.ic_allahabadbank_logo,R.mipmap.ic_andhrabank_logo,R.mipmap.ic_axisbank_logo,R.mipmap.ic_bankofbaroda_logo,
                R.mipmap.ic_bankofindia_logo,R.mipmap.ic_bankofmaharashtra_logo, R.mipmap.ic_bharatiyamahilabank_logo,R.mipmap.ic_canarabank_logo
        ,R.mipmap.ic_capitallocalareabank_logo,R.mipmap.ic_centralbankofindia_logo};

        for(int i = 0; i<menuIcons.length; i++){
            list.add(new SingleRow(menuTitles[i],menuIcons[i]));
        }

        filteredData = new ArrayList<SingleRow>();
        filteredData.addAll(list);

    }

    @Override
    public int getCount() {

        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.d(taglaunch,"Am i called post filter");

        View row = convertView;
        ViewHolder ViewHolder = null;
        if(row==null){
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.singlerow,parent,false);
            ViewHolder = new ViewHolder(row);
            row.setTag(ViewHolder);
        }

        else{
            ViewHolder = (ViewHolder) row.getTag();
        }

        SingleRow temp = list.get(position);

        ViewHolder.BankHeaderTitle.setText(temp.menu);
        ViewHolder.linkNotification.setText( context.getResources().getString(R.string.Click_here));
        ViewHolder.bankLogo.setImageResource(temp.menuIcons);

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


    class SingleRow{
        String menu;
        int menuIcons;

        SingleRow(String menu,int menuIcons){
            this.menu = menu;
            this.menuIcons= menuIcons;
        }
    }

    class ViewHolder{
        TextView BankHeaderTitle;
       TextView linkNotification;
        ImageView bankLogo;

            ViewHolder(View v){
                BankHeaderTitle = (TextView) v.findViewById(R.id.BankHeaderTitle);
                linkNotification = (TextView) v.findViewById(R.id.linkNotification);
                bankLogo = (ImageView) v.findViewById(R.id.bankLogo);
            }

    }


    public class MyFilter extends Filter{

            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                //creating filter results object which needs to returned.
                FilterResults results  = new FilterResults();
                ArrayList<SingleRow> filterResults = new ArrayList<SingleRow>();
                String searchResult;
                if(constraint!=null&&constraint.length()>0)
                    searchResult = constraint.toString().toLowerCase();

                else{
                    results.count = filteredData.size();
                    results.values= filteredData;
                    return results;
                }

                int count = filteredData.size();
                String filterableString;

                for (int i = 0; i < count; i++) {
                    filterableString = filteredData.get(i).menu;
                    if (filterableString.toLowerCase().contains(searchResult)) {
                        Log.d(taglaunch,"adding " +filterableString);
                        SingleRow singleRow = new SingleRow(filteredData.get(i).menu,filteredData.get(i).menuIcons);
                        filterResults.add(singleRow);
                    }
                }

                results.count = filterResults.size();
                results.values= filterResults;
                return results;

            }
            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {

                if (results.count == 0)
                    notifyDataSetInvalidated();
                else {

                    list = (ArrayList<SingleRow>) results.values;
                    Log.d(taglaunch, "Publishing " + list.get(0).menu);

                    new CustomAdapater(context).notifyDataSetChanged();
                }
            }



        }
    }

我的片段是

public class Fd_Fragment extends Fragment implements SearchView.OnQueryTextListener {
    private final String taglaunch = "TAGLAUNCH";
    ListView listview;
    SearchView searchView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
        //  setRetainInstance(true); //Will ignore onDestroy Method (Nested Fragments no need this if parent have it)
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_search) {
            Log.d(taglaunch, "Search Icon Clicked from fragment");

            return false;

        }
        return super.onOptionsItemSelected(item);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Log.d(taglaunch, "FD Fragment Created");

        return inflater.inflate(R.layout.fd_fragment_layout, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {

        super.onViewCreated(view, savedInstanceState);

        listview = (ListView) getActivity().findViewById(R.id.banksListView);
        listview.setOnItemClickListener(new SettingListeners());
        listview.setAdapter(new CustomAdapater(getActivity()));
        if (savedInstanceState != null) {
            ((Appdrawer) getActivity()).setNavIcon();
        }


    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
        searchView.setOnQueryTextListener(this);
    }

    public void onResume() {
        super.onResume();
        String temp;
        String interestType =  getArguments().getString(((Appdrawer)getActivity()).interestType);
        if(interestType.equals(((Appdrawer) getActivity()).fd)) {
           temp = getString(R.string.fdLabel);
        }
        else{
            temp = getString(R.string.rdLabel);
        }
        ((Appdrawer) getActivity())
                .setActionBarTitle(temp);

        Toolbar mToolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Appdrawer) getActivity()).onSupportNavigateUp();
            }
        });

    }

    public class SettingListeners implements AdapterView.OnItemClickListener{

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Fragment fragment = new WebViewClass();
            String selected = ((TextView) view.findViewById(R.id.BankHeaderTitle)).getText().toString();
            Log.d(taglaunch,"List item clicked");
            Bundle args = new Bundle();
            args.putString(((Appdrawer) getActivity()).BankName, selected);
            args.putString(((Appdrawer) getActivity()).interestType, getArguments().getString(((Appdrawer) getActivity()).interestType));
            fragment.setArguments(args);
            Log.d(taglaunch,"clicked item"+selected);
            getFragmentManager().beginTransaction().replace(R.id.app_bar, fragment).addToBackStack(null).commit();

        }
    }



        @Override
        public boolean onQueryTextSubmit(String query) {

            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {

            CustomAdapater adapter = new CustomAdapater(getActivity());
            if (adapter != null && newText != null) {
                adapter.getFilter().filter(newText);

                return true;
            }

            return false;


        }


}

1 个答案:

答案 0 :(得分:1)

publishResults函数调用notifyDataSetChanged()中,无需创建新的适配器