gpt4 book ai didi

java - 火力地堡用户界面 : Solution to receive all keys from a child and use it to receive information from another node and fill it in a Recyclerview?

转载 作者:行者123 更新时间:2023-11-30 10:07:39 33 4
gpt4 key购买 nike

我正在寻找一种解决方案来从我的 child “药物”中获取数据,并使用 ID 从“medication_plan”节点接收特定数据并将它们显示在 Recyclerview 中。

数据库结构:

目前我正在使用 Firebase UI 来填充我的 Recyclerview

private DatabaseReference mRef;

private RecyclerView mRecyclerView;
private RecyclerView.LayoutManager mLayoutManager;
private FirebaseRecyclerAdapter<Medication,MedicationViewHolder> mAdapter;

List<User> items = new ArrayList<>();

User user = new User();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mRecyclerView = findViewById(R.id.my_recycler_view);
mRecyclerView.setHasFixedSize(true);

mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);

setData();
}

private void setData() {
Query query = FirebaseDatabase.getInstance().getReference().child("Medication_plan");
FirebaseRecyclerOptions<Medication> options = new FirebaseRecyclerOptions.Builder<Medication>()
.setQuery(query,Medication.class)
.build();

mAdapter = new FirebaseRecyclerAdapter<Medication, MedicationViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull MedicationViewHolder holder, int position, @NonNull Medication model) {
final Medication tempmed = new Medication(model.getDr()
,model.getMedicine()
,model.getDate()
,model.getComment());

holder.mDoctor.setText(model.getDr());
holder.mMedicine.setText(model.getMedicine());
holder.mDate.setText(model.getDate());
}

@NonNull
@Override
public MedicationViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.my_text_view,viewGroup,false);
MedicationViewHolder vh = new MedicationViewHolder(v);
return vh;
}
};

mRecyclerView.setAdapter(mAdapter);
}

@Override
protected void onStart() {
mAdapter.startListening();
super.onStart();
}

@Override
protected void onStop() {
mAdapter.stopListening();
super.onStop();
}

当我使用此代码时,我得到了每个药物计划,但我只想要用户提供的具体计划。

最佳答案

添加此函数以获取您的数据:

ArrayList<String> list = new ArrayList<String>();
DatabaseReference ref= FirebaseDatabase.getInstance().getReference("User").child("R3").child("medication");
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snap: dataSnapshot.getChildren()) {
list.add(snap.child("id").getValue(String.class));
}
}

@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "onCancelled", databaseError.toException());
}
});

之后添加此代码以获取第二个数据信息:

ArrayList<String> listOfSomething = new ArrayList<String>();
DatabaseReference ref= FirebaseDatabase.getInstance().getReference("Medication_plan");
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (String s: list) {
listOfSomething.add(dataSnapshot.child("s").child("comment").getValue(String.class));
}
}

@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "onCancelled", databaseError.toException());
}
});

在我的示例中,我检索了评论。您可以将其更改为您想要检索的任何内容。

关于java - 火力地堡用户界面 : Solution to receive all keys from a child and use it to receive information from another node and fill it in a Recyclerview?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54153897/

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