如何通过按钮删除房间SQlite行

时间:2018-03-07 09:50:25

标签: android android-room

在我的Room Sqlite数据库适配器中,我有一个图像按钮,定义为:

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

    List<PlaceSaved> items;

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

    @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 void onBindViewHolder(PlacesAdapter.ViewHolder holder, int position) {
        holder.name.setText(items.get(position).getTitle());
        holder.time.setText(items.get(position).getTime());
        holder.delbutton.setClickable(true);
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder{
        public TextView name;
        public TextView time;
        public ImageButton delbutton;

        public ViewHolder(View itemView) {
            super(itemView);
            name = itemView.findViewById(R.id.secondLine);
            time= itemView.findViewById(R.id.firstLine);
            delbutton = itemView.findViewById(R.id.delicon);
        }
    }
}
带有delicon

places_list_item.xml

中定义
  <TextView
     android:id="@+id/secondLine"/>
   <TextView
    android:id="@+id/firstLine"/>
 <ImageButton
    android:id="@+id/delicon"/>

现在,我试图通过单击图像按钮删除每个相应的行。 我已将DBinterface定义为:

@Dao
public interface DatabaseInterface {
      @Query("SELECT * FROM placesaved")
      List<PlaceSaved> getAllItems();

    @Insert
    void insertAll(PlaceSaved... todoListItems);
    @Delete
    void delete(PlaceSaved todeListItems);
}

现在我被卡住了。 我应该如何以及在哪里调用delete函数来删除行? 请帮助。

另外,不知道是否需要这个,列表定义为:

@Entity
public class PlaceSaved {

  @PrimaryKey(autoGenerate = true)
  private int id;

  @ColumnInfo(name = "time")
  private String time;

  @ColumnInfo(name="title")
  private String title;

  public PlaceSaved(){

  }

  public PlaceSaved(String time, String title) {
    this.time = time;
    this.title = title;
  }

  public String getTime() {
    return time;
  }

  public void setTime(String time) {
    this.time = time;
  }

  public String getTitle() {
    return title;
  }

  public void setTitle(String title) {
    this.title = title;
  }

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }
}

1 个答案:

答案 0 :(得分:1)

创建onClickListner()和removeItem()函数并在侦听器中调用它。

@Override
public void onBindViewHolder(PlacesAdapter.ViewHolder holder, int position) {
    final PlaceSaved item=items.get(position);
    holder.name.setText(item.getTitle());
    holder.time.setText(item.getTime());
    holder.delbutton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
          removeItem(item);
         }
      });
    }

private void removeItem(PlaceSaved infoItem){
        myDatabase=new MyDatabase();
        myDatabase.delete(infoItem);
        int pos=items.indexOf(infoItem);
        items.remove(pos);
        notifyItemRemoved(pos);
        myDatabase.close();
    }