【问题标题】:Delete LiveData with Room onClick使用 Room onClick 删除 LiveData
【发布时间】:2018-03-22 07:39:28
【问题描述】:

我按照google codelabs 教程使用LiveDataRoom 创建了一个数据库。

问题是我无法删除带有onClick 的条目。 这是我的

@Dao
public interface PlacesDao {
  @Query("SELECT * from places_table ORDER BY place ASC")
  LiveData<List<Places>> getAllPlaces();
//  LiveData<List<Word>> getAllWords();


  @Insert
  void insert(Places places);

  @Query("DELETE FROM places_table")
  void deleteAll();

  @Delete
  void deletePlace(Places places);
}

这就是 ViewModel

public class PlacesViewModel extends AndroidViewModel {

    private PlacesRepository mRepository;
    private LiveData<List<Places>> mAllWords;

    public PlacesViewModel (Application application) {
        super(application);
        mRepository = new PlacesRepository(application);
        mAllWords = mRepository.getAllPlaces();
    }

    LiveData<List<Places>> getAllWords() { return mAllWords; }

    public void insert(Places places) { mRepository.insert(places); }
}

这是适配器

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

    class PlacesViewHolder extends RecyclerView.ViewHolder {
        public TextView place;
        public TextView lati;
        public TextView longi;
        public ImageView delbutton;

        private PlacesViewHolder(View itemView) {
            super(itemView);
            place = itemView.findViewById(R.id.placeLine);
            longi = itemView.findViewById(R.id.longLine);
            lati = itemView.findViewById(R.id.latiLine);
            delbutton = itemView.findViewById(R.id.delicon);
        }
    }

    private final LayoutInflater mInflater;
    private List<Places> mPlaces; // Cached copy of words

    PlacesAdapter(Context context) { mInflater = LayoutInflater.from(context); }

    @Override
    public PlacesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = mInflater.inflate(R.layout.places_list_item, parent, false);
        return new PlacesViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(PlacesViewHolder holder, final int position) {
      Places current = mPlaces.get(position);
      holder.place.setText(current.getPlace());
      holder.lati.setText(current.getLati());
      holder.longi.setText(current.getLongi());
      holder.delbutton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
          Snackbar.make(view, "DelButton Clisked", Snackbar.LENGTH_LONG).show();
          mPlaces.remove(position);
          Log.e("Adapter", String.valueOf(position));
        }
      });
    }

    void setPlaces(List<Places> places){
        mPlaces = places;
        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 (mPlaces != null)
            return mPlaces.size();
        else return 0;
    }
}

我知道在 BindView 中做 onClickListner 不好,但这是我能做到的最接近的。 单击delbutton 时,Snackber 和 Log 都被激活,Log.e 显示预期值,但该项目未被删除。

那么,有人可以帮助我如何做到这一点吗?

问候,

更新 视图正在运行,但我仍然无法从数据库中删除行。有什么帮助吗?

【问题讨论】:

  • 您已从 RecyclerView 中删除,但未从数据库中删除 :)
  • 实际上,它甚至没有从视图中删除。
  • 是的,因为您从列表中删除了项目,但您没有通知适配器有关更改:)
  • 好的。以及如何从数据库中实际删除该项目?在BindViewHolder上做这个操作可以吗?

标签: android android-room android-livedata


【解决方案1】:

在删除后的 onClick 中,只需调用 notifyItemRemoved(position);。 而且我建议你不要在onClick中使用位置,最好使用holder.getAdapterPosition()

【讨论】:

    猜你喜欢
    • 2019-06-06
    • 1970-01-01
    • 2021-09-24
    • 1970-01-01
    • 2020-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多