所以我可以从我的 Firebase 数据库填充我的 Recycleview,但我的应用程序开始崩溃并且我需要指定数据库 url 的错误。
我已尝试按照以下步骤下载 google-service.json 。改变我写网址等的方式
这是我的依赖:
plugins {
id 'com.android.application'
id 'com.google.gms.google-services'
}
android {
compileSdkVersion 29
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "com.example.app"
minSdkVersion 16
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
}
dependencies {
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'com.google.android.material:material:1.2.1'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'com.google.firebase:firebase-database:19.2.1'
implementation 'com.firebaseui:firebase-ui-database:6.0.2'
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
}
这是我尝试检索数据的方法:
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_articles, container, false);
recyclerView = root.findViewById(R.id.recyclerView);
mbase = FirebaseDatabase.getInstance().getReference();
FirebaseRecyclerOptions<NewsList> news
= new FirebaseRecyclerOptions.Builder<NewsList>()
.setQuery(mbase, NewsList.class)
.build();
adapter = new NewsAdapter(news);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager( new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);
这是我的数据类:
public class NewsList {
private String title;
private String author;
public NewsList(String title,String author) {
this.title = title;
this.author =author;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
我也试过包括 url :
mbase = FirebaseDatabase.getInstance().getReference(url);
我在依赖项中缺少某些东西,或者我没有编写正确的代码?