java - android.widget.TableLayout 无法转换为 android.widget.TableRow

标签 java android android-tablelayout

在我的 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);

        }

    }
}

我像这样设置点击莱斯特纳。

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

如果有人愿意解释这个问题以及如何解决这个问题,我将不胜感激。

提前致谢。

最佳答案

// replace this line
TableRow tablerow = (TableRow)v.getParent();
            to
TableRow tablerow = (TableRow)v;

关于java - android.widget.TableLayout 无法转换为 android.widget.TableRow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19810598/

相关文章:

android - 在 Android 中运行序言

java - 在 Android 中像 Instagram 一样缩放 ImageView

android - 我该如何解决这个错误?方法 ID 不在 [0, 0xffff] : 65536

java - 如何更改进度对话框文本

java - 使用 Volatile 变量来安全发布 Immutable 对象

java - Tomcat : 503 Service Temporarily Unavailable 问题

android - 如何将表格布局垂直分成相等的部分?

java - 亚马逊 AWS RAID 配置

android - 以编程方式在 TableRow 中设置 TextView 引力

android - Android 中表格行项目的边框