My method does not work inside of onKeyEntered
Asked Answered
F

1

6

I am trying to create an app that will allow users to rent a car nearest to them. I want to build a cardView with a textView and imageView once i find the nearest car, I created the method buildCardView to do that. My problem is that the cardView is not getting created when its called. buildCardView works fine when I call it directly, but not when called inside of the Geoquery onKeyEntered method. I debugged the code and the method buildCardView is getting called inside of on onKeyEntered but the cardView is not visible. Below is the code for that calls the firebase method.

FindCar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
                   findClosestCars();
        }
    });

Below is the firebase method that calls buildCardView

private void findClosestCars() {
    if (ActivityCompat.checkSelfPermission(RentersMenuPageActivity.this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

     //  turnOnLocationUpdates();
        //fusedLocationClient.requestLocationUpdates(mLocationRequest, locationCallback, Looper.myLooper());

        databaseReference = FirebaseDatabase.getInstance().getReference("Users").child("Lenders");
        GeoFire geoFire = new GeoFire(databaseReference);
        GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(latitude, longitude), radius);
        geoQuery.removeAllListeners();

        geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
            @Override
            public void onKeyEntered(String key, GeoLocation location) {
           //
                //  Toast.makeText(RentersMenuPageActivity.this, "You Found a Lender",Toast.LENGTH_LONG);
                  //      Toast.LENGTH_LONG).show();


                //showPictureAndPrice();

                if (!driverFound) {
                    driverFound = true;
                    driverFoundID = key;

                    closestLender = FirebaseDatabase.getInstance().getReference("Users").child("Lenders").child(driverFoundID);

                    String [] lenderLocations;

                    buildCardView();
                }

               radius++;
            }

Below is buildCardView that inflates a smaller view inside the main one (works when called directly but not from onkeyentered)

private void buildCardView(){
    Toast.makeText(RentersMenuPageActivity.this, "You Found a Lender",Toast.LENGTH_LONG).show();

    LayoutInflater mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

    View view =getLayoutInflater().inflate(R.layout.card,null);

   // CardView cardView = view.findViewById(R.id.cardView);
    Button rentCarButton = view.findViewById(R.id.rentCarButton);
    TextView price= view.findViewById(R.id.price);
    TextView makeandmodel = view.findViewById(R.id.makeandmodel);
    ImageView imageView =view.findViewById(R.id.imageViewScroll);

    LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
    linearLayout.addView(view);
}
Forsooth answered 29/11, 2022 at 1:40 Comment(6)
I'm not sure I understand what isn't working. Instead of explaining in words, I recommend locally debugging and telling us the result of that. So if you set a breakpoint on each line of the code you shared, run the code in a debugger, and then check the value of each variable on each line, which is the first line that doesn't do what you expect it to do?Republic
Please edit your question and add the information Frank asked for, and please also respond using @.Barrera
@AlexMamo when I debug the app everything inside of the buildCarView method is getting populated but I cant see the cardView, textView and imageView.Forsooth
So it's an issue related more to views rather than Firebase, right?Barrera
@FrankvanPuffelen does the question make more sense now?Forsooth
@AlexMamo I am not sure.The cardview works fine when I call it directly from the Find car button. The card view is only not visible when I call it inside of the on Key entered method.Forsooth
A
0

try creating activity instance as

AppCompatActivity activity = YourActivity.this;

Then use your code as:

private void buildCardView(){
    Toast.makeText(activity, "You Found a Lender",Toast.LENGTH_LONG).show();

    //changed here
    LayoutInflater mInflater = (LayoutInflater) activity.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); 

    //changed here
    View view =activity.getLayoutInflater().inflate(R.layout.card,null); 

    // CardView cardView = view.findViewById(R.id.cardView);
    Button rentCarButton = view.findViewById(R.id.rentCarButton);
    TextView price= view.findViewById(R.id.price);
    TextView makeandmodel = view.findViewById(R.id.makeandmodel);
    ImageView imageView =view.findViewById(R.id.imageViewScroll);

    LinearLayout.LayoutParams linearLayout = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT);
    relativeLayoutMainLayout.addView(view);
}

Tested on GeoQueryDataEventListener and the card view came up.

Allergist answered 7/12, 2022 at 7:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.