我计划在(User)数据库中检索学生userName和userStudNum,并将其存储在(StoreInfo)数据库中。但它给了我零值。帮我修好它
商店信息:
用户名:
我有两个java文件。 www.example.com
这个 www.example.com 。但就像在firebase中一样,它存储空值。
public class scanner extends Drawable {
Button qrbtn;
public static FusedLocationProviderClient fusedLocationProviderClient;
public static TextView qrtext, id, arrival, date, address, name, studNum;
public static int REQUEST_CODE = 100;
private FirebaseUser currentUser;
DatabaseReference usersRef;
ActivityScannerBinding activityScannerBinding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activityScannerBinding = ActivityScannerBinding.inflate(getLayoutInflater());
setContentView(activityScannerBinding.getRoot());
allocateActivityTitle("Attendance Scanner");
qrbtn = (Button) findViewById(R.id.qrbtn);
qrtext = (TextView) findViewById(R.id.qrtext);
arrival = (TextView) findViewById(R.id.arrival);
date = (TextView) findViewById(R.id.date);
id = (TextView) findViewById(R.id.deviceId);
name = (TextView) findViewById(R.id.name);
studNum = (TextView) findViewById(R.id.studentNum);
address = (TextView) findViewById(R.id.location);
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
// saveUser();
qrbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
startActivity(new Intent(getApplicationContext(), qrscanner.class));
getLocation();
saveUser();
}
});
}
private void getLocation() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
fusedLocationProviderClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
try {
Geocoder geocoder = new Geocoder(scanner.this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
address.setText("Location: " + addresses.get(0).getAddressLine(0)
);
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
} else {
askPermission();
}
}
private void askPermission() {
ActivityCompat.requestPermissions(scanner.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull @org.jetbrains.annotations.NotNull String[] permissions, @NonNull @org.jetbrains.annotations.NotNull int[] grantResults) {
if (requestCode == REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getLocation();
} else {
Toast.makeText(scanner.this, "Please provide the required permission", Toast.LENGTH_SHORT).show();
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
private void saveUser() {
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("User");
FirebaseAuth auth = FirebaseAuth.getInstance();
FirebaseUser user = auth.getCurrentUser();
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String nameString = dataSnapshot.child("userName").getValue(String.class);
String studNumString = dataSnapshot.child("userStudNo").getValue(String.class);
name.setText("" + nameString);
studNum.setText("" + studNumString);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
qrscanner.java
public class qrscanner extends Drawable implements ZXingScannerView.ResultHandler
{
ZXingScannerView scannerView;
DatabaseReference dbref, usersRef;
private FirebaseUser currentUser;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
scannerView = new ZXingScannerView(this);
setContentView(scannerView);
//saveUser();
dbref = FirebaseDatabase.getInstance().getReference("StoreInfo");
currentUser = FirebaseAuth.getInstance().getCurrentUser();
usersRef = FirebaseDatabase.getInstance().getReference("User");
Dexter.withContext(getApplicationContext())
.withPermission(Manifest.permission.CAMERA)
.withListener(new PermissionListener() {
@Override
public void onPermissionGranted(PermissionGrantedResponse permissionGrantedResponse) {
scannerView.startCamera();
}
@Override
public void onPermissionDenied(PermissionDeniedResponse permissionDeniedResponse) {
}
@Override
public void onPermissionRationaleShouldBeShown(PermissionRequest permissionRequest, PermissionToken permissionToken) {
permissionToken.continuePermissionRequest();
}
}).check();
}
@Override
public void handleResult(Result result) {
String data = result.getText().toString();
Date dateAndTime = (Date) Calendar.getInstance().getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy", Locale.getDefault());
SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm:ss", Locale.getDefault());
String date = dateFormat.format(dateAndTime);
String time = timeFormat.format(dateAndTime);
String deviceId = Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);
getLocation();
saveUser();
String locationString = scanner.address.getText().toString();
String name = scanner.name.getText().toString();
String studNum = scanner.studNum.getText().toString();
StoreInfo storeInfo = new StoreInfo(data, date, time, deviceId, locationString, name, studNum);
dbref.push().setValue(storeInfo).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
scanner.qrtext.setText("Course: " + data);
scanner.date.setText("Date: " + date);
scanner.arrival.setText("Arrival time: " + time);
scanner.id.setText("Device Id: " + deviceId.toString());
scanner.address.setText("Location: " + locationString);
scanner.name.setText("Name: " + name);
scanner.studNum.setText("Student Number: " + studNum);
onBackPressed();
}
});
}
private void saveUser() {
DatabaseReference ref = FirebaseDatabase.getInstance().getReference("User");
FirebaseAuth auth = FirebaseAuth.getInstance();
FirebaseUser user = auth.getCurrentUser();
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String nameString = dataSnapshot.child("userName").getValue(String.class);
String studNumString = dataSnapshot.child("userStudNo").getValue(String.class);
name.setText("" + nameString);
studNum.setText("" + studNumString);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});}
private void getLocation() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
fusedLocationProviderClient.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
@Override
public void onSuccess(Location location) {
if (location != null) {
try {
Geocoder geocoder = new Geocoder(qrscanner.this, Locale.getDefault());
List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
scanner.address.setText("Location: " + addresses.get(0).getAddressLine(0)
);
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
} else {
askPermission();
}
}
private void askPermission() {
ActivityCompat.requestPermissions(qrscanner.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_CODE);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull @org.jetbrains.annotations.NotNull String[] permissions, @NonNull @org.jetbrains.annotations.NotNull int[] grantResults) {
if (requestCode == REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
getLocation();
} else {
Toast.makeText(qrscanner.this, "Please provide the required permission", Toast.LENGTH_SHORT).show();
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
@Override
protected void onPause() {
super.onPause();
scannerView.stopCamera();
}
@Override
protected void onResume() {
super.onResume();
scannerView.setResultHandler(this);
scannerView.startCamera();
}
}
www.example. com 将用户信息存储在StoreInfo数据库中
1条答案
按热度按时间jyztefdp1#
您正在加载
User
节点,然后从该节点下读取userName
属性。但是User
下没有userName
属性,因为JSON中有一个额外的层(屏幕截图中的201-0001
)。你必须考虑到这个额外的层,例如通过循环
dataSnapshot
的子层: