2012-08-03 53 views
0

我能夠創建一個對話框並在其中顯示一個表格,但我想爲其動態添加更多行。在表中動態添加表格並將其顯示在對話框中android

這些行應該包含textviews(例如連續3個)。

請指導我。

預先感謝

我的代碼,這是不工作的,如下:

struct3x5是3x5的尺寸

1)的表的佈局和表佈局文件。點擊事件代碼

button.setOnClickListener(new OnClickListener() 
    { 

    public void onClick(View arg0) { 

      final String NAMESPACE = "http://tempuri.org/"; 
      final String METHOD_NAME = "method";  
      final String SOAP_ACTION = "http://tempuri.org/method"; 
      final String URL = "http://xyz/pqr/webserv.asmx"; 



      // custom dialog 
     final Dialog dialog = new Dialog(context,R.style.cust_dialog); 
      dialog.setContentView(R.layout.struct3x5); 
      dialog.setTitle("XYZ"); 


      // set the custom dialog components - text, image and button 
      TextView text1 = (TextView) dialog.findViewById(R.id.textView1); 
      text1.setText(""); 

      TextView text2 = (TextView) dialog.findViewById(R.id.textView2); 
      text2.setText("Vehicle(Rs in Lacs)"); 

      TextView text3 = (TextView) dialog.findViewById(R.id.textView3); 
      text3.setText("TPP(Rs in Lacs)"); 

      TextView text4 = (TextView) dialog.findViewById(R.id.textView4); 
      text4.setText("PL(Rs in Lacs)"); 

      TextView text5 = (TextView) dialog.findViewById(R.id.textView5); 
      text5.setText("Insurance(Rs in Lacs)"); 

      String [] data = {}; 
      String x = " "; 
      int colsize = 5; 
      int rowcount =(data.length)/colsize; 
      try { 


SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);    
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
envelope.dotNet = true; 
envelope.setOutputSoapObject(request); 

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL); 
        androidHttpTransport.call(SOAP_ACTION, envelope); 
        SoapObject response = (SoapObject)envelope.getResponse(); 
        data = getarray(response); 
       }    
       catch (Exception e){ 
        Toast.makeText(classname.this,"error",Toast.LENGTH_LONG).show(); 
            } 
      createtable(data,rowcount); 

      dialog.show(); 
     } 
     }); 

2)。 _Function創建表

private void createtable(String[] data, int rowcount) { 
    // TODO Auto-generated method stub 
    LinearLayout t1=(LinearLayout)findViewById(R.id.tableLayout3x5); 
    //TextView[] tva= new TextView[data.length]; 

    int count =0; 
    for(int i=0;i<rowcount;i++) 
    { 
     TableRow tr=new TableRow(this); 
    tr.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
     for(int j=0;j<(data.length/rowcount); j++) 
     { 
      TextView text = new TextView(this); 
      text.setText(data[count].toString()); 
    text.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL)); 
      tr.addView(text); 
      count++; 
     } 
     t1.addView(tr, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 
    } 
}   
+0

大多數事情都是可能的,你想和示例中有你所做的任何企圖是你可以發佈一些代碼? – bytebender 2012-08-03 16:37:01

+0

以上是我正在嘗試的代碼.. – krishnamahadik 2012-08-04 05:19:57

回答

0

你可以做下面幾行內容動態地添加更多行:

LinearLayout table = (LinearLayout)findViewById(R.id.my_container); 

TableRow text_row = new TableRow(this); 
text_row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 

// create new text row for label 
TextView text = new TextView(this); 
text.setText("My Text"); 
text.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); 
text_row.addView(text); 

table.addView(text_row, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT)); 
+0

我嘗試了上述想法,但仍然無法將行添加到表..請參閱我的上述代碼 – krishnamahadik 2012-08-04 05:19:25

+0

您是否嘗試過添加日誌語句或單步執行代碼以確保它是實際上進入for循環? – Nick 2012-08-06 16:02:37

相關問題