Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding / Removing TableRows Dynamically
    primarykey
    data
    text
    <p>In my activity, I have a TableLayout where TableRows are added dynamically. </p> <p>This is done in a for loop like so... </p> <pre><code> for(i = 0; i &lt; orderArray.length(); i++) { getSaleData(i); //build the TextView's for each row... txtItemName = new TextView(this); itemNames.add(txtItemName); txtItemName.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT, 2f)); txtItemName.setTextSize(15); txtItemName.setMaxLines(1); txtItemName.setId(i); txtItemName.setEllipsize(TextUtils.TruncateAt.END); edtItemQty = new EditText(this); edits.add(edtItemQty); edtItemQty.setLayoutParams(new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f)); edtItemQty.setTextSize(15); edtItemQty.setInputType(InputType.TYPE_CLASS_NUMBER); itemNames.get(i).setText(currentItem); edits.get(i).setText(itemQuantity); //alternate between line colors depending on the flag if(lineColor) { itemNames.get(i).setBackgroundColor(Color.parseColor(LINE_COLOR)); edits.get(i).setBackgroundColor(Color.parseColor(LINE_COLOR)); lineColor = false; } else { lineColor = true; } //create a new TableRow and add the TextViews TableRow row = new TableRow(this); row.addView(itemNames.get(i)); row.addView(edits.get(i)); //add row to the table layout tblItems.addView(row, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); } </code></pre> <p>A resulting example looks like this...</p> <p><img src="https://i.stack.imgur.com/EIwm5.png" alt="enter image description here"></p> <p>Could you please advise on how I would go about removing these rows dynamically?</p> <p>Thanks</p> <p>EDIT: Decided to go with a longClickListener</p> <p>Here is the full code I ended up using...</p> <pre><code>longClickListener = new View.OnLongClickListener() { @Override public boolean onLongClick(View v) { //get the row final TableRow row = (TableRow)tblItems.findViewWithTag(v.getTag()); //confirm the deletion first... final AlertDialog.Builder confirm = new AlertDialog.Builder(TransactionEdit.this); confirm.setIcon(R.drawable.warningicon); confirm.setTitle("Remove Item?"); confirm.setMessage("Remove this item from the the order?"); confirm.setPositiveButton("OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { //find the matching row in the List for(int j = 0; j &lt; edits.size(); j ++) { //tags contain a prefix and a number e.g. qty5 (Same with rows) String qtyIndex = edits.get(j).getTag().toString().replace("qty", ""); String rowIndex = row.getTag().toString().replace("row", ""); //if its the same one... if(qtyIndex.equals(rowIndex)) { //...set the text to 0 to flag as not to be include... edits.get(j).setText((CharSequence)"0"); break; } } //...then hide the row row.setVisibility(View.GONE); } }); confirm.setNegativeButton("Cancel", null); confirm.show(); return false; } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload