0

我想implementRecyclerViewFilterable interfaceFilterToolbarSearcView Widget我的名單。的Android RecyclerView與過濾的接口

但我的問題是我不能用新數據更新我的列表。notifyDataSetChanged() in publishResults方法什麼都不做,我不知道爲什麼。

只有元素的數量發生了變化。

這是我實現Recycler AdapterFilterable Interface

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ShoppingListHolder> implements Filterable { 

    private Context context; 
    private ArrayList<ShoppingListModel> shoppingList, filteredShoppingList; 
    private ListFilter listFilter; 

    public RecyclerAdapter(Context context, ArrayList<ShoppingListModel> shoppingList) { 
     this.context = context; 
     this.shoppingList = shoppingList; 
     this.filteredShoppingList = shoppingList; 
    } 

    @Override 
    public ShoppingListHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
     View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.shopping_listitem_layout_new, viewGroup, false); 
     return new ShoppingListHolder(itemView); 
    } 

    @Override 
    public int getItemCount() { 
     return this.filteredShoppingList.size(); 
    } 

    @Override 
    public void onBindViewHolder(ShoppingListHolder shoppingListHolder, int position) { 
     final ShoppingListModel list = this.filteredShoppingList.get(position); 
     shoppingListHolder.titleView.setText(list.getTitle()); 

     if(list.getAlarmDate() == null || list.getAlarmDate().isEmpty()) 
      shoppingListHolder.reminderView.setText("--/--/--"); 
     else 
      shoppingListHolder.reminderView.setText(list.getAlarmDate()); 

     ArrayList<ListItemModel> listItems = DBManager.getShoppingListItems(DBHelper.SHOPPING_LIST_ITEM_PARENT_ID + " = " + list.getId()); 
     double maxItems = listItems.size(); 
     double checkedCount = 0; 
     shoppingListHolder.itemContainer.removeAllViews(); 
     for (int i = 0; i < listItems.size(); i++) { 
      ListItemModel item = listItems.get(i); 
      ShoppingListItemView listItem = new ShoppingListItemView(context, item, i + 1, list.getColor()); 
      shoppingListHolder.itemContainer.addView(listItem); 
      if (item.isChecked() == ListItemModel.ListItemState.Checked.ordinal()) { 
       checkedCount++; 
      } 
     } 

     if(checkedCount == 0){ 
      shoppingListHolder.progressPercentageView.setText("0%"); 
     }else{ 
      shoppingListHolder.progressPercentageView.setText((int)(checkedCount/maxItems * 100) + "%"); 
     } 

     shoppingListHolder.progressBar.setMax((int) maxItems); 
     shoppingListHolder.progressBar.setProgress((int) checkedCount); 

     shoppingListHolder.rippleView.setOnRippleCompleteListener(new RippleView.OnRippleCompleteListener() { 
      @Override 
      public void onComplete(RippleView rippleView) { 
       Intent shoppingList = new Intent(context, ShoppingListItemActivity.class); 
       Bundle extras = new Bundle(); 
       extras.putLong(ShoppingListModel.SHOPPING_LIST_MODEL_KEY, list.getId()); 
       shoppingList.putExtras(extras); 
       if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
        context.startActivity(shoppingList, ActivityOptionsCompat.makeSceneTransitionAnimation((Activity) context).toBundle()); 
       }else{ 
        context.startActivity(shoppingList); 
       } 
      } 
     }); 
    } 

    class ShoppingListHolder extends RecyclerView.ViewHolder { 
     CardView cardView; 
     RippleView rippleView; 
     TextView titleView; 
     TextView reminderView; 
     ImageView locationPin; 
     LinearLayout itemContainer; 
     TextView progressPercentageView; 
     ProgressBar progressBar; 

     public ShoppingListHolder(View itemView) { 
      super(itemView); 
      rippleView = (RippleView) itemView.findViewById(R.id.card_view_ripple); 
      cardView = (CardView) itemView.findViewById(R.id.card_view); 
      titleView = (TextView) itemView.findViewById(R.id.title); 
      reminderView = (TextView) itemView.findViewById(R.id.reminder); 
      locationPin = (ImageView) itemView.findViewById(R.id.location_pin); 
      itemContainer = (LinearLayout) itemView.findViewById(R.id.item_container); 
      progressPercentageView = (TextView) itemView.findViewById(R.id.progress_percentage_label); 
      progressBar = (ProgressBar) itemView.findViewById(R.id.progressBar); 
     } 
    } 

    @Override 
    public Filter getFilter() { 
     if (listFilter == null) { 
      listFilter = new ListFilter(); 
     } 
     return listFilter; 
    } 

    private class ListFilter extends Filter { 

     @Override 
     protected FilterResults performFiltering(CharSequence constraint) { 
      FilterResults filterResults = new FilterResults(); 
      ArrayList<ShoppingListModel> tempList; 
      if (constraint != null && constraint.length() > 0) { 

       tempList = DBManager.getShoppingList(DBHelper.SHOPPING_LIST_TITLE + " like '%" + constraint + "%' or " + 
                DBHelper.SHOPPING_LIST_TAGS + " like '%" + constraint + "%'"); 

       filterResults.count = tempList.size(); 
       filterResults.values = tempList; 
      } else { 
       tempList = DBManager.getShoppingList(null); 
       filterResults.count = tempList.size(); 
       filterResults.values = tempList; 
      } 
      return filterResults; 
     } 

     @Override 
     protected void publishResults(CharSequence constraint, FilterResults results) { 
      filteredShoppingList.clear(); 
      filteredShoppingList.addAll((ArrayList<ShoppingListModel>) results.values); 
      notifyDataSetChanged(); 
     } 
    } 
} 

由於人們對於提前幫助。

回答

0

我建議在加入數組列表代替你清楚你,而不是做這個

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

     filteredShoppingList = (ArrayList<ShoppingListModel>)filterResults.values; 
      if (filterResults.count > 0) { 
       notifyDataSetChanged(); 
      } else { 
       notifyDataSetInvalidated(); 
      } 
    }