请教
大佬,你定义了一个bindadapter toast,在xml中也使用了,可是该如何通过触发它呢?
大佬,我突然想到,在你的paoactivity中将调用dispatchFailure改为mViewModel.error.set(it),可以触发。我这种写法正确吗?
没什么意义吧
------------------ 原始邮件 ------------------ 发件人: "qtybpg"[email protected]; 发送时间: 2019年6月27日(星期四) 上午10:48 收件人: "ditclear/MVVM-Android"[email protected]; 抄送: "Subscribed"[email protected]; 主题: Re: [ditclear/MVVM-Android] 请教 (#6)
大佬,我突然想到,在你的paoactivity中将调用dispatchFailure改为mViewModel.error.set(it),可以触发。我这种写法正确吗?
— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.
好的,主要是我现在处于初学mvvm的状态,toast中传的this也是可以视为在view层传入的吗?之前一直在用mvc,弄的我现在有点懵了。
我认为所有涉及到Android系统的,比如activity、fragment、context 的东西都算是view层的,toast要用到context ,所以应该算是View层的东西
------------------ 原始邮件 ------------------ 发件人: "qtybpg"[email protected]; 发送时间: 2019年6月27日(星期四) 中午11:08 收件人: "ditclear/MVVM-Android"[email protected]; 抄送: "ditclear"[email protected]; "Comment"[email protected]; 主题: Re: [ditclear/MVVM-Android] 请教 (#6)
好的,主要是我现在处于初学mvvm的状态,toast中传的this也是可以视为在view层传入的吗?之前一直在用mvc,弄的我现在有点懵了。
— You are receiving this because you commented. Reply to this email directly, view it on GitHub, or mute the thread.
好的,大佬,之前mvc开发总是view层和model层混在一起,让我都懵了。
不太清楚的话,可以先看看 专栏 https://xiaozhuanlan.com/topic/7590648312
大佬,我目前正在参照你的项目写demo,看了你的前三篇后,有个疑问,你数据库的applicationcontext是自己新建了一个类继承了application吗?从而获得了applicationcontext。
并且这个applicatoncontext是个全局变量
一般来说是这样的,使用Koin之后,可以通过androidApplication()方法获取到,在PaoApp.kt里startKoin的时候就赋值了
的确,我看到了koin,也猜到是这种,不过我想试试不用依赖注入的组件。
fun getWeather()=local.getArticleById(8773).onErrorResumeNext( // Log.e("weather","no localweather") remote.getArticleById(8773) .doOnSuccess { Log.e("weather",it.content) local.insertArticle(it) }.doOnError{ Log.e("weather",it.message) }.doFinally { Log.e("weather","final")
}
)
大佬,我使用了你获得数据的api和同样的bean,可是并没有走doOn系列的方法。您能告诉我问题出现在哪吗?因为连log都没
val remote= Retrofit.Builder() .baseUrl("http://api.jcodecraeer.com/") .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build().create(WeatherService::class.java) val local =TestDataBase.getInstance(getAppConText()).getArticalDao();
val weatherRepo=WeatherRepo(remote,local);
fun getWeather(){
weatherRepo.getWeather()
.async(1000)
.doOnSuccess { result:Article->
result.let {
weatherdara.set(it.content)
}
}
你没有订阅
val weatherRepo=WeatherRepo(remote,local);
fun getWeather(){
weatherRepo.getWeather()
.async(1000)
.doOnSuccess { result:Article->
result.let {
weatherdara.set(it.content)
}
}.subscribe({},{})
嗯,好的,谢谢大佬。
大佬,当我打算插入新数据时,attempt to re-open an already-closed object: SQLiteDatabase:编译器给我推送了这个异常,网上搜到的都和我的情况不太相似。 abstract fun getPersonDao():PersonDao abstract fun getWeatherDao():WeatherDao abstract fun getArticalDao():PaoDao companion object {
@Volatile private var INSTANCE: TestDataBase? = null
fun getInstance(context: Context): TestDataBase =
INSTANCE ?: synchronized(this) {
INSTANCE ?: buildDatabase(context).also { INSTANCE = it }
}
private fun buildDatabase(context: Context) =
Room.databaseBuilder(context.applicationContext,
TestDataBase::class.java, "Test.db")
.build()
}
emmm,我卸载了以后再试就好使了,是因为我修改了我数据Bean的构造方法吗?导致表的结构对不上?
大佬,当我使用room数据库,想在表中插入list
@Embedded
public var weather:Weather?=null
@Relation(parentColumn = "id",entityColumn = "cityId",entity = WeatherData::class)
public var data:List<WeatherData>?=null;
}
@Entity(tableName = "weather") class Weather{ @PrimaryKey @ColumnInfo(name = "id") @NonNull private var cityid: String?=null @ColumnInfo(name = "updatetime") private var update_time: String? = null @ColumnInfo(name = "city") private var city: String? = null @ColumnInfo(name = "city_En") private var cityEn: String? = null @ColumnInfo(name = "country") private var country: String? = null @ColumnInfo(name = "country_En") private var countryEn: String? = null
fun getCityid(): String? {
return cityid
}
fun setCityid(cityid: String) {
this.cityid = cityid
}
fun getUpdate_time(): String? {
return update_time
}
fun setUpdate_time(update_time: String) {
this.update_time = update_time
}
fun getCity(): String? {
return city
}
fun setCity(city: String) {
this.city = city
}
fun getCityEn(): String? {
return cityEn
}
fun setCityEn(cityEn: String) {
this.cityEn = cityEn
}
fun getCountry(): String? {
return country
}
fun setCountry(country: String) {
this.country = country
}
fun getCountryEn(): String? {
return countryEn
}
fun setCountryEn(countryEn: String) {
this.countryEn = countryEn
}
}
@Entity class WeatherData{ @PrimaryKey @ColumnInfo(name = "day") @NonNull private var day: String?=null @ColumnInfo(name = "cityId") private var cityId:String ?=null @ColumnInfo(name = "date") private var date: String? = null @ColumnInfo(name = "week") private var week: String? = null @ColumnInfo(name = "wea") private var wea: String? = null @ColumnInfo(name = "tem1") private var tem1: String? = null @ColumnInfo(name = "tem2") private var tem2: String? = null @ColumnInfo(name = "tem3") private var tem3: String? = null
fun getDay(): String? { return day; }
fun setDay(day:String) { this.day=day; } fun getCityId(): String? { return cityId; } fun setCityId(cityid:String) { this.cityId=cityid; }
fun getDate():String? { return date; } fun setDate(date:String) { this.date=date; }
fun getWeek():String?{ return week; }
fun setWeek(week:String) { this.week=week; } fun getWea():String? { return wea; } fun setWea(wea:String) { this.wea=wea; }
fun getTem1():String?{ return tem1; }
fun setTem1(tem1:String) { this.tem1=tem1; } fun getTem2():String?{ return tem1; } fun setTem2(tem2:String) { this.tem2=tem2; } fun getTem3():String?{ return tem1; }
fun setTem3(tem3:String) { this.tem3=tem3; }
}
@Dao interface WeatherDao {
@Query("SELECT * FROM weather WHERE id = 101190401")
fun getWeatherById(): Single<CityAndWeather>
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertWeather(weather: CityAndWeather)
/**
* Delete all users.
*/
@Query("DELETE FROM weather")
fun deleteAllWeather()
@Update(onConflict = OnConflictStrategy.REPLACE)
fun updateWeather(CityAndWeather: Weather)
}
get set方法不需要的,把private 去掉. 另外,最好附上详细的错误信息
多谢大佬的指点,其实是我看的博客写的不全,在原作者的另一篇博客中已找到解决的方法,主要是我没加外键以及dao的原因
大佬,获取网上的数据,返回的却是空值,用的是嵌入的方式。 这是retrofit val remote= Retrofit.Builder() .baseUrl("https://www.tianqiapi.com/") .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build().create(WeatherService::class.java) 这是service interface WeatherService{ /** * 文章详情 */ @GET("api/") fun getWeather(@Query("cityid") id: String): Single<CityAndWeather> @GET("article_detail.php") fun getArticleById(@Query("id") id: Int): Single<Article>
} 这是实体类 class CityAndWeather{
@Embedded
public var weather:Weather?=null
@Relation(parentColumn = "id",entityColumn = "cityId",entity = WeatherData::class)
public var data:List<WeatherData>?=null;
}
@get("api/") fun getWeather(@query("cityid") id: String): Single<CityAndWeather>
打断点可以发现weather是null,而data里的item中cityid也为空 这是修改后的weather和weatherdata
@Entity(tableName = "weather") class Weather{ @PrimaryKey @ColumnInfo(name = "id") @NonNull public var cityid: String?=null @ColumnInfo(name = "updatetime") public var update_time: String? = null @ColumnInfo(name = "city") public var city: String? = null @ColumnInfo(name = "city_En") public var cityEn: String? = null @ColumnInfo(name = "country") public var country: String? = null @ColumnInfo(name = "country_En") public var countryEn: String? = null }
@Entity(foreignKeys = arrayOf(ForeignKey(entity = Weather::class, parentColumns = arrayOf("id"), childColumns = arrayOf("cityId"),onDelete = CASCADE)),tableName = "weatherdata") class WeatherData{ @PrimaryKey @ColumnInfo(name = "day") @NonNull public var day: String?=null @ColumnInfo(name = "cityId") public var cityId:String ?=null @ColumnInfo(name = "date") public var date: String? = null @ColumnInfo(name = "week") public var week: String? = null @ColumnInfo(name = "wea") public var wea: String? = null @ColumnInfo(name = "tem1") public var tem1: String? = null @ColumnInfo(name = "tem2") public var tem2: String? = null @ColumnInfo(name = "tem3") public var tem3: String? = null }
而如果将接口返回的数据修改为weather,却可以收到数据
https://www.tianqiapi.com/api/?cityid=shanghai 接口是有返回数据的,看你模型是否对的上
emm,如果说api返回的模型使用的是weather而不是CityAndWeather的话是能获取到的,不过weather是使用注解@embedded嵌入CityAndWeather中。所以获取不到数据。
打断点可以发现,当我使用嵌入的时候,同样的接口,模型为CityAndWeather时,嵌入的weather是null,而模型为Weather时,却能获取到数据 可是根据我对这篇博客的理解:https://blog.csdn.net/weixin_33785108/article/details/86842299 嵌入的实体应该是能获取到数据的,请大佬给我答疑一下,谢谢。 ------------------ 原始邮件 ------------------ 发件人: "D•IT•CLEAR"[email protected]; 发送时间: 2019年7月12日(星期五) 下午3:04 收件人: "ditclear/MVVM-Android"[email protected]; 抄送: "弃天永不破格"[email protected];"Author"[email protected]; 主题: Re: [ditclear/MVVM-Android] 请教 (#6)
https://www.tianqiapi.com/api/?cityid=shanghai 接口是有返回数据的,看你模型是否对的上
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.
对了,还有这篇博客,是同一个作者https://blog.csdn.net/weixin_34304013/article/details/86842302
返回的模型是这样的
按你现有的模型解析当然会不对,应该如下:
class CityAndWeather{
@PrimaryKey
@ColumnInfo(name = "id")
@nonnull
var cityid: String?=null
@ColumnInfo(name = "updatetime")
var update_time: String? = null
@ColumnInfo(name = "city")
var city: String? = null
@ColumnInfo(name = "city_En")
var cityEn: String? = null
@Relation(parentColumn = "id",entityColumn = "cityId",entity = WeatherData::class)
public var data:List<WeatherData>?=null;
}
按照大佬你的写法的确实能解析了,可是data里关联的外键仍然为null,因为data关联的表为Weather
------------------ 原始邮件 ------------------ 发件人: "D•IT•CLEAR"[email protected]; 发送时间: 2019年7月15日(星期一) 上午10:36 收件人: "ditclear/MVVM-Android"[email protected]; 抄送: "弃天永不破格"[email protected];"Author"[email protected]; 主题: Re: [ditclear/MVVM-Android] 请教 (#6)
返回的模型是这样的
按你现有的模型解析当然会不对,应该如下: class CityAndWeather{ @PrimaryKey @ColumnInfo(name = "id") @nonnull var cityid: String?=null @ColumnInfo(name = "updatetime") var update_time: String? = null @ColumnInfo(name = "city") var city: String? = null @ColumnInfo(name = "city_En") var cityEn: String? = null @Relation(parentColumn = "id",entityColumn = "cityId",entity = WeatherData::class) public var data:List<WeatherData>?=null; }
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.