在android中,在表中动态添加表行并在对话框中显示
我能够创建一个对话框并在其中显示一个表,但我想动态地向其中添加更多的行 这些行中应该有文本视图(比如一行3个) 请告诉我这件事 提前谢谢 我的代码不起作用,如下所示: struct3x5是具有表布局和3x5维表的布局文件 1) 。点击事件代码在android中,在表中动态添加表行并在对话框中显示,android,dialog,Android,Dialog,我能够创建一个对话框并在其中显示一个表,但我想动态地向其中添加更多的行 这些行中应该有文本视图(比如一行3个) 请告诉我这件事 提前谢谢 我的代码不起作用,如下所示: struct3x5是具有表布局和3x5维表的布局文件 1) 。点击事件代码 button.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { final String NAMESPACE
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) \u创建表格的函数
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));
}
}
private void createtable(字符串[]数据,整数行计数){
//TODO自动生成的方法存根
LinearLayout t1=(LinearLayout)findViewById(R.id.tableLayout3x5);
//TextView[]tva=新的TextView[data.length];
整数计数=0;
对于(int i=0;i您可以按照以下方式动态添加更多行:
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));
大多数事情都可能是你想要的,例如你有没有尝试过发布一些代码?上面是我正在尝试的代码..我尝试了上面的想法,但仍然无法向表中添加行..请查看我的上面的代码您有没有尝试添加日志语句或单步执行代码,以确保它确实进入for l哎呀?