我可以将代码中编写的字符串中的数据存储到SQlite数据库中吗?

时间:2018-12-20 15:00:01

标签: java android sqlite android-studio

我有一个应用程序,它将数据存储在数据库中,并在recyclerview中显示它们。我的recyclerview应该包含图像。我希望将图像URL存储在数据库中。但我不想从用户那里获得URL。我想自己在代码中编写URL,然后将其插入数据库中。有没有一种方法可以从代码内部而不是任何编辑文本中将数据存储在数据库中?

2 个答案:

答案 0 :(得分:0)

不确定我是否理解正确,但是如果您需要编写用代码进行硬编码的字符串,则只需使用此字符串作为常量来创建任何类,然后在每次打开应用程序时将它们写入数据库。但这是不好的做法。您可以只在该类中使用它。像YourConstantsClass.yourConstant;

答案 1 :(得分:0)

我写了这样的代码:

 public void insertwall(ModelWallpaper modelWallpaper) {
        ContentValues c = new ContentValues();
        c.put(Colwall_Url,modelWallpaper.getImageURL());


    try {
        this.getWritableDatabase().insert(TABLE_NAME, null, c);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

和mainActivity中的

wallpaperHelper = new WallpaperHelper(this) ;

    wallpaperHelper.open();


    ModelWallpaper modelWallpaper = new ModelWallpaper();
    modelWallpaper.setImageURL("https://tpc.googlesyndication.com/pagead/imgad?id=CICAgKDb4cfN9wEQARgBMgjiJeT4IZ5nOw");

    wallpaperHelper.insertwall(modelWallpaper);

    wallpaperHelper.close();