如何在已填充的LinearLayout中通过ID获取元素?
Got it, here's how you can grab the TableRow and TextView from your inflated layout and populate them with data from statZoneItem:
Inside your loop, right after inflating the LinearLayout l, use findViewById() on the inflated layout object (since it’s the root of your loader_total_stat_item layout) to locate the child views. Just replace the placeholder IDs below with the actual ones from your XML file:
LinearLayout root = (LinearLayout)findViewById(R.id.linearLayoutContainer); LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); final int size = grandTotalStatsRespond.getStatZoneItems().size(); for (int j = 0; j < size; j++) { StatZoneItem statZoneItem = grandTotalStatsRespond.getStatZoneItems().get(j); LinearLayout l = (LinearLayout) inflater.inflate(R.layout.loader_total_stat_item, null); // Find TableRow and TextView inside the inflated LinearLayout TableRow statTableRow = l.findViewById(R.id.your_table_row_id); // Replace with your actual TableRow ID TextView statTextView = l.findViewById(R.id.your_text_view_id); // Replace with your actual TextView ID // Set values from statZoneItem to the views statTextView.setText(statZoneItem.getYourTextValue()); // Example: populate text from your object // Add any other modifications to the TableRow or TextView here (like styling, visibility, etc.) root.addView(l); }
A quick tip: Even if the TextView is nested inside the TableRow, you can still use l.findViewById() directly to find it—no need to call it on the TableRow first (though you can if you want to be more explicit). Just make sure the IDs match exactly what’s defined in your loader_total_stat_item.xml layout file.
内容的提问来源于stack exchange,提问作者NoWar

