2010-11-27 106 views
1

我有一个TableLayout,其中我动态添加行,首先是空的,我想在用户点击时在该行中加载一个xml。膨胀一个TableRow

我已将OnClick方法分配给该行,但我不知道如何在进入onclick方法时加载xml。

public void onClick(View v){ 
// CODE TO LOAD THE XML 

Toast.makeText(getApplicationContext(), "This should appear", Toast.LENGTH_SHORT).show(); 
} 

我试过使用充气,但它似乎我没有正确使用它,因为应用程序崩溃时单击一行。

我该如何让该行包含xml?

回答

0

按ID查找TableRow中的TextView,然后将XML设置为TextView

8

当您最初添加到TableLayout时,首先为所有TableRow设置标签。因为已动态加载所述的TableRow,我希望,来自充气,像下面

TableRow inflateRow = (TableRow) View.inflate(Activity, R.layout.table_row, null); 

//Main TableLayout 

TableLayout tblAddLayout = (TableLayout) findViewById(R.id.tblAddLayout); 


for (int i=0;i<10;i++){ 

    inflate = (TableRow) View.inflate(myActivity, R.layout.table_row, null); 
    //set tag for each TableRow 
    inflate.setTag(i); 
    //add TableRows to TableLayout 
    tblAddLayout.addView(inflate); 
    //set click listener for all TableRows 
    inflateRow.setOnClickListener(this); 
} 

public void onClick(View v){ 

    String strTag = v.getTag().toString(); 
// Find the corresponding TableRow from the Main TableLayout using the tag when you click. 

    TableRow tempTableRow = (TableRow)tblAddLayout.findViewWithTag(strTag); 
    //then add your layout in this TableRow tempTableRow. 
} 

在上述的onClick方法可以使用标签加载XML文件。