这是我的Java页面
package com.example.myfyp;
import android.Manifest;
import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import com.example.myfyp.Interface.IFirebaseLoadDone;
import com.example.myfyp.Model.UserID;
import com.google.firebase.FirebaseApp;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.List;
public class Homepage extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
Button b1, b2, btnlogout;
AudioManager am;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthStateListener;
TextView newName;
DatabaseReference databaseReference;
//SearchableSpinner searchableSpinner;
Spinner spinner;
IFirebaseLoadDone iFirebaseLoadDone;
FirebaseDatabase database;
List<UserID> movies;
TextView tvtesting;
BluetoothAdapter mBluetoothAdapter;
ArrayList<String> arrayList = new ArrayList<>();
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
}
public void onNothingSelected(AdapterView<?> parent) {
}
private Button pass;
private Button num;
private Button track;
private Button power;
@RequiresApi(api = Build.VERSION_CODES.P)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_homepage);
b1 = (Button) findViewById(R.id.vibrate);
b2 = (Button) findViewById(R.id.normal);
btnlogout = (Button) findViewById(R.id.logout);
newName = findViewById(R.id.textView);
tvtesting = findViewById(R.id.testing);
spinner = (Spinner) findViewById(R.id.selectdevice);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, 101);
}
FirebaseApp.initializeApp(this);
database = FirebaseDatabase.getInstance();
mAuth = FirebaseAuth.getInstance();
//databaseReference = FirebaseDatabase.getInstance().getReference("User");
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String useruid = user.getUid();
databaseReference = FirebaseDatabase.getInstance().getReference().child(useruid).child("name");
//final DatabaseReference databaseReference = database.getReference(mAuth.getUid());
//databaseReference = FirebaseDatabase.getInstance().getReference().child("User").child("user_id");
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, arrayList);
//iFirebaseLoadDone = this;
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
try {
UserID userID = dataSnapshot.getValue(UserID.class);
arrayList.add(String.valueOf(userID));
adapter.notifyDataSetChanged();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
spinner.setAdapter(adapter);
/*databaseReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
try {
/*String user_id = mAuth.getCurrentUser().getUid();
DatabaseReference current_user_db = FirebaseDatabase.getInstance().getReference().child("User").child(user_id);
String name = "maybecan";
Map newPost = new HashMap();
newPost.put("name", name);
current_user_db.setValue(newPost);*/
/*String onDataChange = dataSnapshot.getValue(String.class).toString();
arrayList.add(onDataChange);
adapter.notifyDataSetChanged();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
try {
adapter.notifyDataSetChanged();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});*/
/*databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
try {
List<UserID> movies = new ArrayList<>();
for (DataSnapshot movieSnapShot : dataSnapshot.getChildren()) {
movies.add(movieSnapShot.getValue(UserID.class));
}
iFirebaseLoadDone.onFirebaseLoadSuccess(movies);
}catch (DatabaseException e){
e.printStackTrace();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
try {
iFirebaseLoadDone.onFirebaseLoadFailed(databaseError.getMessage());
}catch(Exception e){
e.printStackTrace();
}
}
});
*/
/*databaseReference = FirebaseDatabase.getInstance().getReference().child("UserID").child("name");
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
int i = 0;
String name = dataSnapshot.child("1").getValue().toString();
tvtesting.setText(name);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});*/
//addName();
//arrayList = new ArrayList<String>();
//adapter = new ArrayAdapter<String>(Homepage.this, android.R.layout.simple_list_item_1, arrayList);
//try {
//getBluetoothName();
// tvtesting.setText(mBluetoothAdapter.agetNme());
//}catch(Exception e){
// e.printStackTrace();
//}
am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
//1- Vibrate 2-Normal
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
am.setRingerMode(1);
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
am.setRingerMode(2);
}
});
/*
try {
Spinner spinner = (Spinner) findViewById(R.id.selectdevice);
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource
(this, R.array.device, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter1);
Spinner spinner1 = (Spinner) findViewById(R.id.selectdevice);
spinner.setOnItemSelectedListener(this);
}catch(Exception e){
e.printStackTrace();
}
*/
pass = (Button) findViewById(R.id.set_password);
pass.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openLock();
}
});
num = (Button) findViewById(R.id.load_number);
num.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openNumber();
}
});
track = (Button) findViewById(R.id.track);
track.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openTracking();
}
});
power = (Button) findViewById(R.id.gps_power);
power.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openGPSPower();
}
});
btnlogout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FirebaseAuth.getInstance().signOut();
openMainActivity();
}
});
}
@RequiresApi(api = Build.VERSION_CODES.O)
public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 101:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
return;
}
newName.setText(Build.DEVICE);
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
protected void onResume() {
super.onResume();
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
return;
}
newName.setText(Build.DEVICE);
}
/*public void addName() {
String name = Build.DEVICE;
if (TextUtils.isEmpty(name)) {
Name phoneName = new Name(name);
databaseReference.child(name).setValue(phoneName);
Toast.makeText(this, "New name has added", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(this, "Error", Toast.LENGTH_LONG).show();
}
}*/
/* public String getBluetoothName() {
if (mBluetoothAdapter == null) {
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
}
String name = mBluetoothAdapter.getName();
if (name == null) {
System.out.println("Name is null!");
name = mBluetoothAdapter.getAddress();
}
return name;
}*/
public void openLock() {
Intent intent = new Intent(this, Lock.class);
startActivity(intent);
}
public void openNumber() {
Intent intent = new Intent(this, Number.class);
startActivity(intent);
}
public void openTracking() {
Intent intent = new Intent(this, Tracking.class);
startActivity(intent);
}
public void openGPSPower() {
Intent intent = new Intent(this, GPSPower.class);
startActivity(intent);
}
public void openMainActivity() {
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
/*private boolean addName(String name){
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("User").child(name);
UserID userID = new UserID(name);
databaseReference.setValue(userID);
Toast.makeText(this, "Successfully", Toast.LENGTH_LONG).show();
return true;
}*/
/*@Override
public void onFirebaseLoadSuccess(List<UserID> movieList) {
try {
movies = movieList;
List<String> name_list = new ArrayList<>();
for (UserID movie : movieList)
name_list.add(movie.getName());
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, name_list);
spinner.setAdapter(adapter);
}catch(DatabaseException e){
e.printStackTrace();
}
}
@Override
public void onFirebaseLoadFailed(String message) {
}*/
}
这是我的获取数据库值页面
package com.example.myfyp.Model;
public class UserID {
int sum=0;
public UserID() {
}
public UserID(int sum) {
//for(int i = 1; i<5;i++){
//sum += i;
this.sum = sum;
//}
}
public int getSum() {
/*for (int i = 1; i < 5; i++) {
sum += i;
return sum;
}*/
return sum;
}
public void setSum(int sum) {
//for (int i = 1; i < 5; i++) {
//sum += i;
//}
this.sum = sum;
}
@Override
public String toString() {
//for (int i = 1; i < 5; i++) {
// sum += i;
//}
return "sum=" + sum +
'}';
}
}
这是我的firebase数据库界面,显示用户ID以及名称和名称中的值
{
"9dT4gNOlzEhfeOEvE2Ff7732Dm02" :{
"name" : ["test1@gmail.com", "try"]
}
}
因此,当我登录时,应该可以在微调框选择中看到test1@gmail.com
和“ try”。但是微调框没有显示任何内容。希望有一些建议可以对我有所帮助。谢谢