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

问题描述:

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

@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函数返回一个空指针的问题。您可以在此处发布该功能,以便我们可以查看它并进行调试并查看发生了什么。