在我的Android Studio项目中,我尝试添加一个功能,该功能在用户登录后,该应用将在firebase实时数据库中获取用户的名称,并获取子节点的名称,“ nomeProf”,具有用户名作为值,并将标签的文本设置为其找到的节点的名称。
my firebase realtimedatabase structure
package com.example.classmob.fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.example.classmob.R;
import com.example.classmob.RecyclerItemClickListener;
import com.example.classmob.activity.alunoActivity;
import com.example.classmob.activity.professorActivity;
import com.example.classmob.adapter.adapterSalas;
import com.example.classmob.model.ContentMain;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
*/
public class salasFragment extends Fragment {
private RecyclerView RecyclerView1;
private List<ContentMain> serie = new ArrayList<>();
private TextView txtSalas;
private String nomeProf, salasAula="";
// private Button btnTentar;
private DatabaseReference reference = FirebaseDatabase.getInstance().getReference("usuarios/");
private DatabaseReference referenceSalas = FirebaseDatabase.getInstance().getReference("salas/");
ArrayList<String> aulaDada = new ArrayList<String>();
public salasFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_salas, container, false);
final Context context = view.getContext();
txtSalas= view.findViewById(R.id.txtSalas);
//btnTentar= view.findViewById(R.id.btnTeste);
salas();
return view; }
public void salas(){
FirebaseAuth autenticacao = FirebaseAuth.getInstance();
String emailUsu= autenticacao.getCurrentUser().getEmail();
reference.orderByChild("email").equalTo(emailUsu).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot datas: dataSnapshot.getChildren()){
nomeProf= datas.child("nome").getValue().toString();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
if(nomeProf != null) {
referenceSalas.orderByChild("nomeProf").equalTo(nomeProf).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot datas : dataSnapshot.getChildren()) {
salasAula += datas.getKey();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
throw databaseError.toException();
}
});
if (!salasAula.equals(""))
txtSalas.setText(salasAula);
else
Toast.makeText(getActivity(), "you dont teach any class yet"/*"Você não dá aula a nenhuma sala ainda"*/, Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getActivity(), "ERROR", Toast.LENGTH_SHORT).show();
}
}
}
我尝试使用此代码来执行此操作,但是它不起作用,我也不知道为什么。它总是最终显示“ ERROR”的吐司消息。我该如何解决?
答案 0 :(得分:1)
您不是在等待响应,而是在查询获得实际结果之前检查select * from Recipe r where r.id in (select rk.Recipe_id from Recipe_keywords rk where rk.keywords like '%:keyword_rec%')
。这是您必须采取的方法:
nomeProf != null