Android Studio从回收站视图和列表中删除项目 [英] Android Studio Delete Items from Recycler View and List

查看:82
本文介绍了Android Studio从回收站视图和列表中删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我单击某个特定项目时,我正尝试将其删除.我已经运行了日志,所以我知道它正在选择一个项目,我只是不明白错误的含义.如果重要的话,我正在使用会议室数据库并使用回收站视图.

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:尝试调用虚拟方法'voiduk.edu.le.co2103.myrecipebook.RecipeListAdapter.notifyItemRemoved(int)'在空对象上参考在uk.edu.le.co2103.myrecipebook.MainActivity $ 4.onClick(MainActivity.java:126)

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/主要活动:列表:[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/主要活动:索引:2

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);


        }
    });

我的适配器代码

 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{}

}

推荐答案

我认为您没有初始化适配器或列表.

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

这篇关于Android Studio从回收站视图和列表中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆