【问题标题】:layout.getchildat() function does not work after update layout更新布局后 layout.getchildat() 函数不起作用
【发布时间】:2015-12-05 08:55:06
【问题描述】:

我有这样的代码:

protected void onActivityResult(int requestCode, int resultCode, Intent returnIntent) 
{
        super.onActivityResult(requestCode,resultCode, returnIntent);

        RelativeLayout layout2=(RelativeLayout)findViewById(R.id.RltLayout);
        Bundle extrasBundle = returnIntent.getExtras();

            String temp = extrasBundle.getString("FollowList");
            final int restSize;
            if(temp != null) {

                for(int i=0;i<count;i++)
                {
                    Button Bt=(Button) layout2.getChildAt(i) ;
                    CharSequence St= Bt.getText();
                    orders[i]=IndexOfLabel((String) St);
                }
                List<String> restList = Arrays.asList(temp.split(","));

                }
}

第一次,layout.getchildat() 完美运行。当我更改布局和视图时,我得到了以前的结果并且该功能不起作用。 问题是什么,我该如何解决。

【问题讨论】:

  • previous results and the function does not work. 是什么意思?你能再解释一下吗?
  • 例如,当我第一次运行此代码时,布局中有 3 个孩子,结果是关于这些孩子和那里的文本。之后,我在布局中添加了另外 2 个孩子并再次运行此代码。结果是一样的(只有 3 个孩子而不是 5 个孩子)
  • 你能多贴一点代码吗?
  • if ( layout.getchildat (i) instanceof Button ) { 获取文本 }
  • 亲爱的 Kunu,我更改了上面的代码。

标签: android layout updates children


【解决方案1】:

我解决了这个问题。实际上这是我的错,因为在重新排列视图之前我没有删除旧视图。通过添加以下代码并删除所有视图并重建布局,我解决了我的问题。

if(layout2.getChildCount() > 0) 
                        layout2.removeAllViews(); 

【讨论】:

    猜你喜欢
    • 2016-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-07
    • 1970-01-01
    相关资源
    最近更新 更多