在2个活动中无法从Firebase数据库检索数据

时间:2019-10-19 11:22:33

标签: android database firebase-realtime-database android-recyclerview backend

这是JSON:

{

  "users" : {
    "QTJ1xqpKP5TojSUnbvJkYl6XOWI2" : {
      "Collection" : {
        "-LrOoIXnTA5OgItNyq7x" : {
          "movie" : "x",
          "Name" : "xxxxxx",
          "id" : "-LrOoIXnTA5OgItNyq7x"
        },
        "-LrOp480npJrLLvuJPIE" : {
          "movie" : "xsss",
          "Name" : "yyy",
          "id" : "-LrOp480npJrLLvuJPIE"
        }
      },
      "contributionCounter" : 0,
      "id" : "-LrOoFF3paOUttLnriwU",
      "inventoryCounter" : 0,
      "password" : "123456",
      "reqCollection" : {
        "-LrP3gCtIM7UDhaDuA5U" : {
          "movie" : "y",
          "Name" : "xReq",
          "id" : "-LrP3gCtIM7UDhaDuA5U"
        }
      },
      "requestCounter" : 0,
      "username" : "x"
    }
  }
}

我正在尝试在MainActivity.java中获取child_1数据,并在RequesList.java中获取child_2数据。问题是,安装应用程序后,我能够在必需的活动中检索child_2数据,但在我的主要活动中未显示child_1的数据。同样,在重新启动应用程序之后,每次它在主要活动中向我显示child_1数据时(必填),但不再显示child_2数据。

这是我的MainActivity.java

公共类MainActivity扩展了AppCompatActivity {

FirebaseAuth mAuth;
FirebaseRecyclerAdapter  firebaseRecyclerAdapter;
private FirebaseDatabase mFirebasedatabase;


private DatabaseReference myRef;

private RecyclerView rv;

public  void  add(View view)
{
    Intent i = new Intent(getApplicationContext(), Add.class);
    startActivity(i);
}

public void reqSearch(View view)
{
    Intent i = new Intent(getApplicationContext(), Requestt.class);
    startActivity(i);

}

private void firebaseCollectionSearch() {

    Toast.makeText(this, "clicked", Toast.LENGTH_SHORT).show();
    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
   Query searchQuery ;
   searchQuery=myRef.child(user.getUid()).child("Collection").orderByChild("Name");

    FirebaseRecyclerOptions<Collection> options =
            new FirebaseRecyclerOptions.Builder<Collection>()
                    .setQuery(searchQuery, Collection.class)
                    .build();

firebaseRecyclerAdapter =新的FirebaseRecyclerAdapter(

            options
    ) {
        @Override
        protected void onBindViewHolder(@NonNull CollectionViewHolder holder, final int position, @NonNull final Collection model) {

            holder.setDetails(model.getName(),model.getRName());

            Log.i("request: ",model.getRName());

            holder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(MainActivity.this, model.getId(), Toast.LENGTH_SHORT).show();




                }
            });

        }

        @NonNull
        @Override
        public RequestViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.collection_list, parent, false);

            return new CollectionViewHolder(view);


        }


    };

    rv.setAdapter(firebaseRecyclerAdapter);
}


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


    mFirebasedatabase = FirebaseDatabase.getInstance();
    myRef = mFirebasedatabase.getReference("users");
    mAuth = FirebaseAuth.getInstance();
    rv = (RecyclerView)findViewById(R.id.CollectionListRecyclerView);
    rv.setHasFixedSize(true);
    rv.setLayoutManager(new LinearLayoutManager(this));

    firebaseCollectionSearch();



    ImageButton img = (ImageButton)findViewById(R.id.profile);
    img.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

           // Toast.makeText(getApplicationContext(),"Hello",Toast.LENGTH_LONG).show();
            Intent i = new Intent(getApplicationContext(), ProfileActivity.class);
            startActivity(i);

        }
    });



   // BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.bottomnavigation);
    //navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);



}

public static class CollectionViewHolder extends RecyclerView.ViewHolder{

    View mView;
    public CollectionViewHolder(@NonNull View itemView) {
        super(itemView);

        mView = itemView;
    }

    public void setDetails(String name, String rName)
    {
        TextView s_name = (TextView)mView.findViewById(R.id.SNameSingle);
        TextView r_name = (TextView)mView.findViewById(R.id.RNameSingle);


        s_name.setText(name);
        r_name.setText(rName);


    }




}

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


    if (mAuth.getCurrentUser() == null) {
        finish();
        startActivity(new Intent(this, LoginActivity.class));
    }




}

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

}

这是我的RequestList.java

public class RequestList extends AppCompatActivity {

FirebaseAuth mAuth;
FirebaseRecyclerAdapter firebaseRecyclerAdapter;
private FirebaseDatabase mFirebasedatabase;
private DatabaseReference myRef;
FirebaseUser user;

private RecyclerView rv;

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

    mFirebasedatabase = FirebaseDatabase.getInstance();
    myRef = mFirebasedatabase.getReference("users");
    myRef.keepSynced(true);
    mAuth = FirebaseAuth.getInstance();
    user = FirebaseAuth.getInstance().getCurrentUser();
    rv = (RecyclerView)findViewById(R.id.reqListRecyclerView);
    rv.setHasFixedSize(true);
    rv.setLayoutManager(new LinearLayoutManager(this));

    firebaseRequestSearch();


}

private void firebaseRequestSearch() {


    Query searchQuery =myRef.child(user.getUid()).child("reqCollection");


    FirebaseRecyclerOptions<Requests> options =
            new FirebaseRecyclerOptions.Builder<Requests>()
                    .setQuery(searchQuery, Requests.class)
                    .build();

    firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Requests,RequestListViewHolder>(

            options
    ) {
        @Override
        protected void onBindViewHolder(@NonNull RequestList.RequestListViewHolder holder, final int position, @NonNull final Requests model) {

            holder.setDetails(model.getName(),model.getRName());

            Log.i("req: ",model.getRName());


            holder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(RequestList.this, model.getId(), Toast.LENGTH_SHORT).show();




                }
            });

        }

        @NonNull
        @Override
        public RequestList.RequestListViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.req_list_layout, parent, false);

            return new RequestList.RequestListViewHolder(view);


        }


    };

    rv.setAdapter(firebaseRecyclerAdapter);

}

public static class RequestListViewHolder extends RecyclerView.ViewHolder {

    View mView;

    public RequestListViewHolder(@NonNull View itemView) {
        super(itemView);

        mView = itemView;
    }

    public void setDetails(String name, String rName)
    {
        TextView s_name = (TextView)mView.findViewById(R.id.reqNameSingle);
        TextView r_name = (TextView)mView.findViewById(R.id.reqRNameSingle);


        s_name.setText(name);
        r_name.setText(rName);

    }



}

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


    if (mAuth.getCurrentUser() == null) {
        finish();
        startActivity(new Intent(this, LoginActivity.class));
    }


}

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

}

1 个答案:

答案 0 :(得分:0)

您没有初始化myRef中的MainActivity,而在searchQuery初始化之前添加这些行

mFirebasedatabase = FirebaseDatabase.getInstance();
myRef = mFirebasedatabase.getReference("users");
Query searchQuery;
searchQuery=myRef.child(user.getUid()).child("Collection").orderByChild("Name");

希望它将解决该问题。