未连接适配器;使用FirebaseRecyclerAdapter跳过布局

时间:2019-01-26 17:53:47

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

关于此FirebaseRecyclerAdapter,我有一个问题,我读了很多有关此问题的文章,但是没有人解决我的问题。我已经使用FirebaseRecyclerAdapter很多次了,并且可以按我的需要工作,但是这次没有连接任何适配器。跳过布局并终止应用程序

预先感谢

模型类

public class ModelClass {
private String Titile, Disc, Image;
public ModelClass(String titile, String disc, String image) {
    Titile = titile;
    Disc = disc;
    Image = image;
}

public ModelClass() {
}

public String getTitile() {
    return Titile;
}

public void setTitile(String titile) {
    Titile = titile;
}

public String getDisc() {
    return Disc;
}

public void setDisc(String disc) {
    Disc = disc;
}

public String getImage() {
    return Image;
}

public void setImage(String image) {
    Image = image;
}

}

主页

public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private FirebaseDatabase database;
private DatabaseReference mRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    database = FirebaseDatabase.getInstance();
    mRef = database.getReference();
    recyclerView = (RecyclerView)findViewById(R.id.MainRecylerView1);
    LinearLayoutManager manager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(manager);
    recyclerView.setHasFixedSize(true);
    RetriveData();


}

@Override
protected void onStart() {
    super.onStart();

}

public void RetriveData(){
    mRef.child("Users").addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()){


                final FirebaseRecyclerAdapter<ModelClass, ViewCalssHolder>firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<ModelClass, ViewCalssHolder>
                        (
                                ModelClass.class,
                                R.layout.display_images_and_titile,
                                ViewCalssHolder.class,
                                mRef
                        ) {
                    @Override
                    protected void populateViewHolder(ViewCalssHolder viewHolder, ModelClass model, int position) {
                        viewHolder.setTitile(model.getTitile().toString());
                        viewHolder.setDisc(model.getDisc().toString());


                    }

                };
                recyclerView.setAdapter(firebaseRecyclerAdapter);

          }


        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}


public class ViewCalssHolder extends RecyclerView.ViewHolder{
    View mView;

    public ViewCalssHolder(View itemView) {
        super(itemView);
        mView = itemView;
    }

    public void setTitile(String titile) {
        TextView T = itemView.findViewById(R.id.post_title);
        T.setText(titile);
    }
    public void setDisc(String disc) {
        TextView T = itemView.findViewById(R.id.post_desc);
        T.setText(disc);

    }
    public void setImage(String image) {

    }

}

}

1 个答案:

答案 0 :(得分:0)

在onStart和onStop方法中添加这些行:

重写onStop(){

super.onStop()

adapter.stopListening()

}

覆盖onStart()的乐趣{

super.onStart()

adapter.startListening()

}