在尝试为我的 ListView 创建 FirebaseListAdapter 时发现了一个问题,我想从Firebase实时数据库中填充它。
当我专注于Cannot resolve symbol 'FirebaseListAdapter'
时,Android Studio会说FirebaseListAdapter<Trick> adapter = new FirebaseListAdapter<Trick>(options){}
。与FirebaseListOptions<Trick> options = new FirebaseListOptions.Builder<Trick>(){}
对已复制的问题进行了所有更改,包括更新 build.gradle ,但没有帮助
我的项目的 build.gradle :
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.0'
classpath 'com.google.gms:google-services:4.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven { url 'https://jitpack.io' }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
我的应用的 build.gradle :
apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'
android {
compileSdkVersion 28
defaultConfig {
applicationId "cz.zelgadiss.myapplication"
minSdkVersion 16
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.google.firebase:firebase-auth:16.2.1'
implementation 'com.google.firebase:firebase-core:16.0.8'
implementation 'com.google.firebase:firebase-database:16.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.github.GwonHyeok:StickySwitch:0.0.15'
implementation 'com.github.medyo:fancybuttons:1.9.1'
}
我的代码:
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
public class EncyActivity extends AppCompatActivity {
ListView list_View;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ency);
Query query = FirebaseDatabase.getInstance().getReference().child("Tricks");
list_View = (ListView) findViewById(R.id.listview);
FirebaseListOptions<Trick> options = new FirebaseListOptions.Builder<Trick>()
.setLayout(R.layout.trick)
.setQuery(query, Trick.class)
.build();
FirebaseListAdapter<Trick> adapter = new FirebaseListAdapter<Trick>(options) {
@Override
protected void populateView(View v, Object model, int position) {
Trick trick = (Trick) model;
TextView name = v.findViewById(R.id.name);
TextView description = v.findViewById(R.id.description);
name.setText(trick.getName().toString());
description.setText(trick.getDescription().toString());
}
};
list_View.setAdapter(adapter);
}
@Override
protected void onStart(){
super.onStart();
adapter.startListening();
}
@Override
protected void onStop(){
super.onStop();
adapter.stopListening();
}
}
答案 0 :(得分:1)
要使用FirebaseListAdapter,您需要导入Firebase UI
https://github.com/firebase/FirebaseUI-Android
// FirebaseUI for Firebase Realtime Database
implementation 'com.firebaseui:firebase-ui-database:4.3.2'
// FirebaseUI for Cloud Firestore
implementation 'com.firebaseui:firebase-ui-firestore:4.3.2'
也请检查