2013-04-30 21 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