bounty将在6天后过期。回答此问题可获得+50的声望奖励。Timothy Smith正在寻找来自知名来源的答案。
我正在尝试创建一个应用程序,允许用户租用离他们最近的汽车。我想构建一个带有textView和imageView的cardView。一旦我找到了最近的汽车,我就创建了方法buildCardView来实现这一点。我的问题是,当调用cardView时,它没有被创建。当我直接调用它时,buildCardView工作正常。但在Geocerquery onKeyEntered方法内部调用时却不是这样。我调试了代码,在onKeyEntered方法内部调用了buildCardView方法,但cardView是不可见的。下面是调用firebase方法的代码。
FindCar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
findClosestCars();
}
});
下面是调用buildCardView的firebase方法
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++;
}
下面是buildCardView,它在主视图中扩大了一个较小的视图(直接调用时有效,但从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);
}
1条答案
按热度按时间tpgth1q71#
onKeyEntered方法是在与主线程不同的线程中调用的,因此您在该方法中对UI所做的任何更改都将不可见。
若要解决此问题,您需要更新主线程上的UI。您可以使用runOnUiThread方法来完成此操作