空引用上的Firebase ArrayList.size()

时间:2017-03-10 12:14:09

标签: android firebase firebase-realtime-database firebase-storage

我正在使用firebase将数据检索到RecyclerView,所以我使用了这些方法,但它给了我一个错误:

public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.PostViewHolder> {
public ArrayList<Getting_Posts> mdataSet;

public RecyclerAdapter(ArrayList<Getting_Posts> dataSet) {
    mdataSet = dataSet;
}

@Override
public RecyclerAdapter.PostViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View inflatedView = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_card_design, parent, false);
    return new RecyclerAdapter.PostViewHolder(inflatedView);}

@Override
public void onBindViewHolder(PostViewHolder holder, int position) {
    Getting_Posts post = mdataSet.get(position);}

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

public void add(int position, Getting_Posts post) {
    mdataSet.add(position, post);
    notifyItemInserted(position);}

public class PostViewHolder extends RecyclerView.ViewHolder {
    TextView Name;
    public PostViewHolder(View inflatedView) {
        super(inflatedView);
        Name = (TextView) inflatedView.findViewById(R.id.PostUsername);}}}

继承我的适配器:

private RecyclerView mPostList;
ArrayList<Getting_Posts> posts;
private RecyclerAdapter mAdapter;

    mPostList = (RecyclerView) v.findViewById(R.id.MainPostList);
    mPostList.setHasFixedSize(true);
    mPostList.setItemAnimator(new DefaultItemAnimator());
    LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
    mPostList.setLayoutManager(layoutManager);
    mAdapter = new RecyclerAdapter(posts);
    mPostList.setAdapter(mAdapter);
    mAuth = FirebaseAuth.getInstance();
    mCurrentUser = mAuth.getCurrentUser();

    //Firebase
    mDatabase = FirebaseDatabase.getInstance().getReference().child("Posts");
    mDatabaseFriends = FirebaseDatabase.getInstance().getReference().child("Following").child(mCurrentUser.getUid());

    return v;
}


@Override
public void onStart() {

    mDatabaseFriends.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for(DataSnapshot following : dataSnapshot.getChildren()) {
                mDatabase.orderByChild("UID").equalTo(following.getKey()).addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        Getting_Posts model = dataSnapshot.getValue(Getting_Posts.class);

                        mAdapter.add(mAdapter.mdataSet.size(), dataSnapshot.getValue(Getting_Posts.class));
                        mAdapter.notifyDataSetChanged();}

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }});}}

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
    super.onStart();
}}

这是我的活动课程:

public class GettingTester {

private String Name;

public GettingTester(String Name) {
    this.Name = Name;
}

public String getName() {
    return Name;
}

public void setName(String name) {
    Name = name;
}

这是我的Getter和setter类:

"Following" : {
    "ORBBKfZAAUhqI1h7ojQDzYOjgkp1" : { 
       "pypD1SYZkbcYesk09WuMUY1AkTf1": true,
      //I need to show this UID posts only but,
  },
  "Posts" : {
    "-KduqnVVczZf5uibQiZ-" : {
      "Describe" : "gg",
      "Time" : 14881230655,
      "UID" : "pypD1SYZkbcYesk09WuMUY1AkTf1",
      "Username" : "Jone",
      "Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped906285501.jpg?alt=media&token=0c1a3a3d-6e48-4c4e-ba59-f5646bf8965f"
    },
    "-Ke5gJ00CxbjhOuhzLIx" : {
      "Describe" : "hajj",
      "Time" : 1488318465,
      "UID" : "pypD1SYZkbcYesk09WuMUY1AkTf1",
      "Username" : "Dom",
      "Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped1717103943.jpg?alt=media&token=a85b2488-5ac7-49a7-9ad0-dbf4e6f29389"
    },
    "-KeCuiFmUCpN19zwsTsR" : {
      "Describe" : "a",
      "Profile" : "https://lh4.googleusercontent.com/-suB77riNoX8/AAAAAAAAAAI/AAAAAAAAAAA/ADPlhfKMzINn-Ki538Sqf6SRGaXC81-WuQ/s200-c/photo.jpg",
      "Time" : 1488439652,
      "UID" : "z2SNUlLd6mQM8nnlkU2VUws5Ggl2",
      "Username" : "Dom",
      "Wallpaper" : "https://firebasestorage.googleapis.com/v0/b/test-3f4c5.appspot.com/o/Posts%2Fcropped1913886685.jpg?alt=media&token=c8ad26b2-8f09-453b-b48a-aad9e4d8b5c3"
    },
}

最后这是我的json:

{{1}}
我只需要为以下用户显示帖子。

1 个答案:

答案 0 :(得分:1)

在此代码中:

mAdapter.add(mAdapter.mdataSet.size(), dataSnapshot.getValue(Getting_Posts.class));

首次执行上述代码时,mdataSetnull。这是为什么?因为在上述代码之前修改mdataSet的唯一代码来自RecyclerAdaptermAdapter)&#39; s init:

mAdapter = new RecyclerAdapter(posts);

mdataSet个对象正在复制posts对象的值,我们知道它确实是空的。

简单地将值放到posts对象上就可以了解这个问题:

ArrayList<Getting_Posts> posts = new ArrayList<>();

希望这有帮助