繁体   English   中英

搜索项目列表视图适配器片段

[英]Search items list view adapter fragment

我有一个带有自定义单项视图的列表视图,该视图显示标题和图像。 我已经实现了一个搜索栏来按标题搜索项目,但是当我尝试搜索某些内容时,我有一个nullpointerexception。 当适配器中只有标题时(所有项目中的图像均相同),它可以正常工作。 但是现在在新适配器中,我还更改了任何单个项目中的图像。 我认为的错误是因为使用Alimentacion.this.adapter.getFilter()。filter(cs); (这行是我有nullpointer的地方)以提供搜索功能,我只将标题放在搜索框中..但我不知道如何仅在标题中而不是在完整的适配器中进行搜索。片段中的列表视图。 Mi代码是:

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View myInflatedView = inflater.inflate(R.layout.fragment_alimentacion, container,false);

    // Set the Text to try this out
    lv = (ListView)myInflatedView.findViewById(R.id.list_view);
    inputSearch = (EditText) myInflatedView.findViewById(R.id.inputSearch);
// Listview Data
    String categorias[] = {"1. Huevos y Lacteos", "2. Carnes y Derivados", "3. Pescados y Mariscos", "4. Aceites y grasos", "5. Verduras y hortalizas",
            "6. Frutas", "7. Bebidas",
            "8. Comida Rapida", "9. Pasta y Cereales", "10. Bolleria y Snacks"};
    Integer ImgCategorias[] = {R.drawable.cat1,R.drawable.cat2,R.drawable.cat3,R.drawable.cat4,R.drawable.cat5,R.drawable.cat6
            ,R.drawable.cat7,R.drawable.cat8,R.drawable.cat9,R.drawable.cat10};
   // String Calorias[]= new String [categorias.length];

    // Pass results to ListViewAdapter Class
    CustomList adapter = new
            CustomList(this.getActivity(), categorias, ImgCategorias);


    // Binds the Adapter to the ListView
    lv.setAdapter(adapter);

    // OLD ADAPTER
 /*   adapter2 = new ArrayAdapter<String>(this.getActivity(), R.layout.adapter_categorias, R.id.TVTitulo, categorias);
    lv.setAdapter(adapter2); */

    /**
     * Enabling Search Filter
     * */
    inputSearch.addTextChangedListener(new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
            Alimentacion.this.adapter.getFilter().filter(cs);
                        }
        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {
            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
        }
    });

    return myInflatedView;
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}

如果该行返回空指针异常,则适配器中的getFilter函数可能存在问题,该函数返回空指针。 您可以在此处发布该函数,以便我们对其进行调试并查看其中发生了什么。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM