android checkbox buttonview在for循环中使用时获取复选框的初始和最后一个id

android checkbox buttonview在for循环中使用时获取复选框的初始和最后一个id,android,layout,checkbox,android-alertdialog,android-inflate,Android,Layout,Checkbox,Android Alertdialog,Android Inflate,大宗报价 为包含tetview、复选框和add按钮的row_项目扩展主布局 private void show_weatherdialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); LayoutInflater li = getLayoutInflater(); 问题是什么?为什么当我选中第一个复选框时buttonView.getId()代码总是获取第一个和最后一个

大宗报价

为包含tetview、复选框和add按钮的row_项目扩展主布局

    private void show_weatherdialog() {

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater li = getLayoutInflater();

问题是什么?为什么当我选中第一个复选框时buttonView.getId()代码总是获取第一个和最后一个id?我试过调试这段代码,但似乎没有任何效果。很难从代码中看出为什么会发生这种情况(也许有人会发现)。但是,如果您使用的是允许调试的IDE(例如Eclipse),您可能会更快地得到答案。如果是这样,只需在switch语句之前放置一个断点,并查看它在何处调用(通过检查堆栈跟踪)。如果您尝试此操作但仍然找不到问题(或已经尝试过此操作),请在此处提供调试信息。
        View v = li.inflate(R.layout.custom_weather_layout, null);
        builder.setView(v);

        add = (Button) v.findViewById(R.id.addLocation);
        container = (LinearLayout) v.findViewById(R.id.container);

        //retrieving values from the database that has fields: id, location_id and location_title. currently there are 7 entries in the db.
        list = Dbhelper.getAllvalues();
        for (i = 0; i < list.size()/*7*/ ; i++) {
            view = LayoutInflater.from(this).inflate(R.layout.custom_weather_item_layout,
                    null);
            location_title = (TextView) view.findViewById(R.id.location_title);
            location_id = (TextView) view.findViewById(R.id.location_id);
            check = (CheckBox) view.findViewById(R.id.checkbox);
            //setting id for each checkbox according to loop
            check.setId(i);
            //checking the id in Log
            id = check.getId();
            Log.d("checkbox id", "" + id);

            location_title.setText(list.get(i).location_title);
            location_id.setText(list.get(i).location_id);
            System.out.println("ADDVIEW:");
            container.addView(view);

            check.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override


                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    switch (iid) {//THE BUTTONVIEW ALWAYS GETS ID 0 AND 6 when the first checkbox is checked
                        case 0:
                            //Kathmandu For the first location
                            a = list.get(id).location_id;
                            checkedvalue = buttonView.isChecked();
                            Log.d("SELECTED", "" + a + " " + checkedvalue);
                            if (checkedvalue) {
                                check.setChecked(true);
                                array.add(a);
                                Log.d("INSERTED INTO ARRAY", "true" + array);
                            } else {
                                check.setChecked(false);
                                array.remove(a);
                                Log.d("UNSELECTED", "true");
                                Log.d("DELETED FROM ARRAY", "true" + array);
                            }
                            break;

                        case 1:
                            //for second location
                            b = list.get(id).location_id;
                            checkedvalue = buttonView.isChecked();

                            Log.d("SELECTED", "" + b);
                            if (checkedvalue) {
                                check.setChecked(true);
                                array.add(b);
                                Log.d("INSERTED INTO ARRAY", "true" + array);
                            } else {
                                check.setChecked(false);
                                array.remove(b);
                                Log.d("UNSELECTED", "true" + array);
                                Log.d("DELETED FROM ARRAY", "true" + array);
                            }
                            break;

                        case 2:
                            //Bhaktapur ko lagi
                            c = list.get(id).location_id;
                            checkedvalue = buttonView.isChecked();
                            Log.d("SELECTED", "" + c);

                            if (checkedvalue) {
                                check.setChecked(true);
                                array.add(c);
                                Log.d("INSERTED INTO ARRAY", "true" + array);
                            } else {
                                check.setChecked(false);
                                array.remove(c);
                                Log.d("UNSELECTED", "true");
                                Log.d("DELETED FROM ARRAY", "true" + array);
                            }
                            break;
                        case 3:
                            //dhading ko lagi
                            d = list.get(id).location_id;
                            checkedvalue = buttonView.isChecked();
                            Log.d("SELECTED", "" + d);

                            if (checkedvalue) {
                                check.setChecked(true);
                                array.add(d);
                                Log.d("INSERTED INTO ARRAY", "true" + array);
                            } else {
                                check.setChecked(false);
                                array.remove(d);
                                Log.d("UNSELECTED", "true");
                                Log.d("DELETED FROM ARRAY", "true" + array);
                            }
                            break;
                        case 4:
                            //nuwakot ko lagi
                            e = list.get(id).location_id;
                            checkedvalue = buttonView.isChecked();
                            Log.d("SELECTED", "" + e);

                            if (checkedvalue) {
                                check.setChecked(true);
                                array.add(e);
                                Log.d("INSERTED INTO ARRAY", "true" + array);
                            } else {
                                check.setChecked(false);
                                array.remove(e);
                                Log.d("UNSELECTED", "true");
                                Log.d("DELETED FROM ARRAY", "true" + array);
                            }
                            break;
                        case 5:
                            //kavre ko lagi
                            f = list.get(id).location_id;
                            checkedvalue = buttonView.isChecked();
                            Log.d("SELECTED", "" + f);

                            if (checkedvalue) {
                                check.setChecked(true);
                                array.add(f);
                                Log.d("INSERTED INTO ARRAY", "true" + array);
                            } else {
                                check.setChecked(false);
                                array.remove(f);
                                Log.d("UNSELECTED", "true");
                                Log.d("DELETED FROM ARRAY", "true" + array);
                            }
                            break;
                        case 6:
                            //makwanpur ko lagi
                            g = list.get(id).location_id;
                            checkedvalue = buttonView.isChecked();
                            Log.d("SELECTED", "" + g);

                            if (checkedvalue) {
                                check.setChecked(true);
                                array.add(g);
                                Log.d("INSERTED INTO ARRAY", "true" + array);
                            } else {
                                check.setChecked(false);
                                array.remove(g);
                                Log.d("UNSELECTED", "true");
                                Log.d("DELETED FROM ARRAY", "true" + array);
                            }
                            break;
                        default:
                          break;

                    }
                    //adding the values of array in the loc_idlist arraylist to insert into another table in database
                    loc_idlist.addAll(array);
                    //Log.d("Arraylist ko values haru", "" + loc_idlist.size() + "" + loc_idlist);
                }
            });
        }

        dialog = builder.create();
        dialog.show();

        add.setOnClickListener(new View.OnClickListener() {//add the arraylist into the database
            @Override
            public void onClick(View v) {
                Log.d("Arraylist values: ", "" + loc_idlist.size() + "" + loc_idlist);
                //display the values of arraylist
                Toast.makeText(getApplication(), "Arraylist values:" + loc_idlist, Toast.LENGTH_SHORT).show();
                dialog.dismiss();

            }
        });
    }