充气通过tablerowtablerow

2023-09-06 22:08:29 作者:孤寂

我有一个TableLayout中,我先加行动态,空的,我想该行中加载XML时,其上的用户点击。

I have a TableLayout in which I add rows dynamically, empty at first and I want to load a xml in that row when the user clicks on it.

我分配OnClick方法对行,但我不知道如何当它在onclick方法进入加载XML。

I've assigned the OnClick method to the row, but I don't know how to load the xml when it enters in the onclick method.

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

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

我已经用充气尝试过,但似乎因为上一排,当点击应用程序崩溃,我不使用正确。

I've tried using inflate, but it seems I am not using it properly because the app crashes when clicking on a row.

我如何才能让该行包含XML?

How could I make the row to contain the xml?

推荐答案

首先为所有的TableRow的标签,当你添加到TableLayout开始。既然你已经动态加载了的TableRow,我希望,来自充气,像下面的

First set the tag for all TableRow when you add to TableLayout initially. since you have loaded the TableRow dynamically, i hope, that comes from inflate, like the following

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文件。

In the above onClick method you can load the xml file using the tag.

 
精彩推荐
图片推荐