Reputation: 2461
I have created an activity containing a tableview. The header and the table rows are added to the tableview dynamically through the activity. All are working fine, but I am not understanding how to add onClick listner to all rows except the header, and get the values of the columns in the particular row.
This is the full code of the activity:
public class ReportListActivity extends Activity {
TableLayout report_table;
TableRow tr_data;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_report_list);
report_table=(TableLayout) findViewById(R.id.report_table);
//---------------Table Header-----------------------------------------------
TableRow tr_head = new TableRow(this);
tr_head.setId(10);
tr_head.setBackgroundColor(Color.GRAY);
tr_head.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView label_sr_no = new TextView(this);
label_sr_no.setId(20);
label_sr_no.setText("S.No.");
label_sr_no.setTextColor(Color.WHITE);
label_sr_no.setPadding(5,5,5,5);
tr_head.addView(label_sr_no);// add the column to the table row here
TextView label_test_name = new TextView(this);
label_test_name.setId(21);// define id that must be unique
label_test_name.setText("Test Name"); // set the text for the header
label_test_name.setTextColor(Color.WHITE); // set the color
label_test_name.setPadding(5,5,5,5); // set the padding (if required)
tr_head.addView(label_test_name); // add the column to the table row here
TextView label_test_date = new TextView(this);
label_test_date.setId(21);// define id that must be unique
label_test_date.setText("Date"); // set the text for the header
label_test_date.setTextColor(Color.WHITE); // set the color
label_test_date.setPadding(5,5,5,5); // set the padding (if required)
tr_head.addView(label_test_date); // add the column to the table row here
report_table.addView(tr_head, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
//---------------Table Header-----------------------------------------------
//--------------------Table Body---------------------------
for (int i=1; i<=10; i++)
{
tr_data = new TableRow(this);
tr_data.setId(10);
tr_data.setBackgroundColor(Color.TRANSPARENT);
tr_data.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
TextView sr_no = new TextView(this);
sr_no.setId(20);
sr_no.setText(""+i);
sr_no.setTextColor(Color.BLACK);
sr_no.setPadding(5,5,5,5);
tr_data.addView(sr_no);// add the column to the table row here
TextView test_name = new TextView(this);
test_name.setId(21);// define id that must be unique
test_name.setText("Speed Test 60(min) Demo-ST-01"); // set the text for the header
test_name.setTextColor(Color.BLACK); // set the color
test_name.setPadding(5,5,5,5); // set the padding (if required)
tr_data.addView(test_name); // add the column to the table row here
TextView test_date = new TextView(this);
test_date.setId(21);// define id that must be unique
test_date.setText("12 Mar 2013"); // set the text for the header
test_date.setTextColor(Color.BLACK); // set the color
test_date.setPadding(5,5,5,5); // set the padding (if required)
tr_data.addView(test_date); // add the column to the table row here
report_table.addView(tr_data, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
}
//--------------------Table Body---------------------------
}
}
Here is the layout file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".ReportListActivity" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableLayout
android:shrinkColumns="0,1,2"
android:id="@+id/report_table" android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="match_parent">
</TableLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
How should I implement onclick listener to the table data except the header row and get the data of all columns of the particular row?
I followed this tutorial!
Upvotes: 2
Views: 1637
Reputation: 1803
You have to setOnClickListener to the Row Added Dynamically i.e. you have to add SetOnclickListener in For Loop where you are setting the Table Body dynamically
check below your edited code
//--------------------Table Body---------------------------
for (int i=1; i<=10; i++)
{
tr_data = new TableRow(this);
tr_data.setId(10);
tr_data.setBackgroundColor(Color.TRANSPARENT);
**tr_data.setOnClickListener(this);** //set on clickListener to Row here
}
hope this help you.
Upvotes: 0
Reputation: 13785
You have to put setOnClickListener
on tr_data and also all TextView final
final TextView test_name = new TextView(this);
test_name.setId(21);// define id that must be unique
test_name.setText("Speed Test 60(min) Demo-ST-01"); // set the text for the header
test_name.setTextColor(Color.BLACK); // set the color
test_name.setPadding(5,5,5,5); // set the padding (if required)
tr_data.addView(test_name); // add the column to the table row here
report_table.addView(tr_data, new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tr_data.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String test_name1= test_name.getText().toString();
Toast.makeText(getApplicationContext(), test_name1, Toast.LENGTH_LONG).show();
}
});
Upvotes: 1