【问题标题】:Get element by ID within inflatered LinearLayout在膨胀的 LinearLayout 中按 ID 获取元素
【发布时间】:2018-10-15 20:43:45
【问题描述】:

如何在膨胀的 LinearLayout 中通过 ID 获取元素?

这是我的代码,它运行良好,我看到了创建的子 LinearLayouts。

LinearLayout root = (LinearLayout)findViewById(R.id.linearLayoutContainer);
LayoutInflater inflater;
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);

    // Here I need to find TableRow and TextView within LinearLayout  by its ID
    // and set values of statZoneItem object.                   

    root.addView(l);
}

【问题讨论】:

  • 如果列表很大,这可能会冻结您的 UI
  • @SantanuSur 感谢您的评论!我计划只使用大约 10-20 个项目。是不是太多了?
  • 我认为 just textviews 不会造成太大伤害,但最好用 recyclerView 替换它!
  • @SantanuSur 酷!谢谢,兄弟!

标签: java android android-linearlayout


【解决方案1】:

试试这个

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);

    // Here I need to find TableRow and TextView with LinearLayout  by its ID

      TableRow  tableRow = l.findViewById(R.id.tableRowID);
      TextView  textView = l.findViewById(R.id.textViewID);
      textView.setText("NILESH");
    // and set values from  statZoneItem object.                   

    root.addView(l);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-12
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多