数据库
我已经在数据库中检索了该名称并将其存储在微调器中。我想问的是在微调器中选择一个名称后如何检索“名称”的“数字”,然后单击按钮后该数字将如何显示在文本视图中?这是我的代码,它不起作用。
private void getNumber(){
mDatabaseRef.child("Drivers' Info").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
String name = postSnapshot.child("name").getValue(String.class);
if (name.equals(sp)) {
String j = (String) postSnapshot.child("number").getValue();
display_number.setTextSize(26);
display_number.setText(j);
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
我的布局
请帮助我!预先感谢!
答案 0 :(得分:0)
以下是我为解决该问题而编写的一段代码中所有为您注释的摘录
//Create the spinner
View spinnerView = LayoutInflater.from(prefs.this).inflate(R.layout.activity_custom_spinner,null);
final Spinner fechasBackupUsuario = spinnerView.findViewById(R.id.spinnerBackup);
mDatabase.child("Users").child(uid).child("Backup").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
//We create an arraylist to hold the values brought from the database and show them in the spinner
lstBackups = new ArrayList<String>();
lstBackupsMilis = new ArrayList<String>();
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
mBackupDates = snapshot.getKey();
if(!mBackupDates.contains("UltimoBackup")){
long serverDate = Long.parseLong(mBackupDates);
String humanDate = convertTimeWithTimeZome(serverDate*1000);
Log.e("FirebaseBackup","HumanDate: "+humanDate+" \\n ServerDate: "+mBackupDates);
lstBackups.add(humanDate);
lstBackupsMilis.add(mBackupDates);
int cantBackups = lstBackups.size();
Log.e("FirebaseBackups","Cantidad de backups: "+cantBackups);
}
//Now we populate the spinner with that arraylist
final ArrayAdapter<String> backupAdapter = new ArrayAdapter<String>(prefs.this,android.R.layout.simple_spinner_item,lstBackups);
backupAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
fechasBackupUsuario.setAdapter(backupAdapter);
//Click event for each spinner element
fechasBackupUsuario.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
// getting the element when we click
mSelectedBackupDate = lstBackupsMilis.get(i);
Log.e("FirebaseBackup",""+mSelectedBackupDate);
//Here i pass the reference from that value into another snapshot in order to query those values, here you need to get your node id and inside just get your number , name and so on
mDatabase.child("Usuarios").child(uid).child("Backup").child(mSelectedBackupDate).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
//logic to get my data from that specific spinner value
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
答案 1 :(得分:0)
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("SmartDab").child("User");
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
List<String > are = new ArrayList<>();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
String userType = ds.child("status").getValue().toString();
switch (userType) {
case "Teacher":
String Name = ds.child("fullname").getValue().toString();
are.add(Name);
break;
}
}
ArrayAdapter<String> areasAdapter = new ArrayAdapter<String>
(giftclass.this, android.R.layout.simple_spinner_item,
are);
areasAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(areasAdapter);
spin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selectedItem = spin.getSelectedItem().toString();
Toast.makeText(giftclass.this, "You Select Prof: "+selectedItem, Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});