为什么我的onclick弄乱了代码?

时间:2019-10-06 07:25:01

标签: android button onclick

我在navdraw中有一个按钮Submit_house,它在另一个按钮的(add_house)onclick侦听器中获取一个onclick侦听器。 Submit_house是结束add_house的onclick的按钮,此后它将获得新的onclick。但是由于某种原因,第一个Submit_house onclick无法正常工作(那里的log命令实际上没有注册)。为什么呢?

main.java中的代码:

building_onclick = new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
                    @Override
                    public void onMapClick(LatLng point) {

                        mMap.setOnMapClickListener(null);

                        // this line clears the arraylist
                        arrayList.clear();
                        // next thing you have to do is check if your adapter has changed
                        adapter.notifyDataSetChanged();
                        building.emptyBuilding();
                        //the navdraw
                        buildingViewNavdraw(add_house, houseList);

                        //launches drawer
                        // locks drawer from opening from swipes
                        navDrawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
                        // If the navigation drawer is not open then open it, if its already open then close it.
                        if(!navDrawer.isDrawerOpen(Gravity.RIGHT)) navDrawer.openDrawer(Gravity.RIGHT);
                        else navDrawer.closeDrawer(Gravity.LEFT);

                        //sets building latlng
                        building.setLat(point.latitude);
                        building.setLng(point.longitude);

                        building.setMarkerId(Double.toString(building.getLat()).replace(".","") + Double.toString(building.getLng()).replace(".",""));

                        View.OnClickListener add_houseOnClick = new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                                //TODO: create alert dialog to get apartment number
                                house.setApartment("1");

                                houseEditNavdraw(status, activists, familyNameEdit, addressEdit, descriptionEdit, currentStatusEdit, submit_house, add_report);

                                isHouseUploaded = false;
                                isInBuilding = true;
                                house.setType("House");

                                //copies over content from building to house
                                house.setLat(building.getLat());
                                house.setLng(building.getLng());
                                house.setMarkerId("A" + house.getApartment() + building.getMarkerId());
                                house.setLocation(building.getLocation());

                                currentStatusEdit.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                                    @Override
                                    public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                                        if (currentStatusEdit.getSelectedItem().toString().equals("קשר המשך")){
                                            currentActivistsEdit.setVisibility(View.VISIBLE);
                                            activists.setVisibility(View.VISIBLE);
                                            currentActivists.setVisibility(View.INVISIBLE);
                                            add_report.setVisibility(View.VISIBLE);
                                        } else if (currentStatusEdit.getSelectedItem().toString().equals("לא מעוניינים") || currentStatusEdit.getSelectedItem().toString().equals("מעוניינים")){
                                            currentActivists.setVisibility(View.INVISIBLE);
                                            activists.setVisibility(View.INVISIBLE);
                                            currentActivistsEdit.setVisibility(View.INVISIBLE);
                                            add_report.setVisibility(View.VISIBLE);
                                        } else if (currentStatusEdit.getSelectedItem().toString().equals("לא עונים")) {
                                            currentActivists.setVisibility(View.INVISIBLE);
                                            activists.setVisibility(View.INVISIBLE);
                                            currentActivistsEdit.setVisibility(View.INVISIBLE);
                                            add_report.setVisibility(View.INVISIBLE);
                                        }
                                    }

                                    @Override
                                    public void onNothingSelected(AdapterView<?> parentView) {
                                        // your code here
                                    }

                                });

                                // this line adds the data of the apartmnet and puts in your array
                                arrayList.add(house.getApartment() + ": " + "משפחת " + house.getName());
                                // next thing you have to do is check if your adapter has changed
                                adapter.notifyDataSetChanged();

                                //sets the report onClicks
                                add_report.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {

                                        reportEditNavdraw(reportEdit, reportDateEdit, reportActivistsEdit, submit_report);

                                        submit_report.setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View v) {

                                                houseEditNavdraw(status, activists, familyNameEdit, addressEdit, descriptionEdit, currentStatusEdit, submit_house, add_report);

                                                if (currentStatusEdit.getSelectedItem().toString() == "קשר המשך"){
                                                    activists.setVisibility(View.VISIBLE);
                                                    currentActivistsEdit.setVisibility(View.VISIBLE);
                                                }

                                                Date date = new Date();

                                                Report rpt = new Report(reportActivistsEdit.getText().toString(), reportEdit.getText().toString(), reportDateEdit.getText().toString());
                                                reportNameList.add(house.getMarkerId() + date.toString().replace(" ", "").replace(":", "").replace("GMT+", "").replace(".",""));
                                                reportList.add(rpt);

                                                lastReport.setVisibility(View.VISIBLE);
                                                lastReport.setText(reportEdit.getText());
                                                reportActivistsTitle.setVisibility(View.VISIBLE);
                                                reportActivists.setVisibility(View.VISIBLE);
                                                reportActivists.setText(reportActivistsEdit.getText());
                                                reportDateTitle.setVisibility(View.VISIBLE);
                                                reportDate.setVisibility(View.VISIBLE);
                                                reportDate.setText(reportDateEdit.getText());
                                                reportEdit.setVisibility(View.INVISIBLE);
                                                reportDateEdit.setVisibility(View.INVISIBLE);
                                                reportActivistsEdit.setVisibility(View.INVISIBLE);
                                                submit_report.setVisibility(View.INVISIBLE);
                                                reportDateEdit.setText("");
                                                reportActivistsEdit.setText("");
                                                reportEdit.setText("");
                                            }
                                        });
                                    }
                                });

                                submit_house.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        // TODO: add safeguards against partial input

                                        //sets data for "blank" drawer
                                        familyName.setText(familyNameEdit.getText());
                                        familyNameEdit.setVisibility(View.INVISIBLE);
                                        familyName.setVisibility(View.VISIBLE);

                                        address.setText(addressEdit.getText());
                                        addressEdit.setVisibility(View.INVISIBLE);
                                        address.setVisibility(View.VISIBLE);

                                        description.setText(descriptionEdit.getText());
                                        descriptionEdit.setVisibility(View.INVISIBLE);
                                        description.setVisibility(View.VISIBLE);

                                        currentStatusEdit.setVisibility(View.INVISIBLE);
                                        currentStatus.setText(currentStatusEdit.getSelectedItem().toString());
                                        currentStatus.setVisibility(View.VISIBLE);

                                        if (currentStatusEdit.getSelectedItem().toString() == "קשר המשך"){
                                            currentActivists.setText(currentActivistsEdit.getText());
                                            currentActivistsEdit.setVisibility(View.INVISIBLE);
                                            currentActivists.setVisibility(View.VISIBLE);
                                            activists.setVisibility(View.VISIBLE);
                                            house.setActivists(currentActivists.getText().toString());
                                        }

                                        submit_report.setVisibility(View.INVISIBLE);
                                        add_report.setVisibility(View.INVISIBLE);

                                        currentStatusEdit.setSelection(0, false);

                                        //setting all the values to house
                                        house.setName(familyName.getText().toString());
                                        house.setAddress(address.getText().toString());
                                        house.setDescription(description.getText().toString());
                                        house.setStatus(currentStatus.getText().toString());

                                        //upload reports to db
                                        if (reportNameList.size() != 0){
                                            for (int i = 0; i < reportNameList.size(); i++){

                                                mDatabase.child("reports").child(reportNameList.get(i)).setValue(reportList.get(i));

                                            }
                                            //sets the report list to the house
                                            house.setReport(reportNameList);
                                            house.setLatestReport(reportNameList.get(reportNameList.size() - 1));
                                            //clears the lists for next use
                                            reportNameList.clear();
                                            reportList.clear();
                                        }


                                        mDatabase.child("houses").child(house.getLocation()).child(house.getMarkerId()).setValue(house);
                                        house.emptyHouse();
                                        isHouseUploaded = true;

                                        building.addHouse(house.getMarkerId());

                                        Log.d("here", building.getHouses().toString());
                                        emptyNavdraw();
                                        buildingViewNavdraw(add_house, houseList);
                                    }
                                });

                            }
                        };

                        add_house.setOnClickListener(add_houseOnClick);

                        add_houseOnClick.onClick(v);

                        submit_house.setVisibility(View.VISIBLE);
                        add_house.setVisibility(View.INVISIBLE);
                        submit_house.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {

                                mDatabase.child("houses").child(building.getLocation()).child("B" + building.getMarkerId()).setValue(building);

                                LatLng latLng = new LatLng(building.getLat(), building.getLng());
                                Marker marker = mMap.addMarker(new MarkerOptions()
                                        .position(latLng)
                                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.building15x15)));
                                marker.setTag(building.getMarkerId());

                                building.emptyBuilding();

                                submit_house.setVisibility(View.INVISIBLE);
                                add_house.setVisibility(View.VISIBLE);
                                submit_house.setOnClickListener(null);
                            }
                        });

                    }
                });
            }

        };

1 个答案:

答案 0 :(得分:0)

Implement your activity with View.OnClickListener interface.
It will implement a method with name onClick().
Now add this line in onCreate() method : submit_house.setOnClickListener(this);
And you can perform your work in onClick() as

public void onClick(View v)
 {
 if(v.id==R.id.submit_house)
   {
   //
   }
  }


But you cannot perform two different functions on a single button. For that 
you have to use internal variable to identify the case.