我已尝试过所有内容,但是当我打开应用程序时,我的firebase数据没有自动加载,我必须单击一个edittext来加载数据。
我试过了
adapter.Update(data);
recycler.invalidate();
notifyDataSetChanged();
adapter.notifyDataSetChanged();
请有人帮助我。
编辑:
我正在这样保存到firebase:
public FirebaseHelper(DatabaseReference db) {
this.db = db;
}
//WRITE IF NOT NULL
public Boolean save(Spacecraft spacecraft)
{
if(spacecraft==null)
{
saved=false;
}else
{
try
{
db.child("Spacecraft").push().setValue(spacecraft);
saved=true;
}catch (DatabaseException e)
{
e.printStackTrace();
saved=false;
}
}
return saved;
}
//IMPLEMENT FETCH DATA AND FILL ARRAYLIST
private void fetchData(DataSnapshot dataSnapshot)
{
spacecrafts.clear();
for (DataSnapshot ds : dataSnapshot.getChildren())
{
Spacecraft spacecraft=ds.getValue(Spacecraft.class);
spacecrafts.add(spacecraft);
}
}
//READ THEN RETURN ARRAYLIST
public ArrayList<Spacecraft> retrieve() {
db.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
fetchData(dataSnapshot);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s){
fetchData(dataSnapshot);
}
});
return spacecrafts;
}
}
我的观看者类
public MyViewHolder(View itemView) {
super(itemView);
nameTxt= (TextView) itemView.findViewById(R.id.nameTxt);
propTxt= (TextView) itemView.findViewById(R.id.propellantTxt);
descTxt= (TextView) itemView.findViewById(R.id.descTxt);
itemView.setOnClickListener(this);
}
public void setItemClickListener(ItemClickListener itemClickListener)
{
this.itemClickListener=itemClickListener;
}
@Override
public void onClick(View view) {
this.itemClickListener.onItemClick(this.getLayoutPosition());
}
我的适配器类
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
Context c;
ArrayList<Spacecraft> spacecrafts;
public MyAdapter(Context c, ArrayList<Spacecraft> spacecrafts) {
this.c = c;
this.spacecrafts = spacecrafts;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v=LayoutInflater.from(c).inflate(R.layout.model,parent,false);
return new MyViewHolder(v);
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
final Spacecraft s=spacecrafts.get(position);
holder.nameTxt.setText(s.getName());
holder.propTxt.setText(s.getPropellant());
holder.descTxt.setText(s.getDescription());
holder.setItemClickListener(new ItemClickListener() {
@Override
public void onItemClick(int pos) {
//OPEN DETAI ACTIVITY
openDetailActivity(s.getName(),s.getDescription(),s.getPropellant());
}
});
}
@Override
public int getItemCount() {
return spacecrafts.size();
}
//OPEN DETAIL ACTIVITY
private void openDetailActivity(String...details)
{
Intent i=new Intent(c,DetailActivity.class);
i.putExtra("NAME_KEY",details[0]);
i.putExtra("DESC_KEY",details[1]);
i.putExtra("PROP_KEY",details[2]);
c.startActivity(i);
}
在onCreate中我的主要活动我正在调用这样的数据
recycler = (RecyclerView) rootView.findViewById(R.id.recycler);
recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
database = FirebaseDatabase.getInstance().getReference();
spacecraft = new ArrayList<>();
firebasehelper = new FirebaseHelper(database);
adapter = new JAdapter(getActivity(), firebasehelper.retrieve());
recycler.setAdapter(adapter);
答案 0 :(得分:0)
问题在于retrieve
方法:它添加了一个ChildEventListener
执行异步(意味着它在获取子数据之前不会阻塞)然后它立即返回仍未由异步侦听器代码填充的spacecrafts
列表。
您应该在提取数据的实际代码中刷新RecyclerView
适配器,例如在fetchData
方法中,例如:
private void fetchData(DataSnapshot dataSnapshot)
{
spacecrafts.clear();
for (DataSnapshot ds : dataSnapshot.getChildren())
{
Spacecraft spacecraft=ds.getValue(Spacecraft.class);
spacecrafts.add(spacecraft);
}
adapter.spacecrafts = spacecrafts; // update the list in the adapter
adapter.notifyDataSetChanged(); // refresh
}
在这种情况下,retrieve
方法可以有void
返回类型,因为从中返回列表毫无意义。
答案 1 :(得分:0)
使用FirebaseRecyclerAdapter从Firebase加载数据。
答案 2 :(得分:0)
将helper constructer修改为:
public FirebaseHelper(Context c, DatabaseReference db, ListView lv) {
this.c = c;
this.db = db;
this.lv = lv;
}
然后在for循环后将其添加到fetchData方法:
if (spacecrafts.size() > 0)
{
adapter = new CustomAdapter(c, spacecrafts);
lv.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
然后在主要活动中修改它:
helper = new FirebaseHelper(this, db, lv);
//ADAPTER
//adapter = new CustomAdapter(this, helper.retrieve());
//lv.setAdapter(adapter);
helper.retrieve();
这对你有用。