shirkkan
shirkkan

Reputation: 124

Inflate a TableRow

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.

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.

How could I make the row to contain the xml?

Upvotes: 1

Views: 9440

Answers (2)

Durai
Durai

Reputation: 481

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.
}

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

Upvotes: 9

Xiaodi Huang
Xiaodi Huang

Reputation: 31

Find the TextView in the TableRow by ID, and then set the XML to the TextView.

Upvotes: 0

Related Questions