一般来说,我对匕首柄和依赖注入很陌生。我遵循了一些我能找到的例子,我设法用剑柄设置我的应用程序,它运行得很好。然而,我在我的一项活动中使用 serviceComponent 来提供一些数据库,据我所知,提供程序非常有用,因为它允许我在需要时初始化并提供我的数据库。我的问题是我不完全了解它现在是如何工作的。我希望能够更改我的数据库,但提供程序似乎只被调用一次,所以我可以这样做。这是我的模块:
@Module
@InstallIn(ServiceComponent::class)
object ServiceModule {
@ServiceScoped
@Provides
fun provideMusicDatabase() = MusicDatabase(listOfSong)
}
Run Code Online (Sandbox Code Playgroud)
这是第一次为我的活动提供音乐数据库,但是当我尝试更改我的歌曲列表时,我不知道如何通知我的服务某些内容已更改。所以我想知道是否有办法重新初始化/删除/清除 ServiceComponent,以便我可以更新我的数据库。或者也许我只是使用了错误的刀柄工具,因为我不太熟悉它?
另外我想通知一下,即使我完成我的活动并且它被破坏,当我重新打开活动时我的 ServiceComponent 也不会重新初始化。我猜这是正常行为,但我是新手,所以我真的不知道。所以也许我需要更改注释,以便在重新启动活动时它会重置。我还尝试将 MusicDatabase 提供程序的 @ServiceScoped 更改为 @ActivityScoped,但它不起作用。
所以也许我只是错过了刀柄的全部要点,或者我不知道如何使用它。但如果我能在这方面得到一些帮助,那就太好了。
我希望我的一些函数参数是可选的,所以我使用了默认参数,如下所示:
fun defaultparameter(param1: String = "", param2: String = "", param3: Int = 0)
Run Code Online (Sandbox Code Playgroud)
这项工作我可以做到这一点:
defaultparameter()
defaultparameter("titi")
defaultparameter("titi", "tata")
Run Code Online (Sandbox Code Playgroud)
我可以这样做,因为默认参数位于末尾,但是当我的参数位于中间时:defaultparameter("titi", 0)
这不起作用,它要求我提供param2.
我猜测编译器只能省略末尾的参数,但这正常吗?我知道当参数是相同类型时,编译器无法知道哪个是哪个,但这里我Int最后有一个类型,所以我认为它可以工作。
我的问题是:默认参数只在最后起作用?或者我错过了什么?有没有什么方法可以在不使用多态性和声明其他函数的情况下实现这一目标,只有两个参数:一String和一Int?
我正在尝试使用 jetpack compose 构建一个应用程序,但是当涉及到使用视图模型进行 api 调用时,我遇到了无限循环。该应用程序不断调用 api,我不明白为什么。这是我的视图模型:
class LibraryViewModel() : ViewModel() {
var library: ArrayList<PKIssue> = arrayListOf()
var loadLibrary by mutableStateOf(false)
init {
getLibrary()
}
fun getLibrary(){
viewModelScope.launch {
Press.issues(
result = object : result<ArrayList<Issue>, Error> {
override fun succeed(result: ArrayList<Issue>?) {
loadLibrary = true
if (result != null) {
library = result
}
}
override fun failed(error: Error?) {
loadLibrary = false
}
})
}
}
Run Code Online (Sandbox Code Playgroud)
但是,一旦我初始化 viewModel,我就会无限调用我的 api,以下是我尝试声明它的方式:
@SuppressLint("StateFlowValueCalledInComposition")
@Destination
@Composable
fun HomeScreen(
navigator: DestinationsNavigator,
libraryViewModel: LibraryViewModel = …Run Code Online (Sandbox Code Playgroud) 当我在音乐应用程序中播放歌曲时,我正在使用待处理的意图来创建通知栏。它曾经完美地工作,但我必须将我的 targetSdk 更新到 31 并且我遇到一个错误,告诉我我需要在endingIntent 中使用一些标志:
Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent.
Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, e.g. if it needs to be used with inline replies or bubbles.
Run Code Online (Sandbox Code Playgroud)
问题是我在声明待决意图时使用此标志:
val pendingIntent = PendingIntent.getActivity(
applicationContext,
0,
packageManager?.getLaunchIntentForPackage(packageName),
PendingIntent.FLAG_IMMUTABLE
)
Run Code Online (Sandbox Code Playgroud)
所以我不明白为什么它说我需要在我已经使用该标志时使用它?我错过了什么吗?
编辑
Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being …Run Code Online (Sandbox Code Playgroud) 我是 jetpack compose 的新手,我真的很喜欢它。但遇到了一个问题:我想创建一张卡片,在其中,我想显示一个项目列表,项目之间有分隔线。我几乎能够实现它,这是我的代码:
Box(modifier = Modifier.background(Color(0xFFf4f4f4))
.fillMaxSize()
.padding(top = 20.dp)) {
Card(
modifier = Modifier
.fillMaxWidth(),
shape = RoundedCornerShape(20.dp),
elevation = 5.dp
) {
val colorNamesList = listOf("Red", "Green", "Blue", "Indigo")
LazyColumn() {
itemsIndexed(
colorNamesList,
) { index, item ->
Column(
modifier = Modifier
.background(
color = Color(0xFFf2f2f2),
)
.clickable {
println(item)
},
horizontalAlignment = Alignment.CenterHorizontally,//those 2 does nothing
verticalArrangement = Arrangement.Center //when i change it nothing change
) {
println(item + index)
Text(
text = "Item at $item", …Run Code Online (Sandbox Code Playgroud) 我已经看到如何在 Java 中返回文件夹的大小:获取文件夹或文件的大小,但我在 Kotlin 中找不到它。
如何在 Kotlin 中获取文件夹的大小?