2016-03-07 50 views
0

您好所有我想添加的搜索funcationality到ListView控件因我DOEN以下Android的列表視圖搜索得到空指針錯誤

@Override 
    public Filter getFilter() { 
     if (valueFilter == null) { 
      valueFilter = new ValueFilter(); 
     } 
     return valueFilter; 
    } 

    private class ValueFilter extends Filter { 

     @Override 
     protected FilterResults performFiltering(CharSequence constraint) { 
      FilterResults results = new FilterResults(); 

      if (constraint != null && constraint.length() > 0) { 

       for (int i = 0; i < mStringFilterList.size()-1; i++) { 
        if ((mStringFilterList.get(i).getServiceName().toString()) 
          .contains(constraint.toString())) { 
         // Contacts contacts = new Contacts(); 


         contacts.setServiceName(mStringFilterList.get(i) 
           .getServiceName()); 
         contacts.setAmount(mStringFilterList.get(i).getAmount() 
           + ""); 
         System.out 
           .println("mStringFilterList.get(i).getAmount()" 
             + mStringFilterList.get(i).getAmount()); 
         contacts.setTrnDate(mStringFilterList.get(i) 
           .getTrnDate()); 
         System.out 
           .println("mStringFilterList.get(i).getTrnDate()" 
             + mStringFilterList.get(i).getTrnDate()); 
         filterList.add(contacts); 
        } 
       } 
       results.count = filterList.size(); 
       results.values = filterList; 
      } else { 
       results.count = mStringFilterList.size(); 
       results.values = mStringFilterList; 
      } 
      return results; 
     } 

     @Override 
     protected void publishResults(CharSequence constraint, 
       FilterResults results) { 
      list=(ArrayList<Contacts>) results.values; 

      notifyDataSetChanged(); 
     } 

    } 

} 

但在mStringFilterList.get(i).getServiceName().toString())

+0

它會幫助,如果你發佈的代碼*** *** MyTransactionAdpter仍然 –

+0

無法看到那裏的原因可能是,根據日誌的NPE是* MyTransactionAdpter.getCount自(MyTransactionAdpter.java:39)* @Hiren Patel的回答可能是正確的,但您可能只需修改適配器中的*** getCount()***。 –

回答

2

得到錯誤更新getCount()方法:

@Override 
public int getCount() { 
    return list.size(); 
} 

原因:您需要退貨號碼元素存儲在ArrayList

完成