单击按钮继续使用不同的数据更新同一活动

如何解决单击按钮继续使用不同的数据更新同一活动

我有一个android应用程序,它向用户询问一些问题,这是一项调查。每个问题都有一个YES和一个NO Button。因此,当用户按下YESNO按钮时,将在ButtonYES按钮上看到一个新的NO进入下一个问题。当用户单击下一个问题button时,它会将问题的textview更改为下一个问题文本,并且将imageView更改为该问题。

我正在使用DataBindingLiveData

我在list类中为以下问题创建了list个图像和另一个ViewModel

//list of images : Question 4 does not have an image
imgs= listOf(
        R.drawable.imgQ1,// 1
        R.drawable.imgQ2,// 2
        R.drawable.imgQ3)//3

//list of questions
qs= listOf(
   "What is .....?",//1
   "Who is ....?",//2
   "How to .....?",//3
   "Why is .....?")//4

我创建了一个ViewState来处理何时显示哪个按钮和UI:

sealed class MyUIState{

    data class UIState(
        val policyText: Boolean,// determines if the policy textview should be visible or not
        val nextQuestionButton: Boolean,// determines if the next question button should be visible or not
        val nextQuestionNewImage: Int = R.drawable.imgQ1,//what image is used for the next question
        val nextQuestion: String //what is the next question from the list
    ) : MyUIState()
}

我还有一个BindingAdapter类,用于处理可见性以及文本更改和图像更改 (基本上是这样-我通过数据绑定将这些功能添加到了我的布局文件中):

@BindingAdapter("btnNextQ")
fun Button.btnNextQ(myState: MyUIState?){

    visibility = if (myState is MyUIState.UIState) {
        if (state.nextQuestionButton)
            View.VISIBLE
        else
            View.GONE
    } else
        visibility
}

@BindingAdapter("newImage")
fun ImageView.newImage(myState: MyUIState?){

    if(myStateis MyUIState.UIState){
        setImageResource(state.nextQuestionNewImage)
    }
}

然后在我的ViewModel类中,我使用YESNO按钮上的click事件函数,在其中我这样称呼UIState:>

//I have this declared at the top of my class (global)
private val myViewState = MutableLiveData<MyUIState>()

//this the button click for yes:
fun YesClick(){

    myViewState.postValue(MyUIState.UIState(
        policyText= true,nextQuestionButton = true,nextQuestion =  qs[0])) //still keep the text for question1

}

现在,我希望在用户单击next question按钮之后更改图像和问题。因此,基本上发生的事情是,在单击下一个问题之后,next question按钮将变为不可见,而其他组件将保持YESNO按钮以及更新的问题和图像。这是我的下一个问题按钮上的click事件的代码:

fun nextQuestion(){

        myViewState .postValue(MyUIState.UIState(
            nextQuestionNewImage = imgs[1],nextQuestion = qs[1],nextQuestionButton = false,policyText= false))
    }

因此,状态的确会移至下一个问题,而必要的项目是可见的和不可见的,但我正在努力将其移至列表中的下一个问题。我可以从question1转到question2,但不确定在问题列表末尾之前如何继续这样做。

所以基本上,我只想继续使用新数据更新同一活动。

任何帮助或建议将不胜感激。

解决方法

不确定我是否理解您的真正问题。也许如果您共享更多代码,我们可以为您提供更好的帮助。

无论如何,我建议您为视图提供两种状态(针对您的情况),问题本身的一种状态包含yesno按钮,问题文本和正确的图像。另一个状态为“移至下一个”问题,其中包含问题文本,按钮以及您想要的任何内容。

然后,您的视图需要处理每个状态。在这种情况下,您需要处理一个状态,同时处理另一种状态,例如显示一个按钮并隐藏不需要的按钮。

因此,每当您单击yes按钮时,VM都会获取事件,检查所有内容,然后将事件以新状态发送回View,依此类推。

这与您所做的完全相同,但是使用两个状态将帮助您处理每个状态并简化代码

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 &lt;select id=&quot;xxx&quot;&gt; SELECT di.id, di.name, di.work_type, di.updated... &lt;where&gt; &lt;if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 &lt;property name=&quot;dynamic.classpath&quot; value=&quot;tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -&gt; systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping(&quot;/hires&quot;) public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate&lt;String
使用vite构建项目报错 C:\Users\ychen\work&gt;npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-