在我的 android 应用程序中,有一个在运行时加载的表格布局。我需要为每一行设置 onclick 监听器。这是我的尝试:
if (lstComplainDetails.size() > 0) {
for (int i = 0; i < lstComplainDetails.size(); i++) {
final TableRow tr = new TableRow(getActivity());
tr.setTag(i);
tr.setLayoutParams(new TableRow.LayoutParams(
TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT));
final TextView txtActivityNo = new TextView(getActivity());
txtActivityNo.setTextSize(1, 12);
txtActivityNo.setTextColor(Color.BLACK);
/*txtActivityNo.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));*/
txtActivityNo.setText("" + lstComplainDetails.get(i).getComplainActivityCatregory() + "/" +lstComplainDetails.get(i).getActivityNo());
txtActivityNo.setPadding(10, 0, 12, 0);
tr.addView(txtActivityNo, cellLp);
final TextView txtOwner = new TextView(getActivity());
txtOwner.setTextSize(1, 12);
txtOwner.setTextColor(Color.BLACK);
/*txtOwner.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));*/
txtOwner.setText("" + lstComplainDetails.get(i).getExecutiveCode());
txtOwner.setPadding(10, 0, 12, 0);
tr.addView(txtOwner, cellLp);
final TextView txtComplain = new TextView(getActivity());
txtComplain.setTextSize(1, 12);
txtComplain.setTextColor(Color.BLACK);
/*txtComplain.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
txtComplain.setText("" + lstComplainDetails.get(i).getActivityText());*/
txtComplain.setPadding(10, 0, 12, 0);
tr.addView(txtComplain, cellLp);
final TextView txtType = new TextView(getActivity());
txtType.setTextSize(1, 12);
txtType.setTextColor(Color.BLACK);
/*txtType.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));*/
txtType.setText("" + lstComplainDetails.get(i).getActivityType());
txtType.setPadding(10, 0, 12, 0);
tr.addView(txtType, cellLp);
final TextView txtActivityDate = new TextView(getActivity());
txtActivityDate.setTextSize(1, 12);
txtActivityDate.setTextColor(Color.BLACK);
/*txtActivityDate.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));*/
txtActivityDate.setText("" + lstComplainDetails.get(i).getActivityDate());
txtActivityDate.setPadding(10, 0, 12, 0);
tr.addView(txtActivityDate, cellLp);
final TextView txtComplainText = new TextView(getActivity());
txtComplainText.setTextSize(1, 12);
txtComplainText.setTextColor(Color.BLACK);
/*txtComplainText.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));*/
txtComplainText.setText("" + lstComplainDetails.get(i).getComplaintDetail1());
txtComplainText.setPadding(10, 0, 12, 0);
tr.addView(txtComplainText, cellLp);
final TextView txtReferredTo = new TextView(getActivity());
txtReferredTo.setTextSize(1, 12);
txtReferredTo.setTextColor(Color.BLACK);
/*txtReferredTo.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));*/
txtReferredTo.setText("" + lstComplainDetails.get(i).getReferredTo());
txtReferredTo.setPadding(10, 0, 12, 0);
tr.addView(txtReferredTo, cellLp);
final TextView txtFollowUpDate = new TextView(getActivity());
txtFollowUpDate.setTextSize(1, 12);
txtFollowUpDate.setTextColor(Color.BLACK);
/*txtFollowUpDate.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));*/
txtFollowUpDate.setText("" + lstComplainDetails.get(i).getFollowUpRequiredBefore());
txtFollowUpDate.setPadding(10, 0, 12, 0);
tr.addView(txtFollowUpDate, cellLp);
final TextView txtAdditionalremarks = new TextView(getActivity());
txtAdditionalremarks.setTextSize(1, 12);
txtAdditionalremarks.setTextColor(Color.BLACK);
/*txtAdditionalremarks.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));*/
txtAdditionalremarks.setText("" + lstComplainDetails.get(i).getAdditionalRemarks());
txtAdditionalremarks.setPadding(10, 0, 12, 0);
tr.addView(txtAdditionalremarks, cellLp);
final TextView txtImplications = new TextView(getActivity());
txtImplications.setTextSize(1, 12);
txtImplications.setTextColor(Color.BLACK);
/*txtImplications.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));*/
txtImplications.setText("" + lstComplainDetails.get(i).getImplications());
txtImplications.setPadding(10, 0, 12, 0);
tr.addView(txtImplications, cellLp);
final TextView txtContactInfo = new TextView(getActivity());
txtContactInfo.setTextSize(1, 12);
txtContactInfo.setTextColor(Color.BLACK);
/*txtContactInfo.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));*/
txtContactInfo.setText("" + lstComplainDetails.get(i).getContactInfo());
txtContactInfo.setPadding(10, 0, 12, 0);
tr.addView(txtContactInfo, cellLp);
final TextView txtCompletedDetail1 = new TextView(getActivity());
txtCompletedDetail1.setTextSize(1, 12);
txtCompletedDetail1.setTextColor(Color.BLACK);
/*txtCompletedDetail1.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));*/
txtCompletedDetail1.setText("" + lstComplainDetails.get(i).getCompletedDetail1());
txtCompletedDetail1.setPadding(10, 0, 12, 0);
tr.addView(txtCompletedDetail1, cellLp);
final TextView txtCompletedDetail2 = new TextView(getActivity());
txtCompletedDetail2.setTextSize(1, 12);
txtCompletedDetail2.setTextColor(Color.BLACK);
/*txtCompletedDetail2.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));*/
txtCompletedDetail2.setText("" + lstComplainDetails.get(i).getCompletedDetail2());
txtCompletedDetail2.setPadding(10, 0, 12, 0);
tr.addView(txtCompletedDetail2, cellLp);
final TextView txtProductName = new TextView(getActivity());
txtProductName.setTextSize(1, 12);
txtProductName.setTextColor(Color.BLACK);
/*txtProductName.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));*/
txtProductName.setText("" + lstComplainDetails.get(i).getProductName());
txtProductName.setPadding(10, 0, 12, 0);
tr.addView(txtProductName, cellLp);
tr.setBackgroundDrawable(getResources().getDrawable(
R.drawable.table_shape));
tr.setVisibility(View.VISIBLE);
tr.setClickable(true);
tr.setOnClickListener(trOnClickListener);
tblComplnDetails.addView(tr, rowLp);
}
}
}
我以这种方式设置了 click lestener。
private OnClickListener trOnClickListener = new OnClickListener() {
public void onClick(View v) {
v.setBackgroundColor(Color.GRAY);
bCancel.setEnabled(true);
bHistory.setEnabled(true);
//trID = v.getId();
TableRow tablerow = (TableRow)v.getParent();
TextView tvNo = (TextView) tablerow.getChildAt(0);
TextView tvDate = (TextView) tablerow.getChildAt(4);
TextView tvFollowUp = (TextView) tablerow.getChildAt(7);
TextView tvActivity = (TextView) tablerow.getChildAt(2);
TextView tvComplain = (TextView) tablerow.getChildAt(5);
no = tvNo.getText().toString();
date = tvDate.getText().toString();
folloeUp = tvFollowUp.getText().toString();
activity = tvActivity.getText().toString();
complain = tvComplain.getText().toString();
}
};
但是 logcat 说:
11-06 11:01:01.922: E/AndroidRuntime(21996): java.lang.ClassCastException: android.widget.TableLayout cannot be cast to android.widget.TableRow
如果有人能如此友好地解释这个问题以及如何解决这个问题,我将不胜感激。
提前致谢。