循环设置视图中的元素属性

敌花师 2014-09-11 02:59:27
我有一个LinearLayout,里面都是checkbox,我想把LinerLayout里面的checkbox都变成选中状态,我的思路就是循环得到这个LinearLayout中的所有元素,然后转成CheckBox ,设置属性,但是转化的时候就是报错,估计不应该这样做,我的代码如下,红色为报错区
LinearLayout ll01 = (LinearLayout) findViewById(R.id.LinearLayout01);
//循环赋值
for(int i=1;i<=ll01.getChildCount();i++)
{
CheckBox cbItem=(CheckBox)ll01.getChildAt(i);
cbItem.setChecked(false);

}
...全文
155 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
敌花师 2014-09-11
  • 打赏
  • 举报
回复
算了,我自己调吧,页面本身我也搞不定
lionfresh 2014-09-11
  • 打赏
  • 举报
回复
引用 14 楼 oBehavior 的回复:
[quote=引用 13 楼 lionfresh 的回复:] [quote=引用 8 楼 oBehavior 的回复:] [quote=引用 5 楼 lionfresh 的回复:] for(int i=1;i<=ll01.getChildCount();i++)应该是for(int i=0;i<ll01.getChildCount();i++)
我那种循环添加checkbox的方式,如果部门太多,直接有的就不见了,求教,LinearLayout不是会自适应高度么[/quote] 是会自适应高度,但是如果LinearLayout不是可滚动的话,超过手机屏幕的内容就不会显示了。[/quote]你的意思是换行?例如每4个一行, 我判断if(i%4)给checkbox添加什么属性呢?我一直没解决这个[/quote] 最外层一个LinearLayout,设置为垂直布局,然后每4个一行放在一个LinearLayout,这个LinearLayout设置为水平布局。
lionfresh 2014-09-11
  • 打赏
  • 举报
回复
引用 10 楼 oBehavior 的回复:
[quote=引用 8 楼 oBehavior 的回复:] [quote=引用 5 楼 lionfresh 的回复:] for(int i=1;i<=ll01.getChildCount();i++)应该是for(int i=0;i<ll01.getChildCount();i++)
我那种循环添加checkbox的方式,如果部门太多,直接有的就不见了,求教,LinearLayout不是会自适应高度么[/quote] 我的xml所有的域都是LinearLayout,为什么还是变成这样 [/quote] LinearLayout的默认布局是横向的,你设置成垂直方向的就是,android:orientation="vertical"
風言楓語 2014-09-11
  • 打赏
  • 举报
回复
你这是宽度问题 不是高度问题-.- 外面套个HorizontalScrollView把
敌花师 2014-09-11
  • 打赏
  • 举报
回复
引用 13 楼 lionfresh 的回复:
[quote=引用 8 楼 oBehavior 的回复:] [quote=引用 5 楼 lionfresh 的回复:] for(int i=1;i<=ll01.getChildCount();i++)应该是for(int i=0;i<ll01.getChildCount();i++)
我那种循环添加checkbox的方式,如果部门太多,直接有的就不见了,求教,LinearLayout不是会自适应高度么[/quote] 是会自适应高度,但是如果LinearLayout不是可滚动的话,超过手机屏幕的内容就不会显示了。[/quote]你的意思是换行?例如每4个一行, 我判断if(i%4)给checkbox添加什么属性呢?我一直没解决这个
lionfresh 2014-09-11
  • 打赏
  • 举报
回复
引用 8 楼 oBehavior 的回复:
[quote=引用 5 楼 lionfresh 的回复:] for(int i=1;i<=ll01.getChildCount();i++)应该是for(int i=0;i<ll01.getChildCount();i++)
我那种循环添加checkbox的方式,如果部门太多,直接有的就不见了,求教,LinearLayout不是会自适应高度么[/quote] 是会自适应高度,但是如果LinearLayout不是可滚动的话,超过手机屏幕的内容就不会显示了。
敌花师 2014-09-11
  • 打赏
  • 举报
回复
兄弟们,别纠结前面了,是我自己2了,没注意套用了LinearLayout,帮忙解决这个我动态添加checkbox的时候,高度问题
風言楓語 2014-09-11
  • 打赏
  • 举报
回复
引用 8 楼 oBehavior 的回复:
[quote=引用 5 楼 lionfresh 的回复:] for(int i=1;i<=ll01.getChildCount();i++)应该是for(int i=0;i<ll01.getChildCount();i++)
我那种循环添加checkbox的方式,如果部门太多,直接有的就不见了,求教,LinearLayout不是会自适应高度么[/quote] LinearLayout 高度设置为 android:layout_height="wrap_content"
敌花师 2014-09-11
  • 打赏
  • 举报
回复
引用 8 楼 oBehavior 的回复:
[quote=引用 5 楼 lionfresh 的回复:]
for(int i=1;i<=ll01.getChildCount();i++)应该是for(int i=0;i<ll01.getChildCount();i++)

我那种循环添加checkbox的方式,如果部门太多,直接有的就不见了,求教,LinearLayout不是会自适应高度么[/quote]
我的xml所有的域都是LinearLayout,为什么还是变成这样
風言楓語 2014-09-11
  • 打赏
  • 举报
回复
引用 4 楼 oBehavior 的回复:
[quote=引用 3 楼 u012456102 的回复:] 转型异常了 你看下LinearLayout ll01 = (LinearLayout)findViewById(R.id.LinearLayout01); 是否获取正确 是否是checkbox的直接父控件
我是采用代码循环添加的方式,源码如下 String deptCode = Tools.ReadXml(xml, "deptcodelist"); String deptName = Tools.ReadXml(xml, "deptnamelist"); String[] deptCodeList;// 部门编码集合 String[] deptNameList;// 部门名称集合 deptCodeList = deptCode.split(","); deptNameList = deptName.split(","); LinearLayout ll = (LinearLayout) this .findViewById(R.id.LinearLayout01); for (int i = 0; i < deptCodeList.length; i++) { LinearLayout checkboxLinearLayout = (LinearLayout) getLayoutInflater() .inflate(R.layout.checkboxdoitem, null); // 获得checkbox.xml文件中的CheckBox对象 CheckBox cbItem = (CheckBox) checkboxLinearLayout .findViewById(R.id.checkBoxItem1); cbItem.setTag(deptCodeList[i]); cbItem.setText(deptNameList[i]); ll.addView(checkboxLinearLayout, i); } 然后我修改代码为 LinearLayout ll01 = (LinearLayout) findViewById(R.id.LinearLayout01); //循环赋值 for(int i=1;i<=ll01.getChildCount();i++) { LinearLayout llItem=(LinearLayout)ll01.getChildAt(i); CheckBox cbItem=(CheckBox)llItem.getChildAt(i); cbItem.setChecked(cbItemCheck); } 报错log如下图 [/quote] 151行报空指针异常
敌花师 2014-09-11
  • 打赏
  • 举报
回复
引用 5 楼 lionfresh 的回复:
for(int i=1;i<=ll01.getChildCount();i++)应该是for(int i=0;i<ll01.getChildCount();i++)
我那种循环添加checkbox的方式,如果部门太多,直接有的就不见了,求教,LinearLayout不是会自适应高度么
lionfresh 2014-09-11
  • 打赏
  • 举报
回复
问题出在这里

LinearLayout checkboxLinearLayout = (LinearLayout) getLayoutInflater()
 .inflate(R.layout.checkboxdoitem, null);

 // 获得checkbox.xml文件中的CheckBox对象
CheckBox cbItem = (CheckBox) checkboxLinearLayout
 .findViewById(R.id.checkBoxItem1);
 cbItem.setTag(deptCodeList[i]);
 cbItem.setText(deptNameList[i]);
 ll.addView(checkboxLinearLayout, i);
你往II中添加的是LinearLayout,而不是CheckBox,当然无法将LinearLayout强制转换为CheckBox
敌花师 2014-09-11
  • 打赏
  • 举报
回复
引用 5 楼 lionfresh 的回复:
for(int i=1;i<=ll01.getChildCount();i++)应该是for(int i=0;i<ll01.getChildCount();i++)
我把i=0改了,忘了改i<=,所以有错,目前好用了
lionfresh 2014-09-11
  • 打赏
  • 举报
回复
for(int i=1;i<=ll01.getChildCount();i++)应该是for(int i=0;i<ll01.getChildCount();i++)
敌花师 2014-09-11
  • 打赏
  • 举报
回复
引用 3 楼 u012456102 的回复:
转型异常了 你看下LinearLayout ll01 = (LinearLayout)findViewById(R.id.LinearLayout01); 是否获取正确 是否是checkbox的直接父控件

我是采用代码循环添加的方式,源码如下
String deptCode = Tools.ReadXml(xml, "deptcodelist");
String deptName = Tools.ReadXml(xml, "deptnamelist");
String[] deptCodeList;// 部门编码集合
String[] deptNameList;// 部门名称集合
deptCodeList = deptCode.split(",");
deptNameList = deptName.split(",");
LinearLayout ll = (LinearLayout) this
.findViewById(R.id.LinearLayout01);
for (int i = 0; i < deptCodeList.length; i++) {
LinearLayout checkboxLinearLayout = (LinearLayout) getLayoutInflater()
.inflate(R.layout.checkboxdoitem, null);

// 获得checkbox.xml文件中的CheckBox对象
CheckBox cbItem = (CheckBox) checkboxLinearLayout
.findViewById(R.id.checkBoxItem1);
cbItem.setTag(deptCodeList[i]);
cbItem.setText(deptNameList[i]);
ll.addView(checkboxLinearLayout, i);
}
然后我修改代码为
LinearLayout ll01 = (LinearLayout) findViewById(R.id.LinearLayout01);
//循环赋值
for(int i=1;i<=ll01.getChildCount();i++)
{
LinearLayout llItem=(LinearLayout)ll01.getChildAt(i);
CheckBox cbItem=(CheckBox)llItem.getChildAt(i);
cbItem.setChecked(cbItemCheck);
}
报错log如下图
風言楓語 2014-09-11
  • 打赏
  • 举报
回复
转型异常了 你看下LinearLayout ll01 = (LinearLayout)findViewById(R.id.LinearLayout01); 是否获取正确 是否是checkbox的直接父控件
敌花师 2014-09-11
  • 打赏
  • 举报
回复
引用 1 楼 u012456102 的回复:
表示子控件索引是从0开始 你把log拿出来看下错误把

大神,图在次,我看不懂java的报错
風言楓語 2014-09-11
  • 打赏
  • 举报
回复
表示子控件索引是从0开始 你把log拿出来看下错误把

80,359

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧