如何迭代动态创建的对象?

时间:2014-11-04 06:32:02

标签: java android object checkbox

假设您有一个这样的对象,并且您希望让程序遍历每个动态创建的复选框,以查看它是否未被检查过。

如果尚未检查,则程序应创建一个通知,提醒用户尚未检查这些对象中的一个或多个。

让程序识别是否选中复选框的最佳方法是什么?

每次运行程序时,它都只适用于上次创建的复选框,无论选中或未选中复选框的数量是多少。 谢谢你的时间。

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());

1 个答案:

答案 0 :(得分:1)

我会帮助你解释一下我的代码示例并尝试解释它,意识到我会照顾你(意味着你不能复制粘贴)因为我还有一些工作要做。

首先,每次点击按钮(spinner内)时都会创建一个新的动态onClick

        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++;

然后,我们可以通过以下方式访问这些动态Spinners:

    for(int i = 0; i < totalDynamicChild; i++)
    {
        Spinner s = (Spinner)findViewById(spinnderIdList.get(i));
        //do something with the spinner's object here
    }

如果您有任何疑问,请随时发表评论。