我是一个初学者学习coroutines
。
不完全是,但我对 a 是什么有一点了解coroutine
。
这suspend function
也很难,但需要一点理解。
我正在一步步学习,但有些地方我不明白。
那是suspendCoroutine
。在示例代码中,suspendCoroutine
和Continuation
在块中使用,但我不知道这两个是什么。
我看过其他网站,但找不到任何可以轻松解释的地方。
您能简单地解释一下suspendCoroutine
和 的Continuation
用途吗?如果可能的话,可以举个例子吗?
我使用的版本2.4.0-alpha04
是为了方便multi-backstack
使用navigation
. (开发文档)
但我收到以下错误:
Class 'ActionDialogToWriteRoutine' is not abstract and does not implement abstract member public abstract val actionId: Int defined in androidx.navigation.NavDirections
Run Code Online (Sandbox Code Playgroud)
我首先在这里搜索以修复错误,并发现具有相同错误的问题。(这里)
我在该链接中看到一个答案,说该错误已从 开始修复v2.4.0-alpha02
。
但我仍然遇到同样的错误。
所以我根据我的情况再次发布这个问题。
发生了什么?
构建.gradle[应用程序]
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-kapt'
// safe args
id 'androidx.navigation.safeargs.kotlin'
}
dependencies {
def lifecycle_version = "2.3.1"
def nav_version = "2.4.0-alpha04"
// AAC
implementation("androidx.fragment:fragment-ktx:1.3.4")
// Navigation Component
implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
}
Run Code Online (Sandbox Code Playgroud)
nav_graph.xml
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" …
Run Code Online (Sandbox Code Playgroud) 我正在研究ViewModel
将其应用到MVVM
设计模式中。
有一个使用方法by viemodels()
和一个ViewModelProvider.Factory
在视图模型创建中使用的方法。
by viewModels()
创建一个ViewModel object
.
ViewModelProvider.Factory
还创建Viewmodel objects
.
这两者有什么区别?
另外,在一些示例代码中,我看到注释中的代码3
,其中使用了by viewModels()
and factory
。这是什么意思?
class WritingRoutineFragment : Fragment() {
private val viewModel: WriteRoutineViewModel by viewModels() // 1
private lateinit var viewModelFactory: WriteRoutineViewModelFactory
// private val viewModel: WriteRoutineViewModel by viewModels(
// factoryProducer = { viewModelFactory } // 3.What does this code mean?
// )
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View? { …
Run Code Online (Sandbox Code Playgroud) 我正在寻找developer doc
要使用的最新版本Dagger Hilt
,但找不到与我在网上看到的版本类似的版本。
在使用的其他网络示例代码中hilt
,我添加了 hilt 版本2.28
或gradle,但我在开发人员文档2.41
中找不到任何类似的版本。
有什么改变吗?
我制作了一个如图所示的屏幕。
如您所见,它们Set information items
位于red box
.
我想将这些数据存储在Room DB
.
是red box
由 组成Workout Class
,其中包含blue box
( )。WorkoutSetInfo class
我只想从 Room 获取列表set information
。
但是我编写了一个查询来获取 的列表set information
,但出现以下错误:
error: The columns returned by the query does not have the fields [id,set,weight,reps] in com.example.lightweight.data.db.entity.WorkoutSetInfo even though they are annotated as non-null or primitive. Columns returned by the query: [sets]
public abstract androidx.lifecycle.LiveData<java.util.List<com.example.lightweight.data.db.entity.WorkoutSetInfo>> getWorkoutSetInfoList()
Run Code Online (Sandbox Code Playgroud)
我的类似问题已经有好几个了,但我还是没能解决。如何只获取设置信息的列表?
锻炼设置信息
data class WorkoutSetInfo(
val id: String …
Run Code Online (Sandbox Code Playgroud) 我在用bottom navigation
。
在切换的屏幕中,还有打开dialog fragment
.
我也为此使用了导航。
这是因为,一旦dialog
完成,data
就必须传递到打开对话框的屏幕。
我用的safe args
是这个。
但我遇到了与标题相同的错误。
我知道错误在哪里,但我不知道为什么会发生。
根据Stack Overflow上的搜索,有人遇到了和我一样的问题,但似乎并不是一个确切的解决方案,因为情况不同。
导航图
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph"
app:startDestination="@id/calendar">
<fragment
android:id="@+id/calendar"
android:name="com.example.writeweight.fragment.CalendarFragment"
android:label="fragment_calendar"
tools:layout="@layout/fragment_calendar" >
</fragment>
<fragment
android:id="@+id/list"
android:name="com.example.writeweight.fragment.WorkoutListFragment"
android:label="fragment_workout_list"
tools:layout="@layout/fragment_workout_list" />
<fragment
android:id="@+id/write"
android:name="com.example.writeweight.fragment.WritingRoutineFragment"
android:label="WritingRoutineFragment"
tools:layout="@layout/fragment_writing_routine">
<action
android:id="@+id/action_write_to_bodyPartDialog"
app:destination="@id/bodyPartDialog" />
<argument
android:name="title"
app:argType="string"
android:defaultValue="Write" />
</fragment>
<dialog
android:id="@+id/bodyPartDialog"
android:name="com.example.writeweight.fragment.BodyPartDialogFragment"
android:label="BodyPartDialogFragment"
tools:layout="@layout/fragment_body_part_dialog">
<action
android:id="@+id/action_bodyPartDialog_to_write"
app:destination="@id/write">
</action>
</dialog>
</navigation>
Run Code Online (Sandbox Code Playgroud)
主要活动
class MainActivity : AppCompatActivity() {
private lateinit var bottomNav: BottomNavigationView
private …
Run Code Online (Sandbox Code Playgroud) 我想将一个项目添加到 a List
of MutableLiveData
in中ViewModel
。
List
是read-only
因为它是用初始化的listOf()
。
但在特定情况下我想添加一个项目。
我以前是这样做的toMutableList()
,type cast
但是调试的结果是没有任何List
变化LiveData
。
如何将数据应用到List
a LiveData
?
class WriteRoutineViewModel : ViewModel() {
private var _items: MutableLiveData<List<RoutineModel>> = MutableLiveData(listOf())
val items: LiveData<List<RoutineModel>> = _items
fun addRoutine(workout: String) {
val item = RoutineModel(workout, "TEST")
_items.value?.toMutableList()?.add(item) // nothing change
}
}
Run Code Online (Sandbox Code Playgroud) 我使用navigation
和SafeArgs
到switch screens
和pass data
。
当在屏幕上选择数据时A
,数据在屏幕切换的同时传输。
但是,有时屏幕会在没有任何操作的情况下A
切换到屏幕B
。
null
我决定在这种情况下发送。
但我不断收到以下错误:
\nE/AndroidRuntime: FATAL EXCEPTION: main\n Process: com.example.lightweight, PID: 9555\n java.lang.RuntimeException: java.lang.reflect.InvocationTargetException\n at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:612)\n at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)\n Caused by: java.lang.reflect.InvocationTargetException\n at java.lang.reflect.Method.invoke(Native Method)\n at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)\n at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)\xc2\xa0\n Caused by: java.lang.reflect.InvocationTargetException\n at java.lang.reflect.Method.invoke(Native Method)\n at androidx.navigation.NavArgsLazy.getValue(NavArgsLazy.kt:54)\n at androidx.navigation.NavArgsLazy.getValue(NavArgsLazy.kt:35)\n at com.example.lightweight.fragment.WriteRoutineFragment.getArgs(WriteRoutineFragment.kt:20)\n at com.example.lightweight.fragment.WriteRoutineFragment.onViewCreated(WriteRoutineFragment.kt:44)\n at androidx.fragment.app.Fragment.performViewCreated(Fragment.java:2985)\n
Run Code Online (Sandbox Code Playgroud)\n导航.xml
\n<fragment\n android:id="@+id/writeRoutine"\n android:name="com.example.lightweight.fragment.WriteRoutineFragment"\n android:label="fragment_write_routine"\n tools:layout="@layout/fragment_write_routine" >\n <action\n android:id="@+id/action_writeRoutineFragment_to_workoutListTabFragment"\n app:destination="@id/workoutListTabFragment" />\n …
Run Code Online (Sandbox Code Playgroud) android android-navigation android-safe-args android-navigation-graph
但我对List collection
使用Arrays.asList
in感到困惑Java
。
当我将返回类型设置为 时List<String>
,asList
会引发错误。
在 的文档中asList
,返回类型是 List,为什么不允许呢?
中IDE
,解决办法是将返回类型改为MutableList<Array<String>>?
present..
MutableList<Array<String>>?
是什么意思
不仅仅是一个 MutableList,Mutable<ArrayList> 是什么意思?
List
, MutableList
, ArrayList
.. 我不知道适合哪个
枚举类
enum class BodyType(_resourceId: Int) {
CHEST(R.array.chest_workout_list),
BACK(R.array.back_workout_list),
LEG(R.array.leg_workout_list),
SHOULDER(R.array.shoulder_workout_list),
BICEPS(R.array.biceps_workout_list),
TRICEPS(R.array.triceps_workout_list),
ABS(R.array.abs_workout_list);
@ArrayRes
private val resourceId: Int = _resourceId
fun getResourceId() : Int = resourceId
}
Run Code Online (Sandbox Code Playgroud)
界面
interface WorkoutListSource {
fun getWorkoutListByPart(type: BodyType) : ArrayList<String>?
}
Run Code Online (Sandbox Code Playgroud)
WorkoutListLocalSource.kt
class …
Run Code Online (Sandbox Code Playgroud) android ×7
kotlin ×7
android-mvvm ×1
android-room ×1
callback ×1
collections ×1
coroutine ×1
dagger-hilt ×1
java ×1
suspend ×1