如何在Android Studio中设置FirebaseAdapter? [英] How do I setup FirebaseAdapter in Android Studio?

查看:237
本文介绍了如何在Android Studio中设置FirebaseAdapter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从Firebase实时读取数据到一个自定义ArrayAdapter。从我的研究来看,FirebaseListAdapter的内置功能可以解决这个问题。但是,我无法让我的代码识别FirebaseListAdapter类。如何让FirebaseListAdapter工作?



Gradle文件



 应用插件:'com.android.application'

android {
compileSdkVersion 24
buildToolsVersion24.0.0

defaultConfig {
applicationIdcom.example.quizzingcompanion
minSdkVersion 15
targetSdkVersion 24
versionCode 1
versionName1.0
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
}
}
}

依赖关系{
编译fileTree(包括:['* .jar'],dir:'libs')
testCompile'junit:junit:4.12'
compile'c​​om.google。 firebase:firebase-database:9.6.0'
compile'c​​om.google.firebase:firebase-core:9.6.0'
compile'c​​om.google.firebase:firebase -auth:9.6.0'
compile'c​​om.android.support:appcompat-v7:24.1.1'
compile'c​​om.android.support:design:24.1.1'
compile 'com.android.support:support-v4:24.1.1'
compile'c​​om.firebase:firebase-client-android:2.3.1'
compile'c​​om.firebaseui:firebase-ui:0.6 .0'
compile'c​​om.firebaseui:firebase-ui-storage:0.6.0'
compile'c​​om.firebaseui:firebase-ui-database:0.6.0'
}

apply plugin:'com.google.gms.google-services'



FirebaseAdapter初始化



  FirebaseListAdapter< Participant> adapter = new FirebaseListAdapter; 



错误讯息



 无法解析:com.google.firebase:firebase-auth:9.6.0 
无法解析:com.google.firebase:firebase-storage:9.6.0
无法解析:com.google.firebase:firebase-database:9.6.0
无法解析:com.google.android.gms:play-services-auth:9.6.0
无法解析符号FirebaseListAdapter



带有列表的片段



  import android.content.Intent; 
导入android.os.Bundle;
导入android.support.design.widget.FloatingActionButton;
导入android.support.v4.app.Fragment;
导入android.view.LayoutInflater;
导入android.view.View;
导入android.view.ViewGroup;
导入android.widget.ListView;

import java.util.ArrayList;

/ **
*一个简单的{@link Fragment}子类。
* /
public class ParticipantFragment extends Fragment {
private DataSource data;
DatabaseReference数据库;
ArrayList< Participant>参与者=新的ArrayList<>();

public ParticipantFragment(){
data = new DummyDataSource();
database = FirebaseDatabase.getInstance()。getReference();


$ b @Override
public View onCreateView(LayoutInflater inflater,ViewGroup容器,
Bundle savedInstanceState){

final查看rootView = inflater.inflate(R.layout.content_participants_fragment,container,false);

FirebaseListAdapter<参与者> adapter = new FirebaseListAdapter;
ListView listView =(ListView)rootView.findViewById(R.id.participants_list);
ParticipantAdapter adapter = new ParticipantAdapter(getActivity(),data.getParticipants());
listView.setAdapter(adapter);

FloatingActionButton fab =(FloatingActionButton)rootView.findViewById(R.id.participants_fab);
fab.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent(getContext(),NewParticipant.class);
startActivity(intent);
}
});}



/ h2>

  import android.app.Activity; 
导入android.media.Image;
导入android.view.LayoutInflater;
导入android.view.View;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.ImageView;
导入android.widget.TextView;

import java.util.ArrayList;

/ **
*由Tim于9/10/2016创建。
* /
public class ParticipantAdapter extends ArrayAdapter {
public ParticipantAdapter(Activity context,ArrayList< Participant> participant){
super(context,0,participant);

$ b $覆盖
public View getView
if(listItemView == null){
listItemView = LayoutInflater.from(getContext())。
}

参与者currentParticipant =(参与者)getItem(position);

ImageView participantImage =(ImageView)listItemView.findViewById(R.id.participant_image);
if(currentParticipant.hasImageValue()){
participantImage.setImageResource(currentParticipant.getImageID());
} else {
participantImage.setImageResource(R.drawable.ic_my_profile);
}

TextView name =(TextView)listItemView.findViewById(R.id.participant_name);
name.setText(currentParticipant.getDisplayName());

ImageView image =(ImageView)listItemView.findViewById(R.id.participant_role);
switch(currentParticipant.getRole()){
caseOfficial:image.setImageResource(R.drawable.ic_official);
caseSpectator:image.setImageResource(R.drawable.ic_spectator);
caseQuizzer:image.setImageResource(R.mipmap.ic_quizzer);
default:image.setVisibility(View.GONE);
}

return listItemView;


$ / code $ / pre
$ b $ h2 / b

 按钮okBtn =(按钮)findViewById(R.id.add_participant); 
okBtn.setOnClickListener(new View.OnClickListener(){
@Override
$ b $ public void onClick(View view){
DatabaseReference ref = database.child( );
EditText firstName =(EditText)findViewById(R.id.first_name);
EditText lastName =(EditText)findViewById(R.id.last_name);
Participant newParticipant = new Participant (),
lastName.getText()。toString(),
sp.getSelectedItem()。toString(),
sp2.getSelectedItem()。toString ());
Map< String,Object> participantValues = newParticipant.toMap();
ref.push().setValue(participantValues);
}
});


解决方案

FirebaseUI readme 有这个方便的兼容性表格

  FirebaseUI Firebase / Play 
版本服务版本
1.2.0 10.2.0
1.1.1 10.0.0或10.0.1
1.0.1 10.0.0或10.0.1
1.0.0 9.8.0
0.6.2 9.8.0
0.6.1 9.6.1
0.6.0 9.6.0
0.5.3 9.4.0
0.4.4 9.4.0
0.4.3 9.2.1
0.4.2 9.2.0
0.4.1 9.0.2
0.4.0 9.0.0 $ b因此,要使用FirebaseUI 0.6,您必须使用Firebase / Play服务SDK 9.6。如果您想使用Firebase / Play服务SDK 9.4,则必须使用FirebaseUI 0.5。 另外,您缺少列表适配器的导入语句:

/ p>

  import com.firebase.ui.database.FirebaseListAdapter; 

Android Studio应该已经提供了关于此导入的提示。


I am trying to read data in real time from Firebase into a custom ArrayAdapter. From my research, it looks like FirebaseListAdapter has built in functionality to handle this. However, I am unable to get my code to recognize the FirebaseListAdapter class. How do I get the FirebaseListAdapter to work?

Gradle File

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 24
    buildToolsVersion "24.0.0"

    defaultConfig {
        applicationId "com.example.quizzingcompanion"
        minSdkVersion 15
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    testCompile 'junit:junit:4.12'
    compile 'com.google.firebase:firebase-database:9.6.0'
    compile 'com.google.firebase:firebase-core:9.6.0'
    compile 'com.google.firebase:firebase-auth:9.6.0'
    compile 'com.android.support:appcompat-v7:24.1.1'
    compile 'com.android.support:design:24.1.1'
    compile 'com.android.support:support-v4:24.1.1'
    compile 'com.firebase:firebase-client-android:2.3.1'
    compile 'com.firebaseui:firebase-ui:0.6.0'
    compile 'com.firebaseui:firebase-ui-storage:0.6.0'
    compile 'com.firebaseui:firebase-ui-database:0.6.0'
}

apply plugin: 'com.google.gms.google-services'

FirebaseAdapter Initialization

FirebaseListAdapter<Participant> adapter = new FirebaseListAdapter;

Error Messages

Failed to resolve: com.google.firebase:firebase-auth:9.6.0
Failed to resolve: com.google.firebase:firebase-storage:9.6.0
Failed to resolve: com.google.firebase:firebase-database:9.6.0
Failed to resolve: com.google.android.gms:play-services-auth:9.6.0
Cannot resolve symbol FirebaseListAdapter

Fragment with List

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 */
public class ParticipantFragment extends Fragment {
    private DataSource data;
    DatabaseReference database;
    ArrayList<Participant> participants = new ArrayList<>();

    public ParticipantFragment() {
        data = new DummyDataSource();
        database = FirebaseDatabase.getInstance().getReference();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        final View rootView = inflater.inflate(R.layout.content_participants_fragment, container, false);

        FirebaseListAdapter<Participant> adapter = new FirebaseListAdapter;
        ListView listView = (ListView) rootView.findViewById(R.id.participants_list);
        ParticipantAdapter adapter = new ParticipantAdapter(getActivity(), data.getParticipants());
        listView.setAdapter(adapter);

        FloatingActionButton fab = (FloatingActionButton) rootView.findViewById(R.id.participants_fab);
        fab.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(getContext(), NewParticipant.class);
                startActivity(intent);
            }
        });}

Custom Adapter

import android.app.Activity;
import android.media.Image;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by Tim on 9/10/2016.
 */
public class ParticipantAdapter extends ArrayAdapter {
    public ParticipantAdapter(Activity context, ArrayList<Participant> participant) {
        super(context, 0, participant);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View listItemView = convertView;
        if(listItemView == null){
            listItemView = LayoutInflater.from(getContext()).inflate(R.layout.participants_list_item, parent, false);
        }

        Participant currentParticipant = (Participant) getItem(position);

        ImageView participantImage = (ImageView) listItemView.findViewById(R.id.participant_image);
        if(currentParticipant.hasImageValue()){
            participantImage.setImageResource(currentParticipant.getImageID());
        }else{
            participantImage.setImageResource(R.drawable.ic_my_profile);
        }

        TextView name = (TextView) listItemView.findViewById(R.id.participant_name);
        name.setText(currentParticipant.getDisplayName());

        ImageView image = (ImageView) listItemView.findViewById(R.id.participant_role);
        switch (currentParticipant.getRole()){
            case "Official": image.setImageResource(R.drawable.ic_official);
            case "Spectator": image.setImageResource(R.drawable.ic_spectator);
            case "Quizzer": image.setImageResource(R.mipmap.ic_quizzer);
            default: image.setVisibility(View.GONE);
        }

        return listItemView;
    }
}

Where Values are Added

Button okBtn = (Button) findViewById(R.id.add_participant);
        okBtn.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View view) {
                DatabaseReference ref = database.child("participants");
                EditText firstName = (EditText)  findViewById(R.id.first_name);
                EditText lastName = (EditText)  findViewById(R.id.last_name);
                Participant newParticipant = new Participant(firstName.getText().toString(),
                                                                lastName.getText().toString(),
                                                                sp.getSelectedItem().toString(),
                                                                sp2.getSelectedItem().toString());
                Map<String, Object> participantValues = newParticipant.toMap();
                ref.push().setValue(participantValues);
            }
        });

解决方案

The FirebaseUI readme has this handy compatibility table:

FirebaseUI  Firebase/Play 
 Version    Services Version
   1.2.0       10.2.0
   1.1.1       10.0.0 or 10.0.1
   1.0.1       10.0.0 or 10.0.1
   1.0.0       9.8.0
   0.6.2       9.8.0
   0.6.1       9.6.1
   0.6.0       9.6.0
   0.5.3       9.4.0
   0.4.4       9.4.0
   0.4.3       9.2.1
   0.4.2       9.2.0
   0.4.1       9.0.2
   0.4.0       9.0.0

So in order to use FirebaseUI 0.6, you will have to use Firebase/Play Services SDK 9.6. If you want to use Firebase/Play Services SDK 9.4, you will have to use FirebaseUI 0.5.

In addition you're missing an import statement for the list adapter:

import com.firebase.ui.database.FirebaseListAdapter;

Android Studio should already be giving you hints about this import.

这篇关于如何在Android Studio中设置FirebaseAdapter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆