Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In practice, I have found that you need to be a bit careful, especially if you are using a bit of xml repeatedly. Suppose, for example, that you have a table that you wish to create a table row for each entry in a list. You've set up some xml:</p> <p>In <code>my_table_row.xml</code>:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;TableRow xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/myTableRow"&gt; &lt;ImageButton android:src="@android:drawable/ic_menu_delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/rowButton"/&gt; &lt;TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="TextView" android:id="@+id/rowText"&gt;&lt;/TextView&gt; &lt;/TableRow&gt; </code></pre> <p>Then you want to create it once per row with some code. It assume that you have defined a parent TableLayout myTable to attach the Rows to.</p> <pre><code>for (int i=0; i&lt;numRows; i++) { /* * 1. Make the row and attach it to myTable. For some reason this doesn't seem * to return the TableRow as you might expect from the xml, so you need to * receive the View it returns and then find the TableRow and other items, as * per step 2. */ LayoutInflater inflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = inflater.inflate(R.layout.my_table_row, myTable, true); // 2. Get all the things that we need to refer to to alter in any way. TableRow tr = (TableRow) v.findViewById(R.id.profileTableRow); ImageButton rowButton = (ImageButton) v.findViewById(R.id.rowButton); TextView rowText = (TextView) v.findViewById(R.id.rowText); // 3. Configure them out as you need to rowText.setText("Text for this row"); rowButton.setId(i); // So that when it is clicked we know which one has been clicked! rowButton.setOnClickListener(this); // See note below ... /* * To ensure that when finding views by id on the next time round this * loop (or later) gie lots of spurious, unique, ids. */ rowText.setId(1000+i); tr.setId(3000+i); } </code></pre> <p>For a clear simple example on handling rowButton.setOnClickListener(this), see <a href="https://stackoverflow.com/questions/5042841/onclicklistener-for-a-programatically-created-button">Onclicklistener for a programatically created button</a>.</p>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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