在会议室数据库中插入值 [英] Insert value in Room database

查看:71
本文介绍了在会议室数据库中插入值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下按钮单击将数据插入数据库:

I am trying to insert data to a database using button click as:

在我的 MainActivity 中,我正在使用:

In my MainActivity, I am using:

 package com.example.trial;

 public CityViewModel  mcityViewModel;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
 ImageButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        String cityName = cityNameWeakReference.getText().toString();
        City cityItem = new City(cityName, 5.0,4.0);
        Snackbar.make(view, cityName+cityItem, Snackbar.LENGTH_LONG)
            .setAction("Action", null).show();
        mcityViewModel.insert(cityItem);
      }
    });

为了更轻松地查看与数据库相关的代码,我在 com.example.trial.db 中定义了所有与数据库相关的文件(除了 DbActivity 中显示> recyclerview ).

For easier look at the database related code, I have defined all database related files in com.example.trial.db (except the DbActivity which shows the recyclerview).

我的实体

package com.example.trial.db

Entity(tableName = "city_table")
public class City {

  @NonNull
  @PrimaryKey
  @ColumnInfo(name = "city")
  private String city = "Atlantis";

  @NonNull
  @ColumnInfo(name = "latitude")
  private Double latitude = 0.0;

  @NonNull
  @ColumnInfo(name = "Longitude")
  private Double longitude = 0.0;

  public City(@NonNull String city, @NonNull Double latitude, @NonNull Double longitude) {
    this.city = city;
    this.latitude = latitude;
    this.longitude = longitude;
  }

  ... getter and setter for city, latitude and longitude ...
  ... not showing for brevity. If you think that is necessary, please ask...

package com.example.trial.db

@Dao
public interface CityDao{

  @Insert(onConflict = OnConflictStrategy.IGNORE)
  void insert(City city);

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

  @Delete
  void deleteCity(City city);

  @Query("SELECT * from city_table LIMIT 1")
  City[] getAnyCity();

  @Query("SELECT * from city_table ORDER BY city ASC")
  LiveData<List<City>> getAllCity();
}

ViewModel

package com.example.trial.db

public class CityViewModel extends AndroidViewModel {
    private CityRepository mRepository;
    private LiveData<List<City>> mAllCity;

    public CityViewModel(Application application) {
        super(application);
        mRepository = new CityRepository(application);
        mAllCity = mRepository.getAllCity();
    }

    public LiveData<List<City>> getAllCity() {
        return mAllCity;
    }

    public void insert(City city) { mRepository.insert(city); }
    public void deleteCity(City city) { mRepository.deleteCity(city); }
    public void deleteAll() {
        mRepository.deleteAll();
    }

}

现在,当我运行代码时,我得到了一个错误

Now, when I run the code, I am getting an error

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.trial.db.CityViewModel.insert(com.example.trial.db.City)' on a null object reference

snackbar 提供了一些值,这表明 cityItem 也不为null,但仍然出现此错误.

The snackbar is giving some value, which shows, cityItem is not null either, but still I am getting this error.

我对Java很陌生,并尝试从

I am very new to java and trying to build this from this codelabs. But I am unable to find out what I am doing wrong here.

请帮助.

推荐答案

您似乎没有在 onCreate(Bundle savedInstanceState)

请在 onCreate 方法中添加以下行:

Please add this line in onCreate method:

mcityViewModel = new ViewModelProvider(this).get(CityViewModel.class);

@Override
protected void onCreate(Bundle savedInstanceState) {
    // -------> Add this line here <-------
    mcityViewModel = new ViewModelProvider(this).get(CityViewModel.class);

    ImageButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        String cityName = cityNameWeakReference.getText().toString();
        City cityItem = new City(cityName, 5.0,4.0);
        Snackbar.make(view, cityName+cityItem, Snackbar.LENGTH_LONG)
            .setAction("Action", null).show();
        mcityViewModel.insert(cityItem);
      }
    });
}

这篇关于在会议室数据库中插入值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆