【问题标题】:Android Firebase List Adapters Constructor ErrorAndroid Firebase 列表适配器构造函数错误
【发布时间】:2019-01-02 09:02:32
【问题描述】:

我正在尝试使用 Firebase 实时数据库中的数据创建一个列表视图。出于某种原因,我一直在此代码下看到一条红线。希望您能在这里告诉我我的错误是什么。谢谢 :)。我附上了我的布局文件以及我的应用程序 gradle 代码。

(this,String.class,android.R.layout.simple_list_item_1,databaseReference)

Below here is the complete code

            DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReferenceFromUrl("https://vsem-inventory.firebaseio.com/");
        FirebaseListAdapter<String> firebaseListAdapter = new FirebaseListAdapter<String>(this,String.class,android.R.layout.simple_list_item_1,databaseReference) {
            @Override
            protected void populateView(@NonNull View v, @NonNull String model, int position) {
                TextView textView = v.findViewById(android.R.id.text1);
            }
        };

        mListView.setAdapter(firebaseListAdapter);

content_main_menu.xml

  <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainMenu">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="0dp" />
</RelativeLayout>

应用分级

 apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.user.vseminventory"
        minSdkVersion 19
        targetSdkVersion 27
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.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.0.2'
    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.google.firebase:firebase-auth:16.0.5'
    implementation 'com.google.firebase:firebase-database:16.0.5'
    implementation 'com.google.firebase:firebase-storage:16.0.5'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.firebase:firebase-client-android:2.3.1'
    implementation 'com.firebaseui:firebase-ui-database:4.3.0'
}

apply plugin: 'com.google.gms.google-services'
com.google.gms.googleservices.GoogleServicesPlugin.config.disableVersionCheck = true

【问题讨论】:

  • 能否说明遇到的错误
  • 它只是说明编译错误
  • 请分享整个活动的代码,包括你的gradle

标签: android firebase android-studio firebase-realtime-database android-listview


【解决方案1】:

看来您使用的是新的 Firebaseui(3.0+ 版本),FirebaseListAdapter 的构造函数已更改。

首先需要使用FirebaseListOptions类配置适配器,然后可以创建FirebaseListAdapter的实例。

从文档中检查此代码:

 FirebaseListOptions<Chat> options = new FirebaseListOptions.Builder<Chat>()
    .setQuery(query, Chat.class)
    .build();

FirebaseListAdapter<Chat> adapter = new FirebaseListAdapter<Chat>(options) {
@Override
protected void populateView(View v, Chat model, int position) {
    // Bind the Chat to the view
    // ...
 }
};

如您所见,FirebaseListAdapter 采用FirebaseListOptions 类型的参数

更多信息在这里:

https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md#using-firebaseui-to-populate-a-listview


你可以在这里查看源代码:

https://github.com/firebase/FirebaseUI-Android/blob/master/database/src/main/java/com/firebase/ui/database/FirebaseListAdapter.java

【讨论】:

  • 我用什么来改变 .setQuery (query,...) 中的查询?
  • SetQuery(databaseReference, String.class)
  • 不要忘记阅读答案中链接的文档并添加startListening()stopListening()
  • @Blacky_99 这个答案对你有帮助吗?
  • 抱歉回复晚了,我的应用程序编译但崩溃了。 :(
猜你喜欢
  • 2018-04-24
  • 1970-01-01
  • 2014-01-03
  • 2020-12-03
  • 2015-09-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多