0

我希望我得到這個時間回答我的問題有相同ID的膨脹多次TextViews或任何其他視圖之間的區別,我已經張貼在這個問題,但我想人所忽視它,因爲它沒有把正確的,這裏是我剛纔的問題How do I know the ID's of views if I am inflating a view multiple times in the same parent on button click? 我膨脹child.xml其中包含一個TableLayout 3個LinearLayouts這又包含TextView的在我的按鈕單擊事件的主要活動的鏈接,用戶可以添加儘可能多的他想要的意見。有沒有辦法從一個XML文件

但我很困惑,如何收集這些textviews或setOnClickListeners給他們的數據,因爲不同的觀點分享的TextView的這相同的ID。

人建議我用適當的類型追加它的膨脹視圖中使用findViewById,並存儲在列表中的參考,但我能夠設置一個OnClickListener僅用於充氣的第一個視圖。

我想知道如果它甚至有可能實現我在做什麼?在另一篇文章中,我讀到findViewById找到它可以找到的視圖的第一個可能的實例。這對我來說是一個問題。

請看看我的代碼片斷

TextView[] tv=new TextView[l.size()]; 
for(int i=0;i<tv.length;i++){ 
tv[i]=(TextView)l.get(i).findViewById(R.id.mdsnew_type_sp); 
tv[i].setOnClickListener(new View.OnClickListener() { 
@Override 
public void onClick(View v) { 
showDialog(DATE_DIALOG_ID); } 
}); 
//I have also tried this 
public void getData(){ 
for(int i=0;i<l.size();i++){ 
tv1=(TextView)l.get(i).findViewById(R.id.mdsnew_type_sp); 
    } 

但我能setOnClick監聽器僅在第TextView的,其餘都被忽略。

這裏是另一個鏈接,說明我如何吹我的活動和類似的情況,但得到的信息不事先說明有Inflating a view multiple times in the same parent when a button is clicked

任何幫助會得到回報,謝謝。

回答

1

由於這些TextViews已被添加到父ViewGroup(在你的情況下,LinearLayout),您可以遍歷子視圖父的名單,並添加OnClickListener到每一個:

for (int childPos = 0; childPos < myLayout.getChildCount(); childPos++) { 
    myLayout.getChildAt(childPos).setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // ... 
     } 
    }); 
} 
+0

如果我這樣做,然後我就可以編輯TextViews一次,如果我試圖回來到特定的TextView和嘗試的setText就可以了,然後在文本設置在LinearLayout中的最後一個TextView的?我現在應該做什麼的猜測? – 2011-12-17 15:04:09

1

這可能幫助

LanearLayout parentLayout = (LinearLayout)findViewById(R.id.main_ll); 
int count = parentLayout.getChildCount(); 
     for (int i = 0; i < count; i++) 
    { 
     View view = mainLl.getChildAt(i); 
     if (view instanceof TextView) 
      { 
       int id=view.getId(); 
      ((TextView) view).setText(" "); 

      } 
    }