【问题标题】:How to update the textview dynamically created textview in childlayout?如何在childlayout中更新textview动态创建的textview?
【发布时间】:2015-11-17 12:43:05
【问题描述】:
    LinearLayout  lay1 = (LinearLayout) findViewById(R.id.lay1);

        for(int i=0;i<list1.size();i++) {

                           View child = lay1.getChildAt(i);
                         LinearLayout  lay3 = (LinearLayout) child.findViewById(R.id.lay3);

                           for (j = 0; j <list2.size(); j++) {
                               View child1 = lay3.getChildAt(j);
      //                         View hiddenInfo11 = getActivity().getLayoutInflater().inflate(R.layout.add_inner_item, lay3 , false);
                               final TextView name = (TextView) child1.findViewById(R.id.name);
                               name.setText("new");
                           }
                       }

XML 布局:

                        <LinearLayout
                            android:id="@+id/lay1"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_margin="4dp"
                            android:orientation="horizontal">


                        </LinearLayout>

                        <LinearLayout
                            android:id="@+id/lay2"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_margin="4dp"
                            android:background="@color/white"
                            android:orientation="horizontal">

                        </LinearLayout>

add_item.xml

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


    <LinearLayout
        android:id="@+id/lay3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:orientation="vertical">

    </LinearLayout>


</LinearLayout>

add_inner_item.Xml

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

    <TextView android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="" />

</LinearLayout>

使用以下方法动态添加视图。

  for(int i=0;i<list.size();i++) {
                    View hiddenInfo = getActivity().getLayoutInflater().inflate(R.layout.add_item, lay, false);
                    LinearLayout lay3 = (LinearLayout) hiddenInfo.findViewById(R.id.lay3);
                    lay1.addView(hiddenInfo);

                    for (j = 0; j < list2.size(); j++) {


                            View hiddenInfo1 = getActivity().getLayoutInflater().inflate(R.layout.add_inner_item, lay3, false);

                            final TextView name = (TextView) hiddenInfo1.findViewById(R.id.name);


                            name.setText(list2.get(j).get("Name"));

                            lay3.addView(hiddenInfo1);

                    }

我有一个动态创建的布局,我想从按钮单击视图之外更新子文本视图。我已经尝试过这段代码。但没有任何改变。任何人都可以对此示例提出建议。

【问题讨论】:

  • 您在单击按钮时编写此代码?
  • yes.inside the button click 我写了这段代码。
  • 我成功创建了视图。但我无法更新子文本视图。实际上视图没有被调用。
  • 你在哪里定义你的文本视图?你没有在你的xml中提到它?请发布您的活动代码
  • 您能否发布将 TextViews 添加到布局的代码?我实际上对代码结构有点困惑。为什么不为每个只处理其子级的子级添加一个侦听器。我没有看到迭代每个孩子的理由。

标签: android dynamic view programmatically-created android-viewgroup


【解决方案1】:

您正在“lay1”容器内寻找“lay2”。根据您的 XML 布局,它们不是嵌套的,因此您将无法以这种方式找到它。改成这样:

LinearLayout  lay2 = (LinearLayout) findViewById(R.id.lay2);

你应该得到实际的lay2容器。我不确定文本视图是否存在,因为我已经看到了您创建视图的代码。

编辑:

我没有指定父级 - 您应该为此使用 Activity。类似的东西:

LinearLayout  lay2 = (LinearLayout) MyActivity.this.findViewById(R.id.lay2);

【讨论】:

  • 我添加了更多详细信息 - 请检查并让我知道它是否有效。 :)
  • 能否包含所有的 xml 布局(片段的那个,add_item,add_inner_item)。其中一个似乎不见了。
  • 嗯,看看你的编辑,一切看起来都很好。您能否验证您发布的代码(没有我的更改)不起作用。如果是这种情况,您能否调试应用程序并检查它是否完全进入 FOR 循环。
猜你喜欢
  • 2016-06-03
  • 1970-01-01
  • 1970-01-01
  • 2020-05-04
  • 2017-08-26
  • 1970-01-01
  • 2021-04-28
  • 1970-01-01
  • 2016-09-06
相关资源
最近更新 更多