首页 > 解决方案 > 我如何检索孩子(姓名和列表图像)

问题描述

我正在尝试从 Basic 子项的 List 子项中检索 name 和 list_image。在这里,我从以前的卡中提取了一个 id 来检索内部信息,但我无法检索数据,找不到实际问题。

在此处输入图像描述

public class fund_list extends AppCompatActivity {

private DatabaseReference listref_basic, listref_advanced;
String FundId = "";
private static final String TAG = "MyActivity";
RecyclerView list_recyclerview;
RecyclerView.LayoutManager layoutManager;
FirebaseRecyclerAdapter adapter;

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


    listref_basic = FirebaseDatabase.getInstance().getReference().child("Basic");
    //listref_advanced = FirebaseDatabase.getInstance().getReference().child("Advanced");

    list_recyclerview = (RecyclerView) findViewById(R.id.list_RecyclerView);
    list_recyclerview.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
    list_recyclerview.setLayoutManager(layoutManager);

    if (getIntent() != null) {
        Log.i(TAG, "inside 1st if statement ");
        FundId = getIntent().getStringExtra("FundId");
    }
    if (!FundId.isEmpty()) {
        Log.i(TAG, "inside 2nd if statement ");
        getList(FundId, listref_basic);
    }

    }

private void getList(String fundId, DatabaseReference DR) {
    DR.child(fundId).child("List");
    Log.i(TAG, "inside getlist method ");

    FirebaseRecyclerOptions<List> options =
            new FirebaseRecyclerOptions.Builder<List>()
                    .setQuery(DR, List.class)
                    .build();
    Log.i(TAG, "after options ");
    adapter = new FirebaseRecyclerAdapter<List, List_holder>(options) {
        @Override
        protected void onBindViewHolder(@NonNull List_holder holder, int position, @NonNull List model) {
            holder.setName(model.getName());
            holder.setList_image(getApplicationContext(), model.getList_image());
            Log.i(TAG, "inside onbind");
        }

        @NonNull
        @Override
        public List_holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.list_card, parent, false);

            return new List_holder(view);

        }
    };
    list_recyclerview.setAdapter(adapter);
    adapter.startListening();
    adapter.notifyDataSetChanged();
    Log.i(TAG, "after set adapter ");

}

这是我一直在尝试检索的 firebase 的图像。

标签: javaandroidfirebasefirebase-realtime-database

解决方案


问题出在这个片段中:

DR.child(fundId).child("List");
Log.i(TAG, "inside getlist method ");

FirebaseRecyclerOptions<List> options =
        new FirebaseRecyclerOptions.Builder<List>()
                .setQuery(DR, List.class)
                .build();

你没有存储DR.child(fundId).child("List")任何地方的结果,所以当你传入DR它时setQuery只是指向Basic.

解决方案是存储对用户列表的引用并将其传递到您的FirebaseRecyclerOptions

DatabaseReference usersListReference = DR.child(fundId).child("List");
Log.i(TAG, "inside getlist method ");

FirebaseRecyclerOptions<List> options =
        new FirebaseRecyclerOptions.Builder<List>()
                .setQuery(usersListReference, List.class)
                .build();

推荐阅读