How do I know the ID's of views if I am inflating a view multiple times in the same parent on button click?

為{幸葍}努か 提交于 2020-01-26 01:47:49

问题


I have a difficult situation, let me put it this way, I want the user to use my 3 text-fields to set data for alarm and after filling in the information, I give him an option to set another alarm for this purpose I use a button. I have three table rows in a table layout which are defined in a child.xml file and every element has an Id set. In my activity, I inflate child.xml multiple times on button click event and I am able to successfully inflate it as many times as I want. But my problem is since I am inflating the same xml file many times(I don't know how many times) at runtime, I am confused as of how would I retrieve the data from particular text-fields like TextViews and identify them by a unique IDs since I am required to do so. How do you go about it?


回答1:


i will start from basic

<Layout>
<Button1>
</layout>

Now in activity

Button btn;View view1=null;
///Inflate file if view is null
first time when you inflating
view1=inflate
 btn=view1.findViewbyid();

Now suppose you have inflate another view but view1 value is not changed then btn willl contain reference for that button.And when it became visible.It will work as like normal button.




回答2:


I would go programmatically about it.

When the user requests for a new alarm, then you inflate your xml file. At this point, keep a reference to the inflated view (in a List, or some other sort of collection) in your activity.

So when you want to retrieve the data, don't use findViewById, just use your reference instead.




回答3:


may be this can help...


            LinearLayout ll=(LinearLayout) findViewById(R.id.i1);   ///main xml file which contains the layout i m inflating
            String str[]={"a","b","c"};
            View vi[]=new View[3];
            LayoutInflater li=(LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
            for(int i=0;i<3;i++){
                  View v=li.inflate(R.layout.addit, null);
                  TextView t=(TextView) v.findViewById(R.id.t1);
                  t.setText(str[i]);
                  ll.addView(v);
                      vi[i]=v;
            }
            TextView t1=new TextView(this);
            TextView t2=new TextView(this);
            TextView t3=new TextView(this);
            TextView r1=(TextView) vi[0].findViewById(R.id.t1);
            TextView r2=(TextView) vi[1].findViewById(R.id.t1);
            TextView r3=(TextView) vi[2].findViewById(R.id.t1);
            t1.setText(r1.getText());
            t2.setText(r2.getText());
            t3.setText(r3.getText());
            ll.addView(t1);
            ll.addView(t2);
            ll.addView(t3);

             setContentView(ll);

my addit.xml file contains the following :

       <?xml version="1.0" encoding="utf-8"?>
       <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/l2"
          android:layout_width="match_parent"
          android:layout_height="100dp"
          android:orientation="vertical" >

       <TextView
          android:id="@+id/t1"
          android:layout_width="fill_parent"
          android:layout_height="40dp"
          android:text="" />
       </TextView>
       </LinearLayout>

here i m adding dynamically the layout from addit.xml 3 times which contains the textview with id t1.. for each textview i m setting a text from the string array str .. and adding each view object v to an array for referencing ... now if i want to get the text for each textview i can use the reference to the view stored in the array and find the text view by id as in the code to get the text from each textview




回答4:


Just remember the View reference you get back when you inflate() each instance. Cast these to TextView when you need to read text from them.



来源:https://stackoverflow.com/questions/8488647/how-do-i-know-the-ids-of-views-if-i-am-inflating-a-view-multiple-times-in-the-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!