Отображается только одна динамически добавляемая таблица. - PullRequest
0 голосов
/ 15 ноября 2018

Я пытаюсь динамически добавлять TableRows в TableLayout.Я подклассифицировал TableRow в класс viewHolder, а затем накачал View из файла XML.Я вижу только последний созданный TableRow в результирующем пользовательском интерфейсе

Extended TableRow Class

public class CustomRow extends TableRow {

    private TextView columnData1;
    private TextView columnData2;
    private TextView columnDataN;

    public CustomRow(Context context, ViewGroup 
    parent) {
        super(context);
        // Inflating TableRow View Here
        View view = 
            LayoutInflater.from(context).
            inflate(R.layout.netdata_tablerow, 
            parent, true);
        view.setLayoutParams(parent.getLayoutParams());
        columnData1 = view.findViewById(R.id.columnData1);
        columnData2 = view.findViewById(R.id.columnData2);
        columnDataN = view.findViewById(R.id.columnData1N);

    public void bindData(Data data) {
       column1.setText(data.text1());
       ...
}

TableRow XML-файл, который надувается

<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" 
android:layout_height="wrap_content">


    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:layout_gravity="center"
        android:id="@+id/columnData1"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:layout_gravity="center"
        android:id="@+id/columnData2"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:layout_gravity="center"
        android:id="@+id/columnDataN"/>
</TableRow>

Activity_layout.xml СодержитTableLayout и некоторые статические заголовки

<LinearLayout 
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"
tools:context=".MainActivity"
android:orientation="vertical">

    <HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.9"
        android:fillViewport="true">


        <TableLayout
            android:id="@+id/tableLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="5dp"
            android:stretchColumns="*"
            android:orientation="vertical">

        <!--Header Row-->

        <TableRow
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:padding="5dp"
                android:text="HEADER1" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:padding="5dp"
                android:text="HEADER2" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:padding="5dp"
                android:text="HEADERN" />


        </TableRow>

        <!-- Dynamically Add Table Data Here -->

    </TableLayout>

MainActivity извлекает данные из списка и создает объекты CustomRow и добавляет их в TableLayout

public class MainActivity extends AppCompatActivity {


private final String TAG = MainActivity.class.getSimpleName();
private Button button;
private TableLayout tableLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

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

    NetworkDataList list = NetworkDataList.getNetworkList(this);

    for (int i = 0; i < list.size(); i++) {
        NetworkData item = list.getNetwork(i);
        CustomRow newRow = new 
            CustomRow(this, tableLayout);
        newRow.bindData(item);
        tableLayout.addView(newRow);
    }
}

Я вижу только один из отображаемых объектов CustomRow,последний, под строкой статического заголовка.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...