假设您有一个像这样的对象,并且您希望程序遍历每个动态创建的复选框以查看它是否尚未被选中。
如果尚未检查,则程序应创建一条通知,提醒用户其中一个或多个对象尚未检查。
让程序识别复选框是否被选中的最佳方法是什么?
每次运行该程序时,它仅适用于最后创建的复选框,无论有多少个选中或未选中的复选框。 感谢您抽出时间。
View ObjectView;
CheckBox check;
//A whole bunch of code here.
public void onClick(View arg0) {
if (check==null){
}
else if (check==null || check.isChecked()){
}
else {
ObjectView.getId();
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(getActivity())
.setSmallIcon(android.R.drawable.stat_notify_more)
.setContentTitle("Items missing")
.setContentText("One or more items are missing");
int ID_Notify = 01;
getActivity();
NotificationManager managenote = (NotificationManager)getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
managenote.notify(ID_Notify, mBuilder.build());
最佳答案
我将帮助您使用我的代码示例并尝试解释它,请注意我不会会照顾您(意味着您不能只是复制粘贴),因为我还有一些工作要做。
首先,每次您单击按钮(在 onClick
内)时,都会创建一个新的动态 spinner
:
Spinner spinner = new Spinner(this);
spinner.setAdapter(spinChildAdapter);
parentSpinner.addView(spinner);
spinner.setId(totalDynamicChild); //the spinner's id will be the increment from 0
spinnderIdList.add(totalDynamicChild); //list of the dynamic spinner ID
totalDynamicChild++;
然后,我们可以通过以下方式访问这些动态 Spinner:
for(int i = 0; i < totalDynamicChild; i++)
{
Spinner s = (Spinner)findViewById(spinnderIdList.get(i));
//do something with the spinner's object here
}
如果您有任何疑问,请随时发表评论。
关于java - 如何迭代动态创建的对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26729035/