0
    private void show_weatherdialog() {

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

Blockquote

inflating the main layout for the row_item that contains tetview, checkbox and add button

        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) {

the buttonView.getId() gets the first and last id

int iid = buttonView.getId(); Log.d("Buttonview getid", "" + iid);

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

            }
        });
    }
Mark Adler
  • 101,978
  • 13
  • 118
  • 158
zzz123
  • 1
  • What is the question ? – Zulu Jan 24 '15 at 16:21
  • why is the buttonView.getId() code always getting the first and last id when i select the first checkbox? i have tried debugging this code but it seems nothing is working – zzz123 Jan 24 '15 at 16:24
  • It's tough to tell from the code why this would be happening (maybe someone will spot it). However, your likely going to get an answer faster if you are using an IDE that allows debugging (e.g. Eclipse). If so, just place a breakpoint before your switch statement and see what it is called with and from where (by examining the stack trace). If you try this and still can't find the issue (or have already tried this), then please provide the debug information here. – blh83 Jan 24 '15 at 19:28

0 Answers0