Reputation: 182
I am trying to display multiple textviews in an array. It should first show a hint to enter value and on click it allows you to enter value, once done, the screen shows the value with another field below to enter the next value
Header ---- Total
Header ---- Total
Header ---- Total
But the below code is not displaying the new textviews. The textview is clickable and should appear one below the other based on size of the array. Not sure why this code is not working. I using RelativeLayout for displaying the dynamic views
val itemTitle = arrayOfNulls<TextView>(size)
val itemValue = arrayOfNulls<TextView>(size)
for (k in invoiceItems!!.indices) {
val rec = invoiceItems!![k].itemTitle
Log.i("Re-ceived Item title", rec)
//Item Name Display on Invoice Activity
val layoutParams: RelativeLayout.LayoutParams = RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
) // or wrap_content
layoutParams.setMargins(0, 0, 0, 0)
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_START)
itemTitle[k] = TextView(this)
itemTitle[k]!!.hint = "Enter Item Title"
itemTitle[k]!!.textSize = 16f
itemTitle[k]!!.layoutParams = layoutParams
itemTitle[k]!!.isClickable = true
itemTitle[k]!!.isFocusable = true
itemTitle[k]!!.setTextColor(resources.getColor(R.color.txtcolor))
invoiceItemsLayout.addView(itemTitle[k])
itemTitle[k]!!.text = invoiceItems!![k].itemTitle
val value = invoiceItems!![k].itemTitle
Log.i("Item title value", value)
//Total Item Value
itemValue[k] = TextView(this)
val layoutParams2: RelativeLayout.LayoutParams = RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
) // or wrap_content
layoutParams2.setMargins(0, 0, 0, 0)
layoutParams2.addRule(RelativeLayout.ALIGN_PARENT_END)
if(k != 0){
layoutParams2.addRule(RelativeLayout.BELOW, (k-1))
}
itemValue[k]!!.text = "00.00"
itemValue[k]!!.textSize = 16f
itemValue[k]!!.setTextColor(resources.getColor(R.color.txtcolor))
itemValue[k]!!.setPadding(10, 0, 0, 10)
itemValue[k]!!.layoutParams = layoutParams2
invoiceItemsLayout.addView(itemValue[k], layoutParams2)
itemValue[k]!!.text = invoiceItems!![k].itemTotal.toString()
if(k == size) {
//New Item Title field
itemTitle[k + 1] = TextView(this)
val layoutParams: RelativeLayout.LayoutParams = RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
) // or wrap_content
layoutParams.setMargins(0, 0, 0, 0)
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_START)
itemTitle[k + 1]!!.hint = "Enter Item Title"
itemTitle[k + 1]!!.textSize = 16f
itemTitle[k + 1]!!.isClickable = true
itemTitle[k + 1]!!.isFocusable = true
itemTitle[k + 1]!!.setTextColor(resources.getColor(R.color.txtcolor))
invoiceItemsLayout.addView(itemTitle[k], layoutParams)
//New item value field
itemValue[k + 1] = TextView(this)
val layoutParams2: RelativeLayout.LayoutParams = RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
) // or wrap_content
layoutParams2.setMargins(0, 0, 0, 0)
layoutParams2.addRule(RelativeLayout.ALIGN_PARENT_END)
itemValue[k + 1]!!.text = "00.00"
itemValue[k + 1]!!.textSize = 16f
itemValue[k + 1]!!.setTextColor(resources.getColor(R.color.txtcolor))
itemValue[k + 1]!!.setPadding(10, 0, 0, 10)
itemValue[k + 1]!!.layoutParams = layoutParams2
invoiceItemsLayout.addView(itemValue[k], layoutParams2)
itemTitle[k + 1]!!.setOnClickListener {
updateInvoiceValues()
val intent = Intent(this, EditBillingItem::class.java)
intent.putExtra("invoiceItem", invoiceItems as Serializable)
intent.putExtra("index", k + 1)
intent.putExtra("invoiceEdit", invoiceEdit as Serializable)
this.startActivity(intent)
}
}
itemTitle[k]!!.setOnClickListener {
updateInvoiceValues()
val intent = Intent(this, EditBillingItem::class.java)
intent.putExtra("invoiceItem", invoiceItems as Serializable)
intent.putExtra("index", k)
intent.putExtra("invoiceEdit", invoiceEdit as Serializable)
this.startActivity(intent)
}
subTotal += invoiceItems!![k].itemTotal
val taxValue = invoiceItems!![k].itemRate!!
taxrate += (subTotal * taxValue)
}
invoiceSubValue.text = subTotal.toString()
total = subTotal + taxrate
invoiceTaxValue.text = taxrate.toString()
invoiceTotalValue.text = total.toString()
}
Upvotes: 0
Views: 56
Reputation: 2736
You need to set TextView's ID and use addRule(BELOW, textview_id) instead of "(k-1)" which means nothing for the Rule. Rules uses "IdRes" and not simple Integers.
Example (Java):
itemValue[k] = new TextView(this);
itemValue[k].setId(View.generateViewId());
...
layoutParams2.addRule(RelativeLayout.BELOW, itemValue[k].getId());
...
invoiceItemsLayout.addView(itemValue[k], layoutParams2);
View.generateViewId() secures that the generated ID will be UNIQUE and not already used by some other Views.
Upvotes: 1