Вот 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"
}
}
}
Я пытаюсь получить данные child_1 в файлах MainActivity.java и child_2 в файле RequesList.java. Проблема в том, что после установки приложения я могу получить данные 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 = new 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();
}
}