【问题标题】:how to store textClock in Firebase FireStore如何在 Firebase FireStore 中存储 textClock
【发布时间】:2021-07-18 15:25:40
【问题描述】:

我想将 textClock 存储在 Firebase FireStore 上,已经尝试了将近一周的时间来解决它们,但仍然无法解决。我正在做一个考勤系统,一旦用户单击上一个活动中的按钮,此代码应该返回并存储值(用户 ID、姓名、时间和位置)在 Firebase FireStore 中

这是我完成的代码

public class DetailsKehadiran extends AppCompatActivity {

TextView tvNama, tvID, tvWaktu, tvLocation;
FusedLocationProviderClient fusedLocationProviderClient;
FirebaseAuth firebaseAuth;
FirebaseFirestore fStore;
TextClock textClock;
String userId;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_details_kehadiran);

    firebaseAuth = FirebaseAuth.getInstance();
    fStore = FirebaseFirestore.getInstance();
    userId = firebaseAuth.getCurrentUser().getUid();

    tvID = findViewById(R.id.tvID);
    tvNama = findViewById(R.id.tvNama);
    tvWaktu = findViewById(R.id.tvWaktuMasuk);
    tvLocation = findViewById(R.id.tvLocation);
    textClock = findViewById(R.id.textClock);

    fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);

    if (ActivityCompat.checkSelfPermission(DetailsKehadiran.this,
            Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
    && ActivityCompat.checkSelfPermission(DetailsKehadiran
    .this,Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
        getLocation();
    } else {
        //when permission is denied
        //request permission
        ActivityCompat.requestPermissions(DetailsKehadiran.this, new String[]
                {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}
                ,100);
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    //check condition
    if (requestCode == 100 && grantResults.length > 0 && (grantResults[0] + grantResults[1]
    == PackageManager.PERMISSION_GRANTED)){
        getLocation();
    }else{
        Toast.makeText(getApplicationContext(), "Permintaan ditolak.",Toast.LENGTH_SHORT).show();
    }
}

@SuppressLint("MissingPermission")
private void getLocation() {

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    //check condition
    if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
            || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){

        fusedLocationProviderClient.getLastLocation().addOnCompleteListener(new OnCompleteListener<Location>() {
            @Override
            public void onComplete(@NonNull Task<Location> task) {
                Location location = task.getResult();
                FirebaseUser user = firebaseAuth.getCurrentUser();

                if (location != null) {

                    try {
                        //initialize geocoder
                        Geocoder geocoder = new Geocoder(DetailsKehadiran.this, Locale.getDefault());
                        //initialize address list
                        List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
                        //set address on TextView
                        //tvLocation.setText(String.valueOf(addresses.get(0).getAddressLine(0)));

                        DocumentReference documentReference = fStore.collection("Users").document(user.getUid());
                        Map<String,Object> userInfo = new HashMap<>();
                        userInfo.put("Location",String.valueOf(addresses.get(0).getAddressLine(0)));
                        //userInfo.put("WaktuMasuk", Timestamp.now());
                        documentReference.update(userInfo);

                        documentReference.addSnapshotListener(DetailsKehadiran.this, new EventListener<DocumentSnapshot>() {
                            @Override
                            public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
                                tvID.setText(documentSnapshot.getString("IDKakitangan"));
                                tvNama.setText(documentSnapshot.getString("FullName"));
                                tvLocation.setText(documentSnapshot.getString("Location"));
                                tvWaktu.setText(documentSnapshot.getString("WaktuMasuk"));
                            }
                        });
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else{
                    LocationRequest locationRequest= new LocationRequest()
                            .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                            .setInterval(10000)
                            .setFastestInterval(1000)
                            .setNumUpdates(1);

                    LocationCallback locationCallback = new LocationCallback() {
                        @Override
                        public void onLocationResult(LocationResult locationResult) {

                            Location location1 = locationResult.getLastLocation();
                            try {
                                //initialize geocoder
                                Geocoder geocoder = new Geocoder(DetailsKehadiran.this, Locale.getDefault());
                                //initialize address list
                                List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
                                DocumentReference documentReference = fStore.collection("Users").document(user.getUid());
                                //tvLocation.setText(String.valueOf(addresses.get(0).getAddressLine(0)));
                                Map<String,Object> userInfo = new HashMap<>();
                                userInfo.put("Location",String.valueOf(addresses.get(0).getAddressLine(0)));
                                //userInfo.put("WaktuMasuk",Timestamp.now());
                                documentReference.update(userInfo);

                                documentReference.addSnapshotListener(DetailsKehadiran.this, new EventListener<DocumentSnapshot>() {
                                    @Override
                                    public void onEvent(@Nullable DocumentSnapshot documentSnapshot, @Nullable FirebaseFirestoreException e) {
                                        tvID.setText(documentSnapshot.getString("IDKakitangan"));
                                        tvNama.setText(documentSnapshot.getString("FullName"));
                                        tvLocation.setText(documentSnapshot.getString("Location"));
                                        tvWaktu.setText(documentSnapshot.getString("WaktuMasuk"));
                                    }
                                });

                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    };
                    fusedLocationProviderClient.requestLocationUpdates(locationRequest
                    ,locationCallback, Looper.myLooper());
                }
            }
        });
    }

    else{
        //when location service is not enabled
        //open location settings
        startActivity(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
                .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
    }
}

顺便说一句,我正在做一个考勤功能,提前谢谢你!

【问题讨论】:

    标签: java android firebase android-studio google-cloud-firestore


    【解决方案1】:

    TimeClock 具有获取字符串值时间的方法。 getText() 是方法。 每当您想保存一些对象并且感到困惑时,我们可以查看是否有一种方法可以获取字符串中的对象值,如果是,那么我们可以使用该方法,并且保存字符串值是 firebase 很简单。

    【讨论】:

      【解决方案2】:

      您可以将 TimeClock 值作为字符串存储在 Firebase 中,方法是通过前一个活动上的按钮上的 textClock.getText() 提取文本值作为触发点。

      【讨论】:

      • @NURNAYLINASUHABINTISHAMSUL 如果我有任何帮助,请尽可能为答案投票。
      猜你喜欢
      • 2021-08-11
      • 2021-07-23
      • 2020-06-15
      • 2020-04-16
      • 2018-08-10
      • 2021-10-14
      • 2020-07-24
      • 2020-03-28
      • 1970-01-01
      相关资源
      最近更新 更多