列表视图中的Firebase列表子项

时间:2017-04-20 23:52:01

标签: android listview firebase firebase-realtime-database

我是Firebase的新用户,我需要在列表视图中显示子信息。它添加了数据,但它没有在listview上显示,应用程序崩溃。日志中没有显示错误,但是在firebase崩溃时我出现此错误:

Exception com.google.firebase.database.DatabaseException: Failed to convert value of type java.util.HashMap to String
com.google.android.gms.internal.zzamy.zzcg ()
com.google.android.gms.internal.zzamy.zzb ()
com.google.android.gms.internal.zzamy.zza ()
com.google.firebase.database.DataSnapshot.getValue ()
br.com.imobapp.CorretorFragment$1.onChildAdded (CorretorFragment.java:58)
com.google.android.gms.internal.zzaip.zza ()
com.google.android.gms.internal.zzakp.zzcxi ()
com.google.android.gms.internal.zzaks$1.run ()
android.os.Handler.handleCallback (Handler.java:739)
android.os.Handler.dispatchMessage (Handler.java:95)
android.os.Looper.loop (Looper.java:145)
android.app.ActivityThread.main (ActivityThread.java:5832)

我的数据是这样的(抱歉,由于我的帐户是新的,只能链接,因此无法发布图片):

https://i.stack.imgur.com/lZHFC.jpg

我的模特是:

import com.google.firebase.database.IgnoreExtraProperties;

@IgnoreExtraProperties
public class ModelCorretor {
    private String CorNome;
    private String CorCreci;


    public ModelCorretor(){

    };

    public ModelCorretor(String CorNome, String CorCreci){

        this.CorNome = CorNome;
        this.CorCreci = CorCreci;

    };

    public String getCorNome() {
        return CorNome;
    }

    public void setCorNome(String corNome) {
        CorNome = corNome;
    }

    public String getCorCreci() {
        return CorCreci;
    }

    public void setCorCreci(String corCreci) {
        CorCreci = corCreci;
    }
}

我的片段在listview上显示:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;

import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.util.ArrayList;

import static android.content.ContentValues.TAG;


public class CorretorFragment extends Fragment implements View.OnClickListener {

    public CorretorFragment() {
        // Required empty public constructor
    }

    ImageButton btAddCor, btSearchCor;
    ListView lvCorretor;
    DatabaseReference dref;
    ArrayList<String> list = new ArrayList<>();
    ArrayAdapter<String> adapter;

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


        View view = inflater.inflate(R.layout.fragment_corretor, container, false);
        btAddCor =(ImageButton)view.findViewById(R.id.btAddCor);
        btAddCor.setOnClickListener(this);
        btSearchCor =(ImageButton)view.findViewById(R.id.btSearchCor);
        btSearchCor.setOnClickListener(this);
        lvCorretor = (ListView)view.findViewById(R.id.lvCorretor);
        adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_dropdown_item_1line, list);
        lvCorretor.setAdapter(adapter);

        dref = FirebaseDatabase.getInstance().getReference().child("Corretores");
        dref.addChildEventListener(new ChildEventListener() {
            @Override
            // Read from the database
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                String value = dataSnapshot.getValue(String.class);
                list.add(value);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                // This method is called once with the initial value and again
                // whenever data at this location is updated.
            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {
                String value = dataSnapshot.getValue(String.class);
                list.remove(value);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onCancelled(DatabaseError  error) {
                // Failed to read value
                Log.w(TAG, "Failed to read value.", error.toException());
            }
        });




        return view;
    }


        @Override
        public void onClick(View view){
        MainActivity activity = (MainActivity)getActivity();
            switch(view.getId()){

                case R.id.btAddCor:
                    activity.chamaAddCor();
                    break;
                case R.id.btSearchCor:
                    activity.chamaSearchCor();
                    break;
                default: throw new RuntimeException("unreachable");

            }
        }
}

如果需要更多代码或我在问题中做错了什么,请告诉我。 我有一个想法,我必须设置一个孩子来显示它,就像我在FirebaseDatabase.getInstance().getReference().child("Corretores");中所做的那样,但我尝试了很多,并且不知道如何在onChildAdded上做到这一点。

1 个答案:

答案 0 :(得分:1)

问题在于:

 String value = dataSnapshot.getValue(String.class);

你不应该得到一个String对象,你应该得到你想要的对象的模型。你有一个Corretor模型,所以你可以:

 ModelCorretor value = dataSnapshot.getValue(ModelCorretor.class);

getValue返回一个hashmap,你将它分配给一个字符串,这就是你得到那个错误的原因。 Firebase非常棒,它只需在ModelCorretor方法中插入getValue作为参数即可为您构建该对象。