Dynamically add table row in table and display it in dialog box in android

我是研究僧i 提交于 2019-12-12 04:13:54

问题


I am able to create a dialog box and show a table in it, but i wanted to add some more rows to it dynamically.

These rows should have textviews in it (say 3 in a row).

Please direct me on this.

Thanks in advance

My code, which is not working, is as follows:

struct3x5 is layout file with table layout and table of 3x5 dimension

1). On click event code

 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 to create table

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));
    }
}           

回答1:


You can do something along these lines to dynamically add more rows:

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));


来源:https://stackoverflow.com/questions/11799770/dynamically-add-table-row-in-table-and-display-it-in-dialog-box-in-android

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!