无法从片段内更新TableLayout

问题描述 投票:0回答:1

我正在开发一个应用,该应用可以让用户进行测验并记录所有测验分数,以便以后需要时参考。我正在构建一个片段,允许用户在TableLayout中查看此记录。我需要动态添加行,因此我试图创建一种在片段的onCreateView方法期间添加行的方法。但是,尽管这些行在调试器中显示为表的子级,但它们从未在应用程序中显示。我尝试过分离然后重新附加该片段,但这没有帮助。我非常感谢能够弄清楚这一点。

这是片段:

public class GalleryFragment extends Fragment {

    private GalleryViewModel galleryViewModel;

    public void addEntryToTable(TableLayout table, String user, String date, String score) {

        QuizHistoryTableRow row = new QuizHistoryTableRow(getContext(), user, date, score);
        table.addView(row);
    }

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_gallery, container, false);

        TableLayout table = root.findViewById(R.id.quiz_history_table);
        addEntryToTable(table, "user", "date", "score");

        return root;
    }
}

这是自定义表格行:

public class QuizHistoryTableRow extends TableRow {

    public QuizHistoryTableRow(Context context, String user, String date, String score){
        super(context);

        TextView userView = new TextView(context);
        userView.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
        userView.setText(user);
        userView.setTextColor(0);
        userView.setTextSize(22);

        TextView dateView = new TextView(context);
        dateView.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
        dateView.setText(date);
        dateView.setTextColor(0);
        dateView.setTextSize(22);

        TextView scoreView = new TextView(context);
        scoreView.setLayoutParams(new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 1f));
        scoreView.setText(score);
        scoreView.setTextColor(0);
        scoreView.setTextSize(22);

        addView(userView);
        addView(dateView);
        addView(scoreView);
    }

}
android android-studio android-fragments android-tablelayout
1个回答
1
投票

已解决,这是TableLayout.LayoutParamsTableRow.LayoutParams的问题。感谢Mike M.的回答。

已修改的片段:

public class GalleryFragment extends Fragment {

    private GalleryViewModel galleryViewModel;

    public TableRow getNewEntry(Context context, String user, String date, String score) {

        QuizHistoryTableRow row = new QuizHistoryTableRow(context, user, date, score);
        row.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        return row;
    }

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fragment_gallery, container, false);

        TableLayout table = root.findViewById(R.id.quiz_history_table);
        TableRow row = getNewEntry(getContext(), "user", "date", "score");
        table.addView(row, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
        return root;
    }

}

以及修改后的表格行:

public class QuizHistoryTableRow extends TableRow {

    public QuizHistoryTableRow(Context context, String user, String date, String score){
        super(context);

        TextView userView = new TextView(context);
        userView.setText(user);
        userView.setTextColor(Color.BLACK);
        userView.setTextSize(22);
        userView.setLayoutParams(new LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));

        TextView dateView = new TextView(context);
        dateView.setText(date);
        dateView.setTextColor(Color.BLACK);
        dateView.setTextSize(22);
        dateView.setLayoutParams(new LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));

        TextView scoreView = new TextView(context);
        scoreView.setText(score);
        scoreView.setTextColor(Color.BLACK);
        scoreView.setTextSize(22);
        scoreView.setLayoutParams(new LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 1f));

        addView(userView);
        addView(dateView);
        addView(scoreView);
    }

}
© www.soinside.com 2019 - 2024. All rights reserved.