Android Studio Delete Items from Recycler View and List [duplicate]

≡放荡痞女 提交于 2021-02-11 14:45:26

问题


I am trying to remove a specific item when I click on it. I have run logs so I know that it's picking an item I just don't understand what the error means. If it is important I am using room database and using a recycler view.

java.lang.NullPointerException: Attempt to invoke virtual method 'void uk.edu.le.co2103.myrecipebook.RecipeListAdapter.notifyItemRemoved(int)' on a null object reference at uk.edu.le.co2103.myrecipebook.MainActivity$4.onClick(MainActivity.java:126)

I have checked that there is data in the list hence the logs below.

D/MAIN ACTIVITY: List: [uk.edu.le.co2103.myrecipebook.Recipe@c97e34d, uk.edu.le.co2103.myrecipebook.Recipe@e7b6e02, uk.edu.le.co2103.myrecipebook.Recipe@d9ea313] 2020-04-01 23:07:29.300 25679-25679/uk.edu.le.co2103.myrecipebook D/MAIN ACTIVITY: Index: 2

Here is the code for my deletion part of main Activity

    public void onItemClick(int position, View view) {
        Log.d(TAG, "onItemClick Position: " + position);
    AlertDialog.Builder alertDialog = new AlertDialog.Builder(this);
    alertDialog.setTitle("Edit or Delete...");
    alertDialog.setPositiveButton("Edit", new 
 DialogInterface.OnClickListener()
 alertDialog.setNegativeButton("Delete", new 
 DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            //Delete
            Log.d(TAG, "List: " + recipesList);
            Log.d(TAG, "Index: " + position);
            int removeIndex = position;
            recipesList.remove(removeIndex);
            mAdapter.notifyItemRemoved(removeIndex);


        }
    });

My Adapter Code

 public class RecipeListAdapter extends 
 RecyclerView.Adapter<RecipeListAdapter.RecipeViewHolder> {
private OnItemClickListener mListener;
private LiveData<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);
        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;
private List<Recipe> mRecipes; // Cached copy of words

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

@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();
}


// getItemCount() is called many times, and when it is first called,
// mWords has not been updated (means initially, it's null, and we can't return null).
@Override
public int getItemCount() {
    if (mRecipes != null)
        return mRecipes.size();
    else return 0;
}
public interface OnNoteListener{}

}


回答1:


Can you share your adapter code here? I had this problem in the past and it happens because you need to refresh your list contents inside your adapter (you are now only noticing it with .notifyItemRemoved(int index) ) but I realy need to see your adapter to help you about how to do it, in my situation an observer was the best way.




回答2:


I think you didn't initialize the adapter or the list.



来源:https://stackoverflow.com/questions/60981158/android-studio-delete-items-from-recycler-view-and-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!