我有一个简单的DAO,包括CRUD功能
FeedEntryDAO.java
@Dao
public interface FeedEntryDAO {
@Query("SELECT * FROM feedEntrys")
LiveData<List<FeedEntry>> getAll();
@Query("SELECT * FROM feedEntrys WHERE uid = :uid LIMIT 1")
LiveData<FeedEntry> findByUid(int uid);
@Insert
void insertAll(FeedEntry... feedEntries);
@Delete
void delete(FeedEntry feedEntry);
@Update
int update(FeedEntry feedEntry);
}
Run Code Online (Sandbox Code Playgroud)
对于select,可以返回LiveData类型.
在Activity中,代码非常适合选择
viewModel.getFeedEntrys().observe(this,entries -> {...});
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试插入,更新,删除数据时.代码看起来有点难看,每次都会创建一个asynctask.
new AsyncTask<FeedEntry, Void, Void>() {
@Override
protected Void doInBackground(FeedEntry... feedEntries) {
viewModel.update(feedEntries[0]);
return null;
}
}.execute(feedEntry);
Run Code Online (Sandbox Code Playgroud)
我有2个问题:
感谢任何建议和意见.谢谢.
android android-room android-livedata android-architecture-components
我在房间里有一个实体
@Entity(foreignKeys ={
@ForeignKey(entity = Label.class, parentColumns = "_id", childColumns = "labelId", onDelete = CASCADE),
@ForeignKey(entity = Task.class, parentColumns = "_id", childColumns = "taskId", onDelete = CASCADE)
})
public class LabelOfTask extends Data{
@ColumnInfo(name = "labelId")
private Integer labelId;
@ColumnInfo(name = "taskId")
private Integer taskId;
}
Run Code Online (Sandbox Code Playgroud)
这个实体的sql语法如下
CREATE TABLE `LabelOfTask` (
`_id` INTEGER PRIMARY KEY AUTOINCREMENT,
`labelId` INTEGER,
`taskId` INTEGER,
FOREIGN KEY(`labelId`) REFERENCES `Label`(`_id`) ON UPDATE NO ACTION ON DELETE CASCADE ,
FOREIGN KEY(`taskId`) REFERENCES `Task`(`_id`) ON UPDATE NO ACTION …Run Code Online (Sandbox Code Playgroud) sqlite android database-design android-room android-architecture-components
我试图在100%Kotlin应用程序中实现ViewModel.我能找到的每一篇文档都说我想用它来获取ViewModel实例:
ViewModelProviders.of(this).get(CustomViewModel::class.java)
Run Code Online (Sandbox Code Playgroud)
根据文档,我应该能够导入:
import android.arch.lifecycle.ViewModelProviders
Run Code Online (Sandbox Code Playgroud)
但是这个导入尚未解决.我在我的构建文件中使用以下内容:
def androidArchVersion = '1.1.1'
implementation "android.arch.lifecycle:viewmodel:$androidArchVersion"
implementation "android.arch.lifecycle:livedata:$androidArchVersion"
annotationProcessor "android.arch.lifecycler:compiler:$androidArchVersion"
testImplementation "android.arch.core:core-testing:$androidArchVersion"
Run Code Online (Sandbox Code Playgroud)
为什么我不能访问ViewModelProviders?
android viewmodel gradle kotlin android-architecture-components
我有一个Activity包含3 RecyclerViews.我需要填充RecyclerViews来自远程存储库的数据(3个不同的请求).我可以使用多个ViewModels在Activity,或者有没有更好的解决办法(最佳实践).
我一直在使用库在我的代码中使用很多RxJava Observables转换.所以我想在RxJava Observable中添加一个扩展函数来轻松转换它们.LiveDataLiveDataReactiveStreams.fromPublisher()LiveData
这些是我的扩展功能:
fun <T> Flowable<T>.toLiveData() : LiveData<T> {
return LiveDataReactiveStreams.fromPublisher(this)
}
fun <T> Observable<T>.toLiveData(backPressureStrategy: BackpressureStrategy) : LiveData<T> {
return LiveDataReactiveStreams.fromPublisher(this.toFlowable(backPressureStrategy))
}
fun <T> Single<T>.toLiveData() : LiveData<T> {
return LiveDataReactiveStreams.fromPublisher(this.toFlowable())
}
fun <T> Maybe<T>.toLiveData() : LiveData<T> {
return LiveDataReactiveStreams.fromPublisher(this.toFlowable())
}
fun <T> Completable.toLiveData() : LiveData<T> {
return LiveDataReactiveStreams.fromPublisher(this.toFlowable())
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:
PS
我是Kotlin的新手,所以我问这些问题.任何有用的东西将不胜感激.非常感谢你.
android kotlin rx-java2 android-livedata android-architecture-components
我有兴趣尝试Android Studio中显示的导航图.但是在导入谷歌样本后,我的预览无法使用
我使用的是Android Studio 3.2 Preview Canary 16
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:startDestination="@+id/launcher_home">
<fragment
android:id="@+id/launcher_home"
android:name="com.android.samples.arch.componentsbasicsample.StartFragment"
android:label="Home">
<action
android:id="@+id/end_action"
app:destination="@id/end_dest" />
</fragment>
<fragment
android:id="@+id/end_dest"
android:name="com.android.samples.arch.componentsbasicsample.EndFragment"
android:label="End"
>
</fragment>
</navigation>
Run Code Online (Sandbox Code Playgroud)
18/6/2018更新:
即使我重建项目它也不起作用.但是如果添加新屏幕,则会在预览模式下显示新屏幕
android android-architecture-components android-studio-3.2 android-architecture-navigation
我是新架构组件WorkManager的新手,我通过Retrofit和RxJava进行API调用.
我的用例是从后端获取新帖子,然后显示通知,并更新小部件.
所以来自Worker类的doWork()方法中的代码可能看起来像这样.
@NonNull
@Override
public Result doWork() {
AppDependencies appDependencies = new AppDependencies((Application) getApplicationContext());
Repository repository = appDependencies.getRepository();
repository.getNewPosts()
.flatMap(newPosts -> repository.inserPosts(newPosts).toObservable())
.doOnError(Timber::e)
//if success - > return Result.SUCCESS,
// -> show notification
// -> update widget
// error-> return Result.Failure
.dontKnowWhatBestNextThing; //blocking or subscribing
//if we reached here then Retry
return Result.RETRY;
}
Run Code Online (Sandbox Code Playgroud)
我的问题是在Worker类中使用RxJava代码的正确方法是什么,因为doWork()方法有一个返回值,所以我必须使Rx代码同步.
如果我使用非阻塞Rx方法,我怎样才能返回值(成功 - 失败 - 重试)
android rx-java2 android-architecture-components android-jetpack android-workmanager
我正在尝试使用架构组件中的WorkManager.我已将compileSdkVersion和targetSdkVersion从27升级到28. gradle sync已成功完成.但是构建时错误不断涌现.由于'android.support:design',android.support库使用的是版本28.0.0-rc02.
我试图添加packagingOptions以排除'proguard/androidx-annotations.pro'.但它没有帮助.但是这次我得到了一个不同的错误信息:
Program type already present: com.google.common.util.concurrent.ListenableFuture
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚出了什么问题.
的build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'io.fabric'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.apps.test"
minSdkVersion 16
targetSdkVersion 28
versionCode 5
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
signingConfigs {
release
}
buildTypes {
release {
signingConfig signingConfigs.release
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
resValue "bool", "enableFirebase", "true"
}
debug {
minifyEnabled false
resValue "bool", "enableFirebase", "false"
}
}
}
dependencies {
implementation fileTree(include: ['*.jar'], …Run Code Online (Sandbox Code Playgroud) android kotlin android-architecture-components android-ktx android-workmanager
使用viewpager进行导航有什么用?我找不到有关此信息,也无法理解该如何做。
我有一个简单的两个片段,需要放入viewpager内,如果可能的话,可以通过导航。
在我学习协程以及如何在 android 应用程序中正确使用它们时,我发现了一些令我感到惊讶的事情。
在启动viewModelScope.launch { }lambda 中使用并设置断点启动协程时,我注意到我的应用程序不再响应,因为它仍在主线程上。
这让我感到困惑,因为文档viewModelScope.launch { }明确指出:
在不阻塞当前线程的情况下启动一个新的协程
当前线程不是主线程吗?如果默认情况下它不在不同的线程上运行,那么启动的全部目的是什么?
我能够viewModelScope.launch(Dispatchers.IO){ }在另一个线程上使用它按我的预期工作,即在另一个线程上运行它。
我试图通过该launch方法完成的是调用存储库并执行一些 IO 工作,即调用 Web 服务并将数据存储在房间数据库中。所以我想调用viewModelScope.launch(Dispatchers.IO){ }在不同的线程上完成所有工作,最后更新 LiveData 结果。
viewModelScope.launch(Dispatchers.IO){
liveData.postValue(someRepository.someWork())
}
所以我的第二个问题是,这是要走的路吗?
multithreading android coroutine android-architecture-components
android ×10
android-architecture-components ×10
kotlin ×3
android-room ×2
rx-java2 ×2
viewmodel ×2
android-architecture-navigation ×1
android-ktx ×1
coroutine ×1
gradle ×1
sqlite ×1