2014-02-20 44 views
0

我想設置每1列1圖像,但錯誤是指定的孩子已經有一個父。您必須先調用子對象的父對象的removeView()。你能給我一個簡單的解釋嗎?我能怎麼做?指定的孩子已經有一個父級。在表佈局

這是我的代碼

public class MybookActivity extends Activity{ 

private BooksDB db; 
private Context context; 
private HashMap< String, ArrayList<String>> hm; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.layout_mybook_sub2); 

    ArrayList<String> list_bokID = new ArrayList<String>(); 
    ArrayList<String> list_title = new ArrayList<String>(); 
    ArrayList<String> list_theme = new ArrayList<String>(); 
    hm = new HashMap<String, ArrayList<String>>(); 


    context = this; 
    db = new BooksDB(context); 
    hm = db.getBookTheme(); 
    if(hm.isEmpty() == true){ 
     System.out.println("NO data"); 

    }else{ 
     System.out.println("have data"); 
     list_bokID = hm.get("bokID"); 
     for (String bokID : list_bokID){ 
      System.out.println(bokID); 
     } 
     list_title = hm.get("title"); 
     for (String title : list_title) { 
      System.out.println(title); 

     } 
     list_theme = hm.get("theme"); 
     for (String themePath : list_theme) { 
      System.out.println(themePath); 
     } 
    } 

    int Theme_size = list_theme.size(); 
    //new 
    int numRow = Theme_size/2; 
    int numCol = 5; 

    TableLayout tblLayout = (TableLayout) findViewById(R.id.tblLayout); 

    for(int i = 0; i < numRow; i++) { 
     HorizontalScrollView HSV = new HorizontalScrollView(this); 
     HSV.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 

     TableRow tblRow = new TableRow(this); 
     tblRow.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); 
     tblRow.setBackgroundResource(R.drawable.newshell); 

     for(int j = 0; j < numCol; j++) { 
      ImageView imageView = new ImageView(this); 

      for (String string : list_theme) { 
       int res_id = getResources().getIdentifier(string, "drawable", getPackageName()); 
       imageView.setImageResource(res_id); 
       tblRow.addView(imageView,j); 
      } 
     } 

     HSV.addView(tblRow); 
     tblLayout.addView(HSV, i); 
    } 
} 




} 

謝謝

+0

顯示此XML代碼太layout_mybook_sub2 –

回答

0

您需要被添加(和任何視圖添加到ViewGroup中)每個圖像視圖的新實例。

移動實例創建內部的內循環:

for (String string : list_theme) { 
    ImageView imageView = new ImageView(this); 

    int res_id = getResources().getIdentifier(string, "drawable", getPackageName()); 
    imageView.setImageResource(res_id); 
    tblRow.addView(imageView,j); 
} 
+0

謝謝愛斯基摩人。但我想設置1每1列imageview.But現在1每個列的imageview。爲什麼是那個。 – user3001046

+0

我無法理解你。 – eski

相關問題