2012-01-02 22 views
0

こんにちはクリックするたびにテーブルの行に新しいボタンを追加するボタンをクリックします。行に3つのボタンが追加された後、新しい表の行を動的に作成して新しいボタンを追加する必要があります。TableRowに動的にボタンを追加する

私はボタンをクリックすると、ボタンを使って行をテーブルに追加する方法を知っています。クリックするたびにテーブルの行を変更する方法がわからないので、追加のボタンを追加できます。

アドバイスは非常に役に立ち、高く評価されます。続き

は私のコードですが、これは事前に

public class DynamicTableView extends Activity { 

    TableLayout mTlayout; 
    String[] mTextofButton = { "Dipak", "E", "I", "J", "L", 
      "M", "G", "R", "N", "T", "H", "P", 
      "K", "Y", "V" }; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     mTlayout = (TableLayout) findViewById(R.id.mTlayout); 


     TableRow tr=new TableRow(this); 
     for(int i=0;i<mTextofButton.length;i++){ 
      Button btn=new Button(this); 
      btn.setText(mTextofButton[i]); 
      tr.addView(btn); 

     } 
     mTlayout.addView(tr); 
    } 
} 

おかげで完璧ではありません。

+0

あなたの問題は何ですか?あなたのコードがうまくいくように見えます。 –

+0

私のコードは動作しますが、ボタンは動的に追加され、上記の私の問題の説明を参照してください。 1行に3つのボタンだけを追加し、次のタブローを作成する方法。 –

+0

あなたは**単純な**コードフローに問題があるように見えます... – Selvin

答えて

10

の使用を使用することです: -

public class DynamicButtonsActivity extends Activity { 

    TableLayout mTlayout; 
    TableRow tr; 
    String[] mTextofButton = { "D", "E", "I", "J", "L", "M", "G", "R", "N", 
      "T", "H", "P", "K", "Y", "V" }; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     mTlayout = (TableLayout) findViewById(R.id.mTlayout); 

     int i = 0; 
     while (i < mTextofButton.length) { 
      if (i % 3 == 0) { 
       tr = new TableRow(this); 
       mTlayout.addView(tr); 
      } 
      Button btn = new Button(this); 
      btn.setText(mTextofButton[i]); 
      btn.setId(i); 
      btn.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View v) { 
        // TODO Auto-generated method stub 
        System.out.println("v.getid is:- " + v.getId()); 
       } 
      }); 
      tr.addView(btn); 
      i++; 
     } 
    } 
} 
1

ButtononClick()は、彼らのaddNewButton()を言うと、このようなものだろう:

public void addNewButton(View b) 
{ 
     for(int i=0; i<mTextofButton.length; i+=3){ 
       TableRow tr=new TableRow(this); 

       Button btnOne=new Button(this); 
       btnOne.setText(mTextofButton[i]); 
       tr.addView(btnOne); 

       Button btnTwo =new Button(this); 
       btnTwo.setText(mTextofButton[i+1]); 
       tr.addView(btnTwo); 

       Button btnThree =new Button(this); 
       btnThree.setText(mTextofButton[i+2]); 
       tr.addView(btnThree); 
       mTlayout.addView(tr); 
     } 
} 

が、あなたのmTextofButton.lengthにご注意を、それはそうあなたがArrayIndexOutOfBondExceptionを得るつもり3でdivisableべきであることを意味します。より良い方法は、コードの次ArrayList<String>

+0

私は3つのボタンを1つの行に追加して、次の表の行を作成しますが、私の質問は「それを行う方法」ですか? –

+0

私の編集された答えを見てください.. !! –

+0

あなたのコードは動作していますが、ダイナミックボタン用のリスナーを作成する方法。 –

4
int i=0; 
    while(i<mTextofButton.length){ 
      if(i%3==0){ 
       TableRow tr=new TableRow(this); 
       mTlayout.addView(tr); 
      } 
      Button btn=new Button(this); 
      btn.setText(mTextofButton[i]); 
      tr.addView(btn); 
      i++; 
    } 
関連する問題