Reputation: 65
i have created some dynamic textviews. I want to get the text of the textviews when the user clicks on the next button. I have written the logic to create the EditText in my Adapter class. I have a button in my activity on Click of the button i want to fetch all the text from the text views that was created dynamically in my adapter class.
if(input_type=="text")
{
holder.question.text=questionsList!![position].question
val tableRow = TableRow(context)
tableRow.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
var ansEditText = EditText(context)
ansEditText.setPadding(5,5,5,15)
ansEditText.setHint("Answer")
ansEditText.setTextColor(Color.parseColor("#000000"))
ansEditText.textSize= 20F
tableRow.addView(ansEditText)
holder.linearQuestionanire.addView(tableRow)
}
Upvotes: 0
Views: 66
Reputation: 3097
So basically you have model something like Question.
One option is add field answer in to that model and add listener on your EditText
onTextChange()
, after text has changed save changes to your Question.answer
field.
When using this approach you will have already mapped question-answer list.
Also Pair<Question, Answer?>
is good use case I think
it might look something like this
if (input_type == "text") {
val question = questionsList!![position]
holder.question.text = question.question
val tableRow = TableRow(context)
tableRow.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
var ansEditText = EditText(context)
ansEditText.setPadding(5, 5, 5, 15)
ansEditText.setHint("Answer")
ansEditText.setTextColor(Color.parseColor("#000000"))
ansEditText.textSize = 20F
tableRow.addView(ansEditText)
holder.linearQuestionanire.addView(tableRow)
ansEditText.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
question.answer = s.toString()
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
// Empty
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
// Empty
}
})
}
also don't forget to remove TextWatcher
so it would not cause memory leak
Upvotes: 1
Reputation: 4070
There are multiple ways you could approach this problem.
textView.getText()
.The most appropriate choice would obviously depend on your code, future considerations and other such criteria.
Upvotes: 0