我是新手。尝试了其他资源,但其中大多数都讨论了回收站视图,但是对于我的应用程序,我正在使用视图模型。期待获得帮助。我想从Firebase云存储中检索用户个人资料数据。以下是我到目前为止提到的代码。
ProfileViewModel.java
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
public class ProfileViewModel extends ViewModel {
private MutableLiveData<String> mText;
public ProfileViewModel() {
mText = new MutableLiveData<>();
mText.setValue( "Your Profile" );
}
public LiveData<String> getText() {
return mText;
}
}
ProfileFragment.java
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import com.example.parkbookingslot.R;
public class ProfileFragment extends Fragment {
private ProfileViewModel profileViewModel;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
profileViewModel =
ViewModelProviders.of( this ).get( ProfileViewModel.class );
View root = inflater.inflate( R.layout.fragment_profile, container, false );
final TextView textView = root.findViewById( R.id.text_profile );
profileViewModel.getText().observe( getViewLifecycleOwner(), new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
textView.setText( s );
}
} );
return root;
}
}
fragment_profile.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.profile.ProfileFragment">
<TextView
android:id="@+id/email"
android:layout_width="84dp"
android:layout_height="31dp"
android:layout_marginEnd="164dp"
android:layout_marginRight="164dp"
android:layout_marginBottom="182dp"
android:text="Email"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/text_profile"
android:layout_width="109dp"
android:layout_height="35dp"
android:layout_marginEnd="150dp"
android:layout_marginRight="150dp"
android:layout_marginBottom="35dp"
android:text="Your Profile"
app:layout_constraintBottom_toTopOf="@+id/name"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/name"
android:layout_width="84dp"
android:layout_height="31dp"
android:layout_marginEnd="164dp"
android:layout_marginRight="164dp"
android:layout_marginBottom="34dp"
android:text="Name"
app:layout_constraintBottom_toTopOf="@+id/phoneno"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/phoneno"
android:layout_width="wrap_content"
android:layout_height="31dp"
android:layout_marginEnd="158dp"
android:layout_marginRight="158dp"
android:layout_marginBottom="36dp"
android:text="Phone Number"
app:layout_constraintBottom_toTopOf="@+id/vehicleno"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="@+id/vehicleno"
android:layout_width="112dp"
android:layout_height="35dp"
android:layout_marginEnd="150dp"
android:layout_marginRight="150dp"
android:layout_marginBottom="30dp"
android:text="Vehicle Number"
app:layout_constraintBottom_toTopOf="@+id/email"
app:layout_constraintEnd_toEndOf="parent" />
ViewModel
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
public class ProfileViewModel extends ViewModel {
public LiveData<UserProfile> getUserProfile(){
final MutableLiveData<UserProfile> userProfileMutableLiveData = new MutableLiveData<>();
FirebaseFirestore db = FirebaseFirestore.getInstance();
DocumentReference docRef = db.collection("users").document("android.dev@gmail.com");
docRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document != null && document.exists()) {
UserProfile userProfile = document.toObject(UserProfile.class);
userProfileMutableLiveData.postValue(userProfile);
} else {
userProfileMutableLiveData.postValue(null);
}
} else {
userProfileMutableLiveData.postValue(null);
}
}
});
return userProfileMutableLiveData;
}
}
片段
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import com.example.parkbookingslot.R;
public class ProfileFragment extends Fragment {
private ProfileViewModel profileViewModel;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate( R.layout.fragment_profile, container, false );
profileViewModel = ViewModelProviders.of(this).get(ProfileViewModel.class);
profileViewModel.getUserProfile().observe(getViewLifecycleOwner(),new Observer<UserProfile>(){
@Override
public void onChanged(UserProfile userProfile) {
if(userProfile != null){
//UPDATE YOUR UI HERE
} else {
//SHOW ERROR MESSAGE
}
}
});
return root;
}
用户个人资料模型
public class UserProfile {
String fullName, address, gender, phone;
public String getFullName() {
return fullName;
}
public void setFullName(String fullName) {
this.fullName = fullName;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
Firestore视图