Android Studio从新活动列表中过滤项目

问题描述 投票:-1回答:1

我正在尝试过滤列表,并从主要活动中获取一个字符串,当您到达SearchResultActivity时,它将显示与字符串中的字符匹配/包含这些字符的项目。目前,我收到一个错误的说法。我是Android Studio编程的新手,所以我们将不胜感激。

我已经使用一个日志来确保Array List中有1个元素(到目前为止已添加的项目),并且有一个String传递给其他活动。只是您需要了解进入其他活动(SearchResults ..)的代码即可。

原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'android.widget.Filter uk.edu.le.co2103.myrecipebook.RecipeListAdapter.getFilter()'在uk.edu.le.co2103.myrecipebook.SearchResultActivity.onCreate(SearchResultActivity.java:46)

我的SearchResultsActivity的代码

 public class SearchResultActivity extends AppCompatActivity {
private static final String TAG = "SEARCH RESULTS";
private RecipeViewModel mRecipeViewModel;
public static final int NEW_WORD_ACTIVITY_REQUEST_CODE = 1;
private RecipeListAdapter mAdapter;
private RecipeDao recDao;
//List of Recipes
LiveData<List<Recipe>> FullrecipesList;
ArrayList searchList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search_result);
    RecyclerView recyclerView = findViewById(R.id.recyclerview);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    mRecipeViewModel = new ViewModelProvider(this).get(RecipeViewModel.class);


   FullrecipesList = mRecipeViewModel.getAllRecipes();
    Log.d(TAG, "SIZE OF LIST IN QUERY: "+ FullrecipesList);
    String query = getIntent().getStringExtra("Search");
        Log.d(TAG, "Query String: " + query);

    mAdapter.getFilter().filter(query);    
    }

}

这里是可过滤代码所在的适配器的代码

public class RecipeListAdapter extends 
RecyclerView.Adapter<RecipeListAdapter.RecipeViewHolder> implements Filterable {
private static final String TAG = "ADAPTER";
private OnItemClickListener mListener;
private List<Recipe> mRecipes; // Cached copy of words
private List<Recipe> fullRecipes;
private List<Recipe> recipeList;



public interface OnItemClickListener{
    void onItemClick(int position, View view);
}
public void setOnItemClickListener(OnItemClickListener listener){
    mListener = listener;
}

class RecipeViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    private final TextView recipeItemView;

    private RecipeViewHolder(View itemView) {
        super(itemView);
        recipeItemView = itemView.findViewById(R.id.textView);
        fullRecipes = new ArrayList<>(mRecipes);
        Log.d(TAG, "Size of Array in Adapter:" + fullRecipes.size());
        itemView.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v){
                if (mListener != null){
                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION){
                        mListener.onItemClick(position, v);
                    }
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        mListener.onItemClick(getAdapterPosition(), view);
    }
}

private final LayoutInflater mInflater;


RecipeListAdapter(Context context) {
    mInflater = LayoutInflater.from(context);
}

@Override
public RecipeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View itemView = mInflater.inflate(R.layout.recyclerview_item, parent, false);
    return new RecipeViewHolder(itemView);
}

@Override
public void onBindViewHolder(RecipeViewHolder holder, int position) {
    if (mRecipes != null) {
        Recipe current = mRecipes.get(position);
        holder.recipeItemView.setText(current.getName());
    } else {
        // Covers the case of data not being ready yet.
        holder.recipeItemView.setText("No Recipes");
    }
}

void setWords(List<Recipe> recipes){
    mRecipes = recipes;
    notifyDataSetChanged();
}

public void deleteItem(final int position) {
    mRecipes.remove(position);
    notifyItemRemoved(position);
}
public void updateItem(Recipe recipe) {
    mRecipes.remove(recipe);
    notifyDataSetChanged();
}
public List<Recipe> getList(){

    return recipeList;
}


@Override
public int getItemCount() {
    if (mRecipes != null)
        return mRecipes.size();
    else return 0;
}
public interface OnNoteListener{}
@Override
public Filter getFilter() {
    return filter;
}
private Filter filter = new Filter(){

    @Override
    protected FilterResults performFiltering(CharSequence constraint) {
       List<Recipe> filteredList = new ArrayList<>();
       Log.d(TAG, "Attempting to filter word: " + constraint);
       if (constraint.length() == 0){
           //Here should say 0 found
       }
       else {
           String filterPattern = constraint.toString().toLowerCase().trim();

           for (Recipe recipe : fullRecipes){
               if (recipe.getName().toLowerCase().contains(filterPattern)){
                   filteredList.add(recipe);
               }
           }
       }
       FilterResults results = new FilterResults();
       results.values = filteredList;
       return results;
    }

    @Override
    protected void publishResults(CharSequence charSequence, FilterResults filterResults) {
            recipeList.clear();
            recipeList.addAll((List)filterResults.values);
            notifyDataSetChanged();
    }
android adapter
1个回答
0
投票

不确定您尝试如何传递查询,但也许可以尝试此操作?

循环遍历食谱数组,并检查食谱是否等于您的查询(如果将其添加到过滤列表中)>>

private void filter(String text)
    {
        ArrayList<recipe> filteredList = new ArrayList<recipe>();

        for (Recipe item : recipes)
        {
            if(item.getName().toLowerCase().equals(query.toLowerCase())){
                filteredList.add(item);

            }

        }
        adapter.filterlist(filteredList);
    }
© www.soinside.com 2019 - 2024. All rights reserved.