Accessing a Dynamically Created TableRow

Put your problem here if it does not fit any of the other categories.

Accessing a Dynamically Created TableRow

Postby atomicx6637 » Sat Jan 22, 2011 12:50 am

Hello, I'm new to Android Programming but have developed in java before. Here is what I'm trying to do. I'm trying to multiple a rows with a Spinner, TextBox and a Button, similar to what the contacts entry does in the phone book. So far I can add the row with no problem but when I try to delete the row when the button is pushed I can seem to figure out how to access the TableRow element in order to delete it. I also cannot figure out how to access data in dynamically created input fields, but that is the second problem I have. Here is some of the code I have pieced together.

Code: Select all
private void onAddPhoneButtonClicked() {
      Log.v(TAG, "Add Phone button clicked");

      TableLayout tableLayout = (TableLayout) findViewById(;

      /* Create a new row to be added. */
      TableRow tr = new TableRow(this);
      //tr.setId("@+id/myRow" + counter);
      tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,


      ArrayList<Integer> mContactPhoneTypes = new ArrayList<Integer>();

      Spinner mContactPhoneTypeSpinner = new Spinner(this);

      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
      Iterator<Integer> iter = mContactPhoneTypes.iterator();
        while (iter.hasNext()) {

      /* Create a Edit Text to be the row-content. */
      EditText et = new EditText(this);

      TableRow.LayoutParams params = new TableRow.LayoutParams(
            new LayoutParams(LayoutParams.FILL_PARENT,

      params.span = 1;

      et.setLayoutParams(params); // causes layout update

      /* Add Button to row. */
      /* Create a Button to be the row-content. */
      TableRow.LayoutParams params2 = new TableRow.LayoutParams(
            new LayoutParams(LayoutParams.FILL_PARENT,
      params2.span = 1;
      params2.width = 27;
      params2.height = 27;
      Button b = new Button(this);
      b.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
        /* Add Button to row. */

      /* Add row to TableLayout. */
      tableLayout.addView(tr, counter, new TableLayout.LayoutParams(
            LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));

This is where I'm having troubles.

Code: Select all
   private void onDeletePhoneButtonClicked() {
      Log.v(TAG, "Delete Phone button clicked: " + b);

      TableLayout tableLayout = (TableLayout) findViewById(;
      //TableRow tr = tableLayout.
      new AlertDialog.Builder(this).setTitle("Removing").setMessage("View @ Index: " + b).setNeutralButton("Close", null).show();


Thanks for you help in advance.
Once Poster
Once Poster
Posts: 1
Joined: Sat Jan 22, 2011 12:43 am


Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 22 guests