实现搜索里面的ListView片段 [英] Implement Search in ListView inside Fragment

查看:130
本文介绍了实现搜索里面的ListView片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我试图实现建成一个片段列表视图搜索功能
ListView控件工作正常,但是当我在的EditText键入要搜索消失

Hey guys i am trying to implement search functionality in a listview built into a fragment The listview is working fine but when i type on the edittext to search it disappears

我的code:

public class DrinksFragment extends Fragment {

    private View rootView;
    private ArrayAdapter<DrinksList> adapter;
    private List<DrinksList> drinks;
    private ListView lv;
    ArrayList<DrinksList> mAllData;
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.activity_drinks_fragment, container, false);
        populateDrinksList();
        doSearch();
        return rootView;
    }

    private void doSearch() {
        final EditText et = (EditText)rootView.findViewById(R.id.searchListDrinks);
        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String text = et.getText().toString().toLowerCase(Locale.getDefault());
            adapter.filter(text);
            }
        });
    }



    private void populateDrinksList() {
        drinks = new ArrayList<DrinksList>();
        drinks.add(new DrinksList(R.drawable.coca, "Coca Cola", 2.50));
        drinks.add(new DrinksList(R.drawable.cocalight, "Coca Cola Light", 2.50));
        drinks.add(new DrinksList(R.drawable.cocazero, "Coca Cola Zero", 2.50));
        drinks.add(new DrinksList(R.drawable.orange, "Fanta Orange", 2.50));
        drinks.add(new DrinksList(R.drawable.lemon, "Fanta Lemon", 2.50));
        drinks.add(new DrinksList(R.drawable.ble, "Fanta Blue", 2.50));
        drinks.add(new DrinksList(R.drawable.sprite, "Sprite", 2.50));
        drinks.add(new DrinksList(R.drawable.soda, "Soda Water", 2.50));
        drinks.add(new DrinksList(R.drawable.tonic, "Tonic Water", 2.50));
        drinks.add(new DrinksList(R.drawable.ioli, "Sparkling Water Ioli", 2.50));
        drinks.add(new DrinksList(R.drawable.perrier, "Sparkling Water Perrier", 2.50));
        drinks.add(new DrinksList(R.drawable.nero, "Still Water", 2.00));
        drinks.add(new DrinksList(R.drawable.redbull, "Red Bull", 4.00));
        drinks.add(new DrinksList(R.drawable.zelita, "Zelita", 2.50));
        lv = (ListView)rootView.findViewById(R.id.drinksListView);
        adapter = new MyCustomDrinksListAdapter(getActivity().getApplicationContext(), R.layout.list_item, drinks);
        lv.setAdapter(adapter);
    }

}public class DrinksFragment extends Fragment {

    private View rootView;
    private ArrayAdapter<DrinksList> adapter;
    private List<DrinksList> drinks;
    private ListView lv;
    ArrayList<DrinksList> mAllData;
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.activity_drinks_fragment, container, false);
        populateDrinksList();
        doSearch();
        return rootView;
    }

    private void doSearch() {
        final EditText et = (EditText)rootView.findViewById(R.id.searchListDrinks);
        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                DrinksFragment.this.adapter.getFilter().filter(s);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }



    private void populateDrinksList() {
        drinks = new ArrayList<DrinksList>();
        drinks.add(new DrinksList(R.drawable.coca, "Coca Cola", 2.50));
        drinks.add(new DrinksList(R.drawable.cocalight, "Coca Cola Light", 2.50));
        drinks.add(new DrinksList(R.drawable.cocazero, "Coca Cola Zero", 2.50));
        drinks.add(new DrinksList(R.drawable.orange, "Fanta Orange", 2.50));
        drinks.add(new DrinksList(R.drawable.lemon, "Fanta Lemon", 2.50));
        drinks.add(new DrinksList(R.drawable.ble, "Fanta Blue", 2.50));
        drinks.add(new DrinksList(R.drawable.sprite, "Sprite", 2.50));
        drinks.add(new DrinksList(R.drawable.soda, "Soda Water", 2.50));
        drinks.add(new DrinksList(R.drawable.tonic, "Tonic Water", 2.50));
        drinks.add(new DrinksList(R.drawable.ioli, "Sparkling Water Ioli", 2.50));
        drinks.add(new DrinksList(R.drawable.perrier, "Sparkling Water Perrier", 2.50));
        drinks.add(new DrinksList(R.drawable.nero, "Still Water", 2.00));
        drinks.add(new DrinksList(R.drawable.redbull, "Red Bull", 4.00));
        drinks.add(new DrinksList(R.drawable.zelita, "Zelita", 2.50));
        lv = (ListView)rootView.findViewById(R.id.drinksListView);
        adapter = new MyCustomDrinksListAdapter(getActivity().getApplicationContext(), R.layout.list_item, drinks);
        lv.setAdapter(adapter);
    }

}

我的适配器:

public class MyCustomDrinksListAdapter extends ArrayAdapter<DrinksList> {


    private List<DrinksList> items = null;
    private ArrayList<DrinksList> arraylist;
    public MyCustomDrinksListAdapter(Context context, int layoutId, List<DrinksList> items) {
        super(context, layoutId, items);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View arrayView = convertView;
        if(arrayView == null){
            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            arrayView = vi.inflate(R.layout.list_item, parent, false);
        }

        DrinksList currentPosition = getItem(position);
        if(currentPosition != null){
            ImageView image = (ImageView)arrayView.findViewById(R.id.product_image_coffee);
            image.setImageResource(currentPosition.getImage());

            TextView name = (TextView)arrayView.findViewById(R.id.product_name_coffee);
            name.setText(currentPosition.getName());

            TextView price = (TextView)arrayView.findViewById(R.id.product_price_coffee);
            price.setText(String.format("%.2f", currentPosition.getPrice()));
        }
        return arrayView;
    }

    public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        items.clear();
        if (charText.length() == 0) {
            items.addAll(arraylist);
        } else {
            for (DrinksList wp : arraylist) {
                if (wp.getName().toLowerCase(Locale.getDefault())
                        .contains(charText)) {
                    items.add(wp);
                }
            }
        }
        notifyDataSetChanged();
    }
}

我试过之后的And​​r​​oid开发者的例子,但我不能。
提前致谢!!
任何想法??

I tried following the android developers example but i couldnt. Thanks in advance!! any ideas??

推荐答案

相反适配器类添加过滤器的方法。 ..添加它在你分段..这样的..

Instead of adding filter method in Adapter class. .. add it in you fragment.. like this..

    public class DrinksFragment extends Fragment {

    private View rootView;
    private ArrayAdapter<DrinksList> adapter;
    private List<DrinksList> drinks;
    private ListView lv;
    ArrayList<DrinksList> mAllData=new ArrayList<DrinksList>();
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.activity_drinks_fragment, container, false);
        populateDrinksList();
        doSearch();
        return rootView;
    }

    private void doSearch() {
        final EditText et = (EditText)rootView.findViewById(R.id.searchListDrinks);
        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String text = et.getText().toString().toLowerCase(Locale.getDefault());
            filter(text);
            }
        });
    }



    private void populateDrinksList() {
        drinks = new ArrayList<DrinksList>();
        drinks.add(new DrinksList(R.drawable.coca, "Coca Cola", 2.50));
        drinks.add(new DrinksList(R.drawable.cocalight, "Coca Cola Light", 2.50));
        drinks.add(new DrinksList(R.drawable.cocazero, "Coca Cola Zero", 2.50));
        drinks.add(new DrinksList(R.drawable.orange, "Fanta Orange", 2.50));
        drinks.add(new DrinksList(R.drawable.lemon, "Fanta Lemon", 2.50));
        drinks.add(new DrinksList(R.drawable.ble, "Fanta Blue", 2.50));
        drinks.add(new DrinksList(R.drawable.sprite, "Sprite", 2.50));
        drinks.add(new DrinksList(R.drawable.soda, "Soda Water", 2.50));
        drinks.add(new DrinksList(R.drawable.tonic, "Tonic Water", 2.50));
        drinks.add(new DrinksList(R.drawable.ioli, "Sparkling Water Ioli", 2.50));
        drinks.add(new DrinksList(R.drawable.perrier, "Sparkling Water Perrier", 2.50));
        drinks.add(new DrinksList(R.drawable.nero, "Still Water", 2.00));
        drinks.add(new DrinksList(R.drawable.redbull, "Red Bull", 4.00));
        drinks.add(new DrinksList(R.drawable.zelita, "Zelita", 2.50));

mAllData.addAll(drinks);
        lv = (ListView)rootView.findViewById(R.id.drinksListView);
        adapter = new MyCustomDrinksListAdapter(getActivity().getApplicationContext(),        R.layout.list_item, drinks);
        lv.setAdapter(adapter);
    }


public void filter(String charText) {
        charText = charText.toLowerCase(Locale.getDefault());
        drinks .clear();
        if (charText.length() == 0) {
            drinks.addAll(mAllData);
        } else {
            for (DrinksList wp : mAllData) {
                if (wp.getName().toLowerCase(Locale.getDefault())
                        .contains(charText)) {
                    drinks .add(wp);
                }
            }
        }
        notifyDataSetChanged();
    }

}

和你适配器类将保持原样(删除过滤法)。

and you Adapter class will remain as it is(remove filter method)..

 public class MyCustomDrinksListAdapter extends ArrayAdapter<DrinksList> {


   // private List<DrinksList> items = null; // no use
    private ArrayList<DrinksList> arraylist;
    public MyCustomDrinksListAdapter(Context context, int layoutId, List<DrinksList> items) {
        super(context, layoutId, items);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View arrayView = convertView;
        if(arrayView == null){
            LayoutInflater vi;
            vi = LayoutInflater.from(getContext());
            arrayView = vi.inflate(R.layout.list_item, parent, false);
        }

        DrinksList currentPosition = getItem(position);
        if(currentPosition != null){
            ImageView image = (ImageView)arrayView.findViewById(R.id.product_image_coffee);
            image.setImageResource(currentPosition.getImage());

            TextView name = (TextView)arrayView.findViewById(R.id.product_name_coffee);
            name.setText(currentPosition.getName());

            TextView price = (TextView)arrayView.findViewById(R.id.product_price_coffee);
            price.setText(String.format("%.2f", currentPosition.getPrice()));
        }
        return arrayView;
    }


}

感谢您。希望工程...... !! 1

Thank you. Hope it works...!!1

这篇关于实现搜索里面的ListView片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆