我正在尝试从我的数据库中获取当前登录教师的相同电子邮件从教师那里获取TeacherId
。然后将其存储到字符串变量中,然后将其传递给适配器类以便再次查询。但每次我尝试使用ValueEventListener
检索它时,都会返回null
。
public class SchedList extends AppCompatActivity {
//member vars. ui objects
public ListView SubSchedListView;
public TextView schedList;
public TextView tIDTextView;
//data vars
private String email;
private String teacherid;
private ArrayList<DataSnapshot> snapshot;
//classes
private TeacherAccount ta;
private subjSchedAdapter mAdapter;
//
private DatabaseReference mDatabaseReference;
private SharedPreferences prefs;
//listener
private String x;
private String y;
private String z;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sched_list);
mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("teacheraccount").child("TeacherId");
//ui objects
SubSchedListView = (ListView) findViewById(R.id.SubSchedListView);
schedList = (TextView) findViewById(R.id.schedTextView);
tIDTextView = (TextView) findViewById(R.id.tIDTextView);
//data vars
Intent intent = new Intent();
email = intent.getStringExtra("email");
snapshot = new ArrayList<>();
}
@Override
public void onStart() {
super.onStart();
Intent intent = new Intent();
Query query = mDatabaseReference.orderByChild("Email").equalTo(intent.getStringExtra("email"));
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ta = dataSnapshot.getValue(TeacherAccount.class);
x = ta.getTeacherId(); //the part where it says it returns null
y = dataSnapshot.getValue(String.class);
if (dataSnapshot.getKey().equals("TeacherId")) {
z = dataSnapshot.getValue(String.class);
}
Log.d("WHICH_HAS", x + y + z);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
// Setting the adapter
Log.d("WHICH_HAS", x + y + z);
//mAdapter = new subjSchedAdapter(this, mDatabaseReference,teacherid);
SubSchedListView.setAdapter(mAdapter);
}
@Override
public void onStop() {
super.onStop();
mAdapter.cleanup();
}
}
哪一部分有问题? valueListener
应该在Onstart
吗? OnCreate
?还是靠自己的功能?如果是这样,我在哪个部分调用该函数?我做错了什么其他部分?
如何将字符串数据传递给基本适配器类?
public class subjSchedAdapter extends BaseAdapter{
private Activity mActivity;
private DatabaseReference mDatabaseReference;
private String mTeacherId;
private ArrayList<DataSnapshot> mSnapshotList;
private Query query;
private Context mContext;
// child event listener
private ChildEventListener mListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
mSnapshotList.add(dataSnapshot);
notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
mSnapshotList.add(dataSnapshot);
notifyDataSetChanged();
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
//constructor
public subjSchedAdapter(Activity activity, DatabaseReference ref, String id){
mActivity = activity;
//mTeacherId = subjteacherid;
mDatabaseReference = ref.child("subjects");
//this is where id = the teacherId I retrieved
query = mDatabaseReference.orderByChild("teacherid").equalTo(id).limitToFirst(1);
query.addChildEventListener(mListener);
mSnapshotList = new ArrayList<>();
}
//view holder
static class ViewHolder{
TextView subjectCodeAndName;
TextView time;
TextView sectionName;
TextView teacherId;
LinearLayout.LayoutParams params;
}
@Override
public int getCount() {
return mSnapshotList.size();
}
@Override
public SubjectScheds getItem(int position) {
DataSnapshot snapshot = mSnapshotList.get(position);
return snapshot.getValue(SubjectScheds.class);
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView == null){
LayoutInflater inflater = (LayoutInflater) mActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.subject_sched_list, parent, false);
final ViewHolder holder = new ViewHolder();
holder.subjectCodeAndName = (TextView) convertView.findViewById(R.id.subjectCodeAndName);
holder.time = (TextView) convertView.findViewById(R.id.time);
holder.sectionName = (TextView) convertView.findViewById(R.id.sectionName);
holder.teacherId = (TextView) convertView.findViewById(R.id.teacherid);
holder.params = (LinearLayout.LayoutParams) holder.subjectCodeAndName.getLayoutParams();
convertView.setTag(holder);
}
final SubjectScheds subjectsched = getItem(position);
final ViewHolder holder = (ViewHolder) convertView.getTag();
String subjcodeandname = subjectsched.getSubjectCode() + " - " + subjectsched.getSubjectName();
String time = "time : " + subjectsched.getTime();
String sectionName = "section : " + subjectsched.getSectionname();
String teacherId = "teacher : " + subjectsched.getTeacherid();
holder.subjectCodeAndName.setText(subjcodeandname);
holder.time.setText(time);
holder.sectionName.setText(sectionName);
holder.teacherId.setText(teacherId);
return convertView;
}
public void cleanup(){
mDatabaseReference.removeEventListener(mListener);
}
}
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot finalSnap :dataSnapshot.getChildren()){
ta = finalSnap.getValue(TeacherAccount.class);
x = ta.getTeacherId(); //the part where it says it returns null
tIDTextView.setText(x);
Display(x);
}
}
private void Display(String x){
mAdapter = new subjSchedAdapter(this,mDatabaseReference,x);
SubSchedListView.setAdapter(mAdapter);
}
答案 0 :(得分:0)
如果您想通过电子邮件订购Orderby,那么您的数据库引用是错误的,那么您希望获得对该特定字段的父级的引用。
根据您的情况,您需要按照以下方式查找教师帐户的参考:
mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("teacheraccount");