我想設置每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);
}
}
}
謝謝
顯示此XML代碼太layout_mybook_sub2 –