单击时删除数据库项并更新视图

时间:2018-03-13 06:04:11

标签: android android-sqlite android-room

我试图在这里删除按钮上的项目onClick(定义为delbutton)。 我的适配器看起来像(为了简洁,我已经包含了定义并删除了onBindViewHolder),并且onclick方法在ViewHolder中。

public class PlacesAdapter extends RecyclerView.Adapter<PlacesAdapter.ViewHolder> {

  private static final String TAG = "PlaceAdapter";

  List<PlaceSaved> items;
  PlaceDatabase db;

  public PlacesAdapter(List<PlaceSaved> items, PlaceDatabase db) {
    this.items = items;
    this.db = db;
  }

  @Override
  public PlacesAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext())
        .inflate(R.layout.places_list_item, parent, false);
    return new ViewHolder(view);
  }

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

      public class ViewHolder extends RecyclerView.ViewHolder
          implements View.OnClickListener {
        public TextView name;
        public TextView time;
        public TextView longi;
        public ImageButton delbutton;
        public CardView cview;

        public ViewHolder(View itemView) {
          super(itemView);
          name = itemView.findViewById(R.id.secondLine);
          time = itemView.findViewById(R.id.firstLine);
          longi = itemView.findViewById(R.id.longitude);
          delbutton = itemView.findViewById(R.id.delicon);
          cview = itemView.findViewById(R.id.place_view);
          itemView.setOnClickListener(this);
          delbutton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
             // Log.e("What", "Get"+getAdapterPosition());
              removeItem(getAdapterPosition());
            }
          });
        }

        @Override
        public void onClick(View view) {
          // Clicked postion
          int position = getAdapterPosition();
          PlaceSaved place = items.get(position);
          // Here is the data Do your stuff
          Log.e("Will WORK", place.getLongi());
        }
      }
      private void removeItem(int position){
        db.databaseInterface().delete(items.get(position));
        items.remove(position);
        notifyItemRemoved(position);
      }
    }

现在点击delbutton,我收到错误:

java.lang.IllegalStateException:无法访问主线程上的数据库,因为它可能会长时间锁定UI。

我知道,我需要以某种方式调用runOnUIThread,但在ViewHolder中,我无法定义它。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

创建新的Thread Executors类,如下所示

    public class AppExecutors {

    private final Executor mDiskIO;

    private AppExecutors(Executor diskIO) {
        this.mDiskIO = diskIO;
    }

    public AppExecutors() {
        this(Executors.newSingleThreadExecutor());
    }

    public Executor diskIO() {
        return mDiskIO;
    }
 }

像这样使用那个类..使用返回类型的删除接口int类型,检查成功删除项目。

private void removeItem(int position) {
        new AppExecutors().diskIO().execute(new Runnable() {
            @Override
            public void run() {
                if(db.databaseInterface().delete(items.get(position)) > 0 ){
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            items.remove(position);
                            notifyItemRemoved(position);
                        }
                    });
                }
            }
        });
    }