我如何检索孩子(姓名和list_image)

时间:2018-07-03 12:20:27

标签: java android firebase firebase-realtime-database

我正在尝试从基本子级的列表子级检索名称和list_image。  在这里,我从前一张卡中获取一个ID以检索内部信息,但是我无法检索数据,找不到实际问题。

enter image description here

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的图像。

2 个答案:

答案 0 :(得分:1)

问题在于以下代码段:

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();

答案 1 :(得分:0)

    // retrieve the data    
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference()
        ref.child("Basic").child("01").child("List").addChildEventListener(new ChildEventListener() {
  @Override
  public void onChildAdded(DataSnapshot dataSnapshot, String prevChildKey) {
// do something here
  }

  @Override
  public void onChildChanged(DataSnapshot dataSnapshot, String prevChildKey) {}

  @Override
  public void onChildRemoved(DataSnapshot dataSnapshot) {}

  @Override
  public void onChildMoved(DataSnapshot dataSnapshot, String prevChildKey) {}

  @Override
  public void onCancelled(DatabaseError databaseError) {}
});