title: how to update database using room persistance library and livedata
body: <p>I am trying to update my database using <strong>Room persistence Library</strong> and <strong>Livedata</strong>. I am very new to java, so, with the manual and various tutorial, I have setup the DAO, entity etc. But I am still struggling with how to actually add the data.</p>
  
  <p>This is my database defination:</p>
  
  <pre><code>@Entity
  public class PlaceSaved {
  
    @PrimaryKey(autoGenerate = true)
    private int id;
    private String place;
    private String lati;
    private String longi;
  
  
    public PlaceSaved(String place, String lati, String longi) {
      this.place = place;
      this.lati = lati;
      this.longi = longi;
    }
  
    public String getPlace() {
      return place;
    }
    public void setPlace(String place) {
      this.place = place;
    }
  
    public String getLongi() {
      return longi;
    }
    public void setLongi(String longi) {
      this.longi = longi;
    }
  
  
    public String getLati() {
      return lati;
    }
    public void setLati(String lati) {
      this.lati = lati;
    }
  
    public int getId() {
      return id;
    }
    public void setId(int id) {
      this.id = id;
    }
  }
  </code></pre>
  
  <p><strong>DAO</strong></p>
  
  <pre><code>@Dao
  public interface DatabaseInterface {
    @Query("SELECT * FROM placesaved")
    LiveData&lt;List&lt;PlaceSaved&gt;&gt; getAllItems();
  
    @Insert
    void insertAll(PlaceSaved... placeSaveds);
    @Delete
    void delete(PlaceSaved... placeSaveds);
    @Update
    void update(PlaceSaved... placeSaveds);
  }
  </code></pre>
  
  <p><strong>Adapter</strong></p>
  
  <pre><code>public class PlacesAdapter extends RecyclerView.Adapter&lt;PlacesAdapter.RecyclerViewHolder&gt;{
    private List&lt;PlaceSaved&gt; items;
    private View.OnClickListener ClickListener;
  
    public  PlacesAdapter(List&lt;PlaceSaved&gt; items){//}, View.OnClickListener ClickListener) {
          this.items = items;
          //this.ClickListener = ClickListener;
    }
  
      @Override
      public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          return new RecyclerViewHolder(LayoutInflater.from(parent.getContext())
                  .inflate(R.layout.places_list_item, parent, false));
      }
  
      @Override
      public void onBindViewHolder(final RecyclerViewHolder holder, int position) {
          PlaceSaved placeSaved = items.get(position);
          holder.itemTextView.setText(placeSaved.getPlace());
          holder.nameTextView.setText(placeSaved.getLati());
          holder.dateTextView.setText(placeSaved.getLongi());
  /*        holder.dateTextView.setText(borrowModel.getBorrowDate().toLocaleString().substring(0, 11));
          holder.itemView.setTag(borrowModel);
          holder.itemView.setOnLongClickListener(longClickListener);*/
      }
  
      @Override
      public int getItemCount() {
          return items.size();
      }
  
      public void addItems(List&lt;PlaceSaved&gt; items) {
          this.items = items;
          notifyDataSetChanged();
      }
  
      static class RecyclerViewHolder extends RecyclerView.ViewHolder {
          private TextView itemTextView;
          private TextView nameTextView;
          private TextView dateTextView;
  
          RecyclerViewHolder(View view) {
              super(view);
              itemTextView = (TextView) view.findViewById(R.id.firstLine);
              nameTextView = (TextView) view.findViewById(R.id.secondLine);
              dateTextView = (TextView) view.findViewById(R.id.longitude);
          }
      }
  }
  </code></pre>
  
  <p>and the <strong>ViewModel</strong></p>
  
  <pre><code>public class PlacesViewModel  extends AndroidViewModel {
    private final LiveData&lt;List&lt;PlaceSaved&gt;&gt; PlacedatabaseList;
  
    private PlaceDatabase appDatabase;
  
    public PlacesViewModel(Application application) {
      super(application);
  
      appDatabase = PlaceDatabase.getDatabase(this.getApplication());
  
      PlacedatabaseList = appDatabase.PlacedatabaseInterface().getAllItems();
    }
  
  
    public LiveData&lt;List&lt;PlaceSaved&gt;&gt; getPlaceList() {
      return PlacedatabaseList;
    }
  
    public void deleteItem(PlaceSaved placeSaved) {
      new deleteAsyncTask(appDatabase).execute(placeSaved);
    }
  
    private static class deleteAsyncTask extends AsyncTask&lt;PlaceSaved, Void, Void&gt; {
  
      private PlaceDatabase db;
  
      deleteAsyncTask(PlaceDatabase appDatabase) {
        db = appDatabase;
      }
  
      @Override
      protected Void doInBackground(final PlaceSaved... params) {
        db.PlacedatabaseInterface().delete(params[0]);
        return null;
      }
  
    }
  }
  </code></pre>
  
  <p>Now, I am trying to add an item to database with <code>OnClick</code> of a <code>fab</code> as:</p>
  
  <pre><code>public class PlacesActivity extends AppCompatActivity {
    private PlacesViewModel viewModel;
    private PlacesAdapter placesAdapter;
    private RecyclerView recyclerView;
    FloatingActionButton fab, fab1, fab2, fab3;
    LinearLayout fabLayout1, fabLayout2, fabLayout3;
    boolean isFABOpen = false;
    View fabBGLayout;
    PlaceDatabase db;
  
  
    @Override
    protected void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
      setContentView(R.layout.places_layout);
  
      viewModel = ViewModelProviders.of(this).get(PlacesViewModel.class);
  
      Runnable r =new Runnable() {
        @Override
        public void run() {
          recyclerView = findViewById(R.id.my_recycler_view);
          placesAdapter = new PlacesAdapter(new ArrayList&lt;PlaceSaved&gt;());//, this);
          recyclerView.setLayoutManager(new LinearLayoutManager(getApplication()));
          recyclerView.setAdapter(placesAdapter);
        }
      };
  
      Thread newThread = new Thread(r);
      newThread.start();
  
      fab1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          //Dialog
  /*
      Add location manually
  */
          AlertDialog.Builder placeLLDialog = new AlertDialog.Builder(PlacesActivity.this);
          LayoutInflater inflater = getLayoutInflater();
          final View view = inflater.inflate(R.layout.place_add_dialog, null);
  
          placeLLDialog.setView(view);
  
          final EditText todo = view.findViewById(R.id.placeN);
          final EditText time = view.findViewById(R.id.placell);
          final EditText longi = view.findViewById(R.id.placell2);
          placeLLDialog.setTitle("Add Place with Latitude and Longitude")
            .setPositiveButton("Add", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                  if (!todo.getText().toString().equals("") &amp;&amp;
                    !time.getText().toString().equals("") &amp;&amp;
                    !longi.getText().toString().equals("")) {
                      Snackbar.make(view, "Running", Snackbar.LENGTH_LONG).show();
  
                    /* HERE I AM TRYING TO ADD THE DATA, WHICH IS NOT WORKING
                     final PlaceSaved placeSaved = new PlaceSaved(todo.getText().toString(),
                      time.getText().toString(), longi.getText().toString());
                      AsyncTask.execute(new Runnable() {
                      @Override
                      public void run() {
                        db.databaseInterface().insertAll(placeSaved);
                        items = db.databaseInterface().getAllItems();
                        runOnUiThread(new Runnable() {
                          @Override
                          public void run() {
                            adapter = new PlacesAdapter(items, db, null);
                            adapter.notifyDataSetChanged();
                            recyclerView.setAdapter(adapter);
                            closeFABMenu();
                          }
                        });
                      }
                  });*/
                  }
                }
            })
            .setNegativeButton("Cancel", null);
  
          AlertDialog alertDialog = placeLLDialog.create();
          alertDialog.show();
          alertDialog.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE  | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
          alertDialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        }
      });
  </code></pre>
  
  <p>since I am very new to java, I can't find out how to add the data to the database, which is inside <code>fab1.setOnClickListner</code>.</p>
  
  <p>I will be grateful if someone kindly helps.</p>
  
  <p><strong>UPDATE</strong>
  I forgot to add database itself, here it is:</p>
  
  <pre><code>@Database(entities = {PlaceSaved.class},version = 1)
  public abstract class PlaceDatabase extends RoomDatabase {
    private static PlaceDatabase INSTANCE;
    public static PlaceDatabase getDatabase(Context context){
      if (INSTANCE == null){
        INSTANCE = Room.databaseBuilder(context.getApplicationContext(), PlaceDatabase.class,
            "places_db").build();
      }
      return INSTANCE;
    }
  
    public abstract DatabaseInterface PlacedatabaseInterface();
  </code></pre>