动态表布局在onPause状态下中断

时间:2018-02-13 09:39:59

标签: android android-layout dialog

我正在一个项目中工作,我正在生成动态表数据。但是当活动进入onPause()状态时,表格布局中断了。附图。 enter image description here

Xml文件:

<ScrollView
            android:id="@+id/scrollViewTableLayoutReport"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/DetailReportId"
            android:layout_marginTop="30dp"
            android:layout_marginBottom="30dp">

            <RelativeLayout
                android:id="@+id/RelativeLayout1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="start"
                android:orientation="vertical">
                <TableLayout
                    android:id="@+id/tableLayoutReport"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:stretchColumns="0,1,2,3">
                </TableLayout>
            </RelativeLayout>

        </ScrollView>

和代码在这里,我生成一个对话框,当我单击对话框时,动态表数据正在显示。但是当活动进入onpause()状态后,表格布局会自动显示损坏。

runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //All report operation will be performed here
                generate_report.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {

                        // custom dialog
                        dialog = new Dialog(ReportingActivityMVP.this, R.style.PauseDialogTitle);
                        dialog.setContentView(R.layout.report_custom_dialog);
                        dialog.setTitle(getString(R.string.report));

                        myPreferences.setBackButtonReport("back_button_report");

                        //Showing Show all report
                        all_report = dialog.findViewById(R.id.all_report);
                        showAllReport();

                        ...........
..................
            }
        });



private void showAllReport() {
        all_report.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                backState = 1;
                topLinearLayout.setVisibility(View.GONE);
                ReportingViewLayout.setVisibility(View.VISIBLE);
                dialog.dismiss();

                ShapeDrawable border = new ShapeDrawable(new RectShape());
                border.getPaint().setStyle(Paint.Style.STROKE);
                border.getPaint().setColor(Color.BLACK);
                //Table Layout
                TableLayout stk = findViewById(R.id.tableLayoutReport);
                TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT);
                TableRow.LayoutParams tvParam = new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT);
                //Table Row
                TableRow tbrow0 = new TableRow(ReportingActivityMVP.this);

                //TextView
                TextView tv0 = new TextView(ReportingActivityMVP.this);
                tv0.setText(" Name ");
                tv0.setTextColor(Color.BLACK);
                tv0.setGravity(Gravity.CENTER);
                tv0.setPadding(5, 5, 5, 5);
                tv0.setBackground(border);
                tbrow0.addView(tv0);

                //TextView
                TextView tv1 = new TextView(ReportingActivityMVP.this);
                tv1.setText(" Phone ");
                tv1.setTextColor(Color.BLACK);
                tv1.setGravity(Gravity.CENTER);
                tv1.setPadding(5, 5, 5, 5);
                tv1.setBackground(border);
                tbrow0.addView(tv1);

                //TextView
                TextView tv2 = new TextView(ReportingActivityMVP.this);
                tv2.setText(" InTime ");
                tv2.setTextColor(Color.BLACK);
                tv2.setGravity(Gravity.CENTER);
                tv2.setPadding(5, 5, 5, 5);
                tv2.setBackground(border);
                tbrow0.addView(tv2);

                //TextView
                TextView tv3 = new TextView(ReportingActivityMVP.this);
                tv3.setText(" Out Time");
                tv3.setTextColor(Color.BLACK);
                tv3.setGravity(Gravity.CENTER);
                tv3.setPadding(5, 5, 5, 5);
                tv3.setBackground(border);
                tbrow0.addView(tv3);

                stk.addView(tbrow0);

                for (Visitor visitor : resultsVisitor) {
                    List<Visit> visits =  visitor.getVisits();

                    for (Visit visit : visits) {

                        TableRow tbrow = new TableRow(ReportingActivityMVP.this);

                        TextView t1v = new TextView(ReportingActivityMVP.this);
                        t1v.setText(visitor.getVisitorName());
                        t1v.setTextColor(Color.BLACK);
                        t1v.setGravity(Gravity.CENTER);
                        t1v.setPadding(5, 5, 5, 5);
                        t1v.setBackground(border);
                        tbrow.addView(t1v);

                        TextView t2v = new TextView(ReportingActivityMVP.this);
                        t2v.setText(visitor.getVisitorMobileNumber());
                        t2v.setTextColor(Color.BLACK);
                        t2v.setGravity(Gravity.CENTER);
                        t2v.setPadding(5, 5, 5, 5);
                        t2v.setBackground(border);
                        tbrow.addView(t2v);

                        TextView t3v = new TextView(ReportingActivityMVP.this);
                        t3v.setText(visit.getVisitInDateTime());
                        t3v.setTextColor(Color.BLACK);
                        t3v.setGravity(Gravity.CENTER);
                        t3v.setPadding(5, 5, 5, 5);
                        t3v.setBackground(border);
                        tbrow.addView(t3v);

                        TextView t4v = new TextView(ReportingActivityMVP.this);
                        if (visit.getVisitOutDateTime().equals(Config.LOGOUT_TIME)) {
                            t4v.setText("Still In");
                        } else {
                            t4v.setText(visit.getVisitOutDateTime());
                        }

                        t4v.setTextColor(Color.BLACK);
                        t4v.setGravity(Gravity.CENTER);
                        t4v.setPadding(5, 5, 5, 5);
                        t4v.setBackground(border);
                        tbrow.addView(t4v);

                        stk.addView(tbrow);
                    }
                }
            }
        });
    }

如何解决此问题?

1 个答案:

答案 0 :(得分:0)

最后我找到了问题的根本原因。当setBackground(border)设置边框时,则生成问题。所以,我删除了边框,问题已修复。