RecyclerView和数据库,无法删除项目

时间:2015-07-13 11:28:04

标签: android database sqlite android-recyclerview

好的,所以我在这个问题的过去几天一直在圈子里,无法找到一个有效的解决方案,也许我不知道如何正确地说出这个问题。但是这里。

我正在使用一个使用RecyclerView适配器的项目,从我的sqlite数据库加载和显示简单元素,包含String引用和对图像资源的字符串引用。这部分工作正常,但我似乎无法删除任何项目。

我已经完成了代码,确保与我的数据库的连接仅在需要时打开并在之后关闭,并且此处应该没有未解决的问题。

我实际上可以在单击元素时从Recyclerview适配器内部删除项目,但如果我退出并再次输入,则已再次加载已删除的项目。这就把我的问题放在了我想的数据库中,或者也许某种决斗班卓就像函数重新创建一个已删除项目的问题。但是我现在已经多次遵循这个代码,而对于我的生活,我似乎无法解决这个问题。

在我的删除点击事件中,我正在对我的数据库和我的recyclerview适配器执行删除调用,但问题仍然存在。

我希望有任何可以帮助我查明问题的输入。我不确定在这个问题中包含哪些代码,我已经包含了我的RecyclerView适配器和数据库适配器。而且我知道在不能看到代码的情况下回答这些问题几乎是不可能的,但仍然如此。

*请原谅一塌糊涂。我一直在尝试很多东西。

// RECYCLERVIEW ADAPTER

    public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.DataObjectHolder>
{
    private ArrayList<Instance> instanceDataset;
    private theClickListener clickListener;
    public Context context;

    private DBAdapter db;
    private Button deleteButton;
    private int thePosition;


    public RecyclerViewAdapter(Context context)
    {
        this.context = context;
    }


// ViewHolder inner class


    class DataObjectHolder extends RecyclerView.ViewHolder implements View.OnClickListener
    {
        CardView cv;
        TextView comment;
        ImageView imageViewLocalAdapter;


        public DataObjectHolder(final View itemView)
        {
            super(itemView);
            cv = (CardView) itemView.findViewById(R.id.cardview_layout);
            comment = (TextView) itemView.findViewById(R.id.cardview_textView);
            imageViewLocalAdapter = (ImageView) itemView.findViewById(R.id.cardview_imageView);

            deleteButton = (Button) itemView.findViewById(R.id.delete_button);
            //itemView.setOnClickListener(this);
        }

        // this here displays the position of item clicked
        @Override
        public void onClick(View v)
        {
            //Intent intent = new Intent(context, SingleInstanceFragment.class);

           // Log.d(" clicking ", "onClick " + getPosition());



            //clickListener.onTheGreatItemClick(getPosition(), v);
        }
    }



    public void setOnItemClickListener(theClickListener clickListener)
    {
        this.clickListener = clickListener;
    }

    public RecyclerViewAdapter(ArrayList<Instance> instanceDataSet, Context context)
    {
        this.instanceDataset = instanceDataSet;
        this.context = context;
    }

// THE VIEWHOLDER


    @Override
    public DataObjectHolder onCreateViewHolder(ViewGroup viewGroup, int viewType)
    {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview, viewGroup, false);
        DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
        return dataObjectHolder;
    }

    @Override
    public void onBindViewHolder(final DataObjectHolder holder, final int position)
    {
        final Instance inst = instanceDataset.get(position);
        openDB();
        thePosition = position;

        holder.comment.setText(inst.getComment());

        String middle = "/sdcard/unapp/";
        Uri uri = Uri.parse("file://" + middle + inst.getImage());

        Picasso.with(holder.imageViewLocalAdapter.getContext())
                .load(uri)
                .fit().centerCrop()
                .into(holder.imageViewLocalAdapter);

        final RecyclerView.ViewHolder reHolder = holder;

        holder.imageViewLocalAdapter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                deleteItem(position);
                Log.d("position in ", " RecyclerView Adapter " + position);
                onBindViewHolder(holder, position);
//                Intent intent = new Intent(context, SingleInstanceActivity.class);
//                intent.putExtra("position", position);
//
//                context.startActivity(intent);
            }

        });
    }

    public void addItem(Instance dataObj, int index)
    {
        instanceDataset.add(dataObj);
        notifyItemInserted(index);
    }

    public void deleteItem(int index)
    {
        Log.d("count", "deleteItem " +getItemCount());
        instanceDataset.remove(index);
        db.deleteRow(index);

        db.close();
        notifyItemRemoved(index);

        //new RecyclerViewAdapter(instanceDataset, context);

        Log.d("count", "deleteItem DEUX  " +getItemCount());
    }

    private void openDB()
    {
        db = new DBAdapter(this.context);
        db.open();
    }


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



    public Instance getItem(int position)
    {
        return instanceDataset.get(position);
    }

//  clicklistener interface

    public interface theClickListener
    {
        void onTheGreatItemClick(int position, View v);

    }
}

// DATABASE ADAPTER

    public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.DataObjectHolder>
{
    private ArrayList<Instance> instanceDataset;
    private theClickListener clickListener;
    public Context context;

    private DBAdapter db;
    private Button deleteButton;
    private int thePosition;


    public RecyclerViewAdapter(Context context)
    {
        this.context = context;
    }


// ViewHolder inner class


    class DataObjectHolder extends RecyclerView.ViewHolder implements View.OnClickListener
    {
        CardView cv;
        TextView comment;
        ImageView imageViewLocalAdapter;


        public DataObjectHolder(final View itemView)
        {
            super(itemView);
            cv = (CardView) itemView.findViewById(R.id.cardview_layout);
            comment = (TextView) itemView.findViewById(R.id.cardview_textView);
            imageViewLocalAdapter = (ImageView) itemView.findViewById(R.id.cardview_imageView);

            deleteButton = (Button) itemView.findViewById(R.id.delete_button);
            //itemView.setOnClickListener(this);
        }

        // this here displays the position of item clicked
        @Override
        public void onClick(View v)
        {
            //Intent intent = new Intent(context, SingleInstanceFragment.class);

           // Log.d(" clicking ", "onClick " + getPosition());



            //clickListener.onTheGreatItemClick(getPosition(), v);
        }
    }



    public void setOnItemClickListener(theClickListener clickListener)
    {
        this.clickListener = clickListener;
    }

    public RecyclerViewAdapter(ArrayList<Instance> instanceDataSet, Context context)
    {
        this.instanceDataset = instanceDataSet;
        this.context = context;
    }

// THE VIEWHOLDER


    @Override
    public DataObjectHolder onCreateViewHolder(ViewGroup viewGroup, int viewType)
    {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cardview, viewGroup, false);
        DataObjectHolder dataObjectHolder = new DataObjectHolder(view);
        return dataObjectHolder;
    }

    @Override
    public void onBindViewHolder(final DataObjectHolder holder, final int position)
    {
        final Instance inst = instanceDataset.get(position);
        openDB();
        thePosition = position;

        holder.comment.setText(inst.getComment());

        String middle = "/sdcard/unapp/";
        Uri uri = Uri.parse("file://" + middle + inst.getImage());

        Picasso.with(holder.imageViewLocalAdapter.getContext())
                .load(uri)
                .fit().centerCrop()
                .into(holder.imageViewLocalAdapter);

        final RecyclerView.ViewHolder reHolder = holder;

        holder.imageViewLocalAdapter.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                deleteItem(position);
                Log.d("position in ", " RecyclerView Adapter " + position);
                onBindViewHolder(holder, position);
//                Intent intent = new Intent(context, SingleInstanceActivity.class);
//                intent.putExtra("position", position);
//
//                context.startActivity(intent);
            }

        });
    }

    public void addItem(Instance dataObj, int index)
    {
        instanceDataset.add(dataObj);
        notifyItemInserted(index);
    }

    public void deleteItem(int index)
    {
        Log.d("count", "deleteItem " +getItemCount());
        instanceDataset.remove(index);
        db.deleteRow(index);

        db.close();
        notifyItemRemoved(index);

        //new RecyclerViewAdapter(instanceDataset, context);

        Log.d("count", "deleteItem DEUX  " +getItemCount());
    }

    private void openDB()
    {
        db = new DBAdapter(this.context);
        db.open();
    }


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



    public Instance getItem(int position)
    {
        return instanceDataset.get(position);
    }

//  clicklistener interface

    public interface theClickListener
    {
        void onTheGreatItemClick(int position, View v);

    }
}

0 个答案:

没有答案