从Firebase检索时,数据未在RecyclerView中加载

时间:2016-09-17 10:23:15

标签: android firebase firebase-realtime-database

我正在尝试从Firebase数据库中检索数据,但我的RecyclerView中没有任何内容。以下是我的MainActivity.java。我不知道我哪里出错了。

package oerrride.we.huzykamz.testingappfirebase;

import android.app.Dialog;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.google.android.gms.common.GoogleApiAvailability;
import com.google.android.gms.tasks.Task;
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 java.util.List;


public class MainActivity extends AppCompatActivity {

    private Button but;
    private DatabaseReference mRef;
    private MyAdapter adapter;
    public RecyclerView recyclerView;

    public List<ModelClass> movies_;
    public Context c;
    private LinearLayoutManager linearLayoutManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        movies_ = new ArrayList<ModelClass>();

        recyclerView =(RecyclerView) findViewById(R.id.recyclerview);

        linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        mRef =  FirebaseDatabase.getInstance().getReferenceFromUrl("https://askdoctor-546bf.firebaseio.com/");




        mRef.child("Name").addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                   LoadData(dataSnapshot);
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                LoadData(dataSnapshot);
            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

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

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });




    }



    public void LoadData (DataSnapshot dataSnapshot){
        for(DataSnapshot singlesnapshot :dataSnapshot.getChildren()){
          String taskTitle =singlesnapshot.getValue(String.class);



            movies_.add(new ModelClass(taskTitle));

            adapter = new MyAdapter(MainActivity.this, movies_);
            recyclerView.setAdapter(adapter);



        }

    }

}

然后这是我的Adapter类。

package oerrride.we.huzykamz.testingappfirebase;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by HUZY_KAMZ on 9/16/2016.
 */
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {


  protected   Context c;
    List<ModelClass> movies;

    public MyAdapter(Context c, List<ModelClass> movies) {
        this.c = c;
        this.movies = movies;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_activity,parent,false);
        MyViewHolder holder = new MyViewHolder(v);
        return null;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {

        holder.name.setText(movies.get(position).getName());


    }

    @Override
    public int getItemCount() {
        return movies.size();
    }
}

这是我的POJO课程。

    [package oerrride.we.huzykamz.testingappfirebase;

/**
 * Created by HUZY_KAMZ on 9/16/2016.
 */
public class ModelClass {

    private String name;


    public ModelClass(){}


    public ModelClass(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

这是我的控制台。这是我想要检索的数据:

1

1 个答案:

答案 0 :(得分:1)

您在Adapter类中的

返回null;

返回你的持有人对象;

return holder;

而不是

return null;

喜欢

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_activity,parent,false);
        MyViewHolder holder = new MyViewHolder(v);
        return holder;
    }