从Firebase

时间:2016-09-18 17:37:52

标签: android firebase firebase-realtime-database

以下是我在Firebase中的数据的图片:

1

因此,当他们是recyclerView的项目列表中的两位医生时,我正在尝试在Recyclerview中检索DoctorsNameEmail。但我不知道如何使用get()方法在我的Recycleriew中检索它们。 这是我得到的错误

java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference

以下是我的MainActiviy.java

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 com.google.firebase.database.ValueEventListener;

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


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;

    public DatabaseReference mMovieRef ;


    @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);
        recyclerView.setHasFixedSize(true);
        mRef =  FirebaseDatabase.getInstance().getReferenceFromUrl("https://askdoctor-546bf.firebaseio.com/Doctors");
       //  mMovieRef = mRef.child("Doctors");


        mRef.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班级。

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

import com.google.firebase.FirebaseApp;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.squareup.picasso.Picasso;

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;
    FirebaseStorage storage = FirebaseStorage.getInstance();

   StorageReference url =storage.getReferenceFromUrl("gs://askdoctor-546bf.appspot.com/");

    private List<ModelClass> itemList = null;


    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 holder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        final  ModelClass feedItems = itemList.get(position);

        holder.name.setText(movies.get(position).getName());
        holder.email_txt.setText(movies.get(position).getEmail());
      // PicassoClient.downloadImage(c,movies.get(position).getUrl(),holder.img);
        //Download image using picasso library
        Picasso.with(c).load(url+ feedItems.getName())
                .error(R.mipmap.ic_launcher).
                resize(120, 50)
                .placeholder(R.mipmap.ic_launcher).into(holder.img);




    }

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

1 个答案:

答案 0 :(得分:0)

在你的onBindViewHolder

final ModelClass feedItems = itemList.get(position);

itemList为null ,您尝试从空列表中获取内容,这就是为什么会出现java.lang.NullPointerException。