简体   繁体   中英

Set TableLayout programmatically to content_main.xml

I am new in android developing and have my problems by develop my UI what looks easy but doesn't work. I have a method for my App which should built me a new UI .

It should create a tableLayout with x table rows . I don't know before how many that are so I do it in Code. But I see nothing and get no Errors/Warnings.

Here my Code from MainActivity.java :

private void buildNewUI(PatientRepository repoPatient, ExternalSystemsRepository repoExtSys){

    TableLayout tableLayout = new TableLayout(this);
    tableLayout.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));
    tableLayout.setStretchAllColumns(true);

    Iterator patIterator = repoPatient.getIterator();

    while(patIterator.hasNext()){
        TableRow row = new TableRow(this);
        Patient patient = (Patient) patIterator.next();
        TextView outputLeft = new TextView(this);
        outputLeft.setText(patient.getSurname());
        row.addView(outputLeft);
        tableLayout.addView(row);
    }

}

Activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.patientfinder.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_main" />

</android.support.design.widget.CoordinatorLayout>


and content_main.xml:

    <?xml version="1.0" encoding="utf-8"?>
    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    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"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.patientfinder.MainActivity"
    tools:showIn="@layout/activity_main">
</TableLayout>

What is my mistake?

I'am sorry! Sometimes you find solution one minute later...

I gave my TableLayout in the context_main.xml the id TableLayout and then in Code I replaced creating new table layout with:

TableLayout layout = (TableLayout) findViewById(R.id.TableLayout);

Now it works and I see my Table!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM