2013-04-30 56 views
0

我在我的主要布局的按钮的onClick我重复使用以下方法另一个XML布局:获取视图的ID,而resuing XML布局

View view = layoutInflater.inflate(R.layout.single_elemnt, 
          createLayout, false); 
createLayout.addView(view, position); 
position++; 

其中single_elemnt是将被添加到布局当前主布局每次点击add按钮(出现在主布局中)。

single_elemnt有2视图:a textviewedittext。我每次使用tv = (TextView) findViewById(R.id.tv);时,点击了add

但问题是tv总是指第一textview即使我已经把findViewById代码中的onClick。

main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

     <!-- some other views --> 

      <Button 
       android:id="@+id/add" 
       android:layout_width="100dp" 
       android:layout_height="wrap_content" 
       android:layout_centerHorizontal="true" 
       android:layout_centerVertical="true" 
       android:gravity="center" 
       android:paddingTop="10dp" 
       android:text="Add" 
       android:textSize="20sp" /> 

</RelativeLayout> 

在活动

View view = layoutInflater.inflate(R.layout.single_elemnt, 
           createLayout, false); 
    createLayout.addView(view, position); 
    position++; 
tv = (TextView)profile.findViewById(R.id.tv); 
tv.setText("some text"); 

的onClick方法难道我做错了什么吗?任何帮助赞赏。

+0

请您发表您的代码'的onClick()'方法? – Vladimir 2013-04-30 08:04:42

+0

请检查编辑的问题。 – GAMA 2013-04-30 08:15:03

回答

1

你需要找到视图像这样的特定视图:

View view = layoutInflater.inflate(R.layout.single_elemnt, 
           createLayout, false); 
createLayout.addView(view, position); 
tv = (TextView) view.findViewById(R.id.tv); 
tv.setText("some text"); 

position++; 
+0

我该如何使用这个记住我的问题。还有另一个我正在使用的布局。所以,在你的答案中,而不是**查看**,我应该使用什么? – GAMA 2013-04-30 08:06:36

+0

onClick()你在做什么的按钮在哪里?你可以发布按钮及其相关代码吗? – 2013-04-30 08:09:10

+0

请切记编辑的问题。 – GAMA 2013-04-30 08:14:19