[英]How to Execute an activity N times ,Where N is user input
I wanted to execute a particular activity or a fragment which takes data and save it to database,and that activity executes N number of times.我想执行一个特定的活动或一个片段,它获取数据并将其保存到数据库中,并且该活动执行 N 次。 Here N(integer) is the given input from the user这里 N(integer) 是来自用户的给定输入
If A is an activity which executes activity B multiple time or N number of times, where N is the given input from the user.where activity B takes some user input data and saves in database N number of times.如果 A 是一个活动,它多次或 N 次执行活动 B,其中 N 是来自用户的给定输入。其中活动 B 获取一些用户输入数据并在数据库中保存 N 次。
Eg: Activity A ---> Enter the number of questions _____(8) Button[next]--Executes activity B例如:活动 A ---> 输入问题数量 _____(8) 按钮[下一步]--执行活动 B
Activity B----> Enter the 1st Question ______________________________ Button[Next] When i click Next button, it should save the first question and must repeat activity B 8 times Activity B----> Enter the 1st Question ______________________________ Button[Next] 当我点击 Next 按钮时,它应该保存第一个问题并且必须重复 Activity B 8 次
I believe you want a For loop.我相信你想要一个 For 循环。 Without knowing what code you have, I can only provide psuedo-code.不知道你有什么代码,我只能提供伪代码。
int userInput = getUserInput();
for(i = 0; i < userInput; i++) {
// Display the form
// User inputs data
// Write it to the database
}
provide a startActivity method with a parameter N for activity B, and use it to pass N from activity A to B, on click of the next button goto activity B again if N is greater than zero and finish current activity to avoid memory leaks and minus N by one each time, this should work, here is some kotlin code为活动 B 提供一个带有参数 N 的 startActivity 方法,并使用它来将 N 从活动 A 传递到 B,如果 N 大于零,单击下一个按钮再次转到活动 B 并完成当前活动以避免内存泄漏和减去每次 N 一个,这应该可以工作,这是一些 kotlin 代码
class ActivityB : AppCompatActivity() {
companion object {
@JvmStatic
fun start(context: Context, n: Int) {
val intent = Intent(context, ActivityB::class.java)
intent.putExtra("N", n)
context.startActivity(intent)
}
lateinit var n: Int
.
.
.
override fun onCreate(savedInstanceState: Bundle?) {
...
n = intent.getIntExtra("N", 0)
nextButton.setOnClickListener {
if( n > 0 ) {
ActivityB.startActivity(this, n-1)
} else {
// do what you need to do
}
activity?.finish()
}
...
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.