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(R.id.tableLayout);

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

      

      ArrayList<Integer> mContactPhoneTypes = new ArrayList<Integer>();
      mContactPhoneTypes
            .add(ContactsContract.CommonDataKinds.Phone.TYPE_HOME);
      mContactPhoneTypes
            .add(ContactsContract.CommonDataKinds.Phone.TYPE_WORK);
      mContactPhoneTypes
            .add(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE);
      mContactPhoneTypes
            .add(ContactsContract.CommonDataKinds.Phone.TYPE_OTHER);

      Spinner mContactPhoneTypeSpinner = new Spinner(this);

      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
       
      Iterator<Integer> iter = mContactPhoneTypes.iterator();
        while (iter.hasNext()) {
            adapter.add(ContactsContract.CommonDataKinds.Phone.getTypeLabel(
                    this.getResources(),
                    iter.next(),
                    getString(R.string.undefinedTypeLabel)).toString());
        }
       
      mContactPhoneTypeSpinner.setAdapter(adapter);
      
      tr.addView(mContactPhoneTypeSpinner);

      /* 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,
                  LayoutParams.WRAP_CONTENT));

      params.span = 1;

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

      /* Add Button to row. */
      tr.addView(et);
      
      /* Create a Button to be the row-content. */
      TableRow.LayoutParams params2 = new TableRow.LayoutParams(
            new LayoutParams(LayoutParams.FILL_PARENT,
                  LayoutParams.WRAP_CONTENT));
      
      params2.span = 1;
      params2.width = 27;
      params2.height = 27;
      
      Button b = new Button(this);
        b.setText("-");
        b.setLayoutParams(params2);       
           
        b.setTag(counter);
       
      b.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
            onDeletePhoneButtonClicked();
         }
      });
       
        /* Add Button to row. */
        tr.addView(b);

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


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(R.id.tableLayout);
            
      //TableRow tr = tableLayout.
      
      new AlertDialog.Builder(this).setTitle("Removing").setMessage("View @ Index: " + b).setNeutralButton("Close", null).show();
      
      tableLayout.removeView(b);

      
   }


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

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 9 guests