gpt4 book ai didi

android - 从 Firebase 数据库检索数据并将其设置到 recyclerview 适配器中

转载 作者:行者123 更新时间:2023-12-02 10:45:39 24 4
gpt4 key购买 nike

我想从 firebasedatabase 读取数据并显示到 recyclerview 中通过 orderbychild 方法从 FirebaseDatabase 检索数据。我想读取那个 child 的数据,该 child 的电子邮件与我当前从 app 登录的 userremail 匹配。

这是我的 Firebase:

enter image description here

我已经成功完成了第一部分,但我在第二部分中遇到了问题,如何将检索到的数据传递给 recyclerview 适配器。

例如,每当用户从 mobeen@gmail.com 登录时我读取了他的位置和 Ticket_no 并将该数据显示到 recyclerview 中。我遇到了这条线的问题

programinglist.setAdapter(new myadapter(this,  modl1));

我应该在哪里将我的 modl1 ( ArrayList<model> modl1 ) 传递给 recyclerview.setadapter

public class recyclierview extends AppCompatActivity {
public RecyclerView programinglist;
DatabaseReference b1, b2;
public ArrayList<model> modl1;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recyclierview);
programinglist = findViewById(R.id.my_recycler_view);
programinglist.setLayoutManager(new LinearLayoutManager(this));
modl1 =new ArrayList<model>();
readdata(new firebasecallback() {
@Override
public void oncallback(ArrayList<model> list) {
Log.v("aliraza" , "value oncalback" + list.get(0).getTicket_no());
}
});
}

private void readdata(final firebasecallback firebasecallback)
{
b1= FirebaseDatabase.getInstance().getReference();
b2=b1.child("tasks");
b2.orderByChild("email").equalTo(MainActivity.email).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {


model m = new model();
String location = dataSnapshot.child("location").getValue().toString();
String tickeno = dataSnapshot.child("ticket_no").getValue().toString();
Log.v("ALI", "family value " +location);
Log.v("ALI", "family value " + tickeno);
m.setLocation(location);
Log.v("ALI", "location " +m.getLocation());
m.setTicket_no(tickeno);
modl1.add(m);
Log.v("ALI", "family value " +modl1.get(0).getTicket_no());
firebasecallback.oncallback(modl1);
}

@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

}

@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

}

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

}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});
programinglist.setAdapter(new myadapter(this, modl1));// where should i call this setadapter function of recyclierview.enter code here



}
private interface firebasecallback
{
void oncallback( ArrayList<model> list);
}
}

最佳答案

初始化你的数组列表和适配器;

 public ArrayList<model> modl1=new ArrayList();
private myadapter adapter;

onCreate()方法中使用空白列表初始化适配器并将适配器设置为recyclerview。

adapter=new myadapter(this,  modl1);
programinglist.setAdapter(adapter);

然后在适配器中创建一个方法addItem()

public void addItem(model item){
list.add(item); // add item to your adapter list
notifyItemInserted(list.size());
}

onChildAdded() 方法的更改

modl1.add(m); //remove this line 
firebasecallback.oncallback(m);

readdata(new firebasecallback() {
@Override
public void oncallback(model m) {
modl1.add(m);
adapter.add(m);
Log.v("aliraza" , "value oncalback" + list.get(0).getTicket_no());

}
});

您可以删除 firebasecallback 方法,因为不需要它,并直接在 onChildAdded() 方法中将项目添加到适配器。只需从 onCreate()

调用 readdata() 方法即可
private  void readdata()
{
b1= FirebaseDatabase.getInstance().getReference();
b2=b1.child("tasks");
b2.orderByChild("email").equalTo(MainActivity.email).addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {


model m = new model();
String location = dataSnapshot.child("location").getValue().toString();
String tickeno = dataSnapshot.child("ticket_no").getValue().toString();
Log.v("ALI", "family value " +location);
Log.v("ALI", "family value " + tickeno);
m.setLocation(location);
Log.v("ALI", "location " +m.getLocation());
m.setTicket_no(tickeno);
modl1.add(m);
Log.v("ALI", "family value " +modl1.get(0).getTicket_no());
adapter.add(m);
}

@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

}

@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

}

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

}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});




}

希望这会有所帮助!

关于android - 从 Firebase 数据库检索数据并将其设置到 recyclerview 适配器中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60056369/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com