检查是否存在来自布局充气机的视图,然后再添加一个视图 [英] check to see if a view exists from a layout inflater befor adding another one

查看:45
本文介绍了检查是否存在来自布局充气机的视图,然后再添加一个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的android项目中,我将表单动态添加到线性布局中,然后在完成按钮操作后销毁了它们.但是,当我单击添加按钮"时,尽管我一次只想要一个表单,但它无限地添加了更多表单.如何将我的linearLayout帐户"添加到视图中或当时是否存在于视图中?这是添加视图的代码.在添加视图之前,如何检查视图是否已经存在?

In my android project, I am dynamically adding forms to my linear layout and then destroying them when I am done with a button. However, When I click the "add button" It infinitely adds more forms although I want only one at a time. How can i chec if my linearLayout "accounts" has been added to the view or if it exists in the view at the time? This is the code to add the view. How can I check to see if the view already exists before I add the view?

  public void showForm(String form){
        View view;
        LayoutInflater inflater    
          =(LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.forms, null);
        LinearLayout item = (LinearLayout) view.findViewById(R.id.accounts);
        l.addView(item);
   }

推荐答案

选项1:使用boolean accountsAdded=false;,在需要时将其设置为true

Option #1: Use boolean accountsAdded=false;, setting it to true when needed

选项2:使用l.findViewById(R.id.accounts)并查看是否返回null

Option #2: Use l.findViewById(R.id.accounts) and see if that returns null

顺便说一句,如果R.layout.forms的根窗口小部件不是R.id.accounts View,您将崩溃,因此请通过addView()view而不是item添加到l.

BTW, you will crash if the root widget of R.layout.forms is not the R.id.accounts View, so please add view, not item, to l via addView().

这篇关于检查是否存在来自布局充气机的视图,然后再添加一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆