[英]Can I store data in SQlite database from Strings I've written inside the code?
我有一个应用程序,它将数据存储在数据库中,并在recyclerview中显示它们。 我的recyclerview应该包含图像。 我希望将图像URL存储在数据库中。 但我不想从用户那里获得URL。 我想自己在代码中编写URL,然后将其插入数据库中。 有没有一种方法可以从代码内部而不是任何编辑文本中将数据存储在数据库中?
不确定我是否理解正确,但是如果您需要编写用代码进行硬编码的字符串,只需将仅将此字符串作为常量的任何类制作为常数,然后在每次打开应用程序时将其写入数据库。 但这是不好的做法。 您可以只在该类中使用它。 像YourConstantsClass.yourConstant;
我写了这样的代码:
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.