我有一个Activity beginwork ,然后A的布局是Begin_work,在这个Activity中添加了三个layout1,2,3,现在需要在beginwork这个activity创建的开始获得一个文本中的信息,并将它做成checkbox的内容添加到layout1中,但是怎么弄都不成功啊,现在确定数据是肯定取出来了,就是显示不出来,大神们帮帮忙啊!!!不胜感激
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//需要添加checkbox的layout
LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.lay1, null);
@SuppressWarnings("deprecation")
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
ArrayList<Dish> dishs = AddMenu.read("b.txt"); //取出数据
listener=new CheckBoxListener();
for (Dish dish : dishs) {
CheckBox checkBox = new CheckBox(this);
checkBox.setText(dish.getName());
linearLayout.addView(checkBox,layoutParams);
checkBox.setOnCheckedChangeListener(listener);
Log.i("add", checkBox.getText()+"");
}
setContentView(R.layout.begin_work);
InitImageView();
InitTextView();
InitViewPager();
}
然后是在这个activity中添加layout1,2,3
private void InitViewPager() {
mPager = (ViewPager) findViewById(R.id.vPager);
listViews = new ArrayList<View>();
LayoutInflater mInflater = getLayoutInflater();
View layout1 = mInflater.inflate(R.layout.lay1,null);
View layout2 = mInflater.inflate(R.layout.lay2,null);
View layout3 = mInflater.inflate(R.layout.lay3,null);
listViews.add(layout1);
listViews.add(layout2);
listViews.add(layout3);
mPager.setAdapter(new MyPagerAdapter(listViews));
mPager.setCurrentItem(0);
mPager.setOnPageChangeListener(new MyOnPageChangeListener());
------解决方案--------------------
你需要listViews.add(linearLayout);
而不是重新生成个view添加进去。