如何将字符串值传递给适配器类(基本适配器)android JAVA

时间:2018-04-06 03:29:31

标签: java android firebase firebase-realtime-database

我试图显示我的firebase数据库中所有项目的教师ID等于当前登录教师的教师ID,以便获取当前用户的教师ID是我的意思没有。活动类:

    package com.example.ronanlina.attendancechecker;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.firebase.database.ChildEventListener;
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.Query;
import com.google.firebase.database.ValueEventListener;

public class SubjectSchedules extends AppCompatActivity {

    //member vars;
    private String mEmail;
    private ListView mSchedListView;
    private subjSchedAdapter mAdapter;
    private DatabaseReference mDatabaseReference;
    private TeacherAccount ta;
    private Context context;

    public TextView idText;
    public String mTeacherId;

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

        mDatabaseReference = FirebaseDatabase.getInstance().getReference();

        idText = (TextView) findViewById(R.id.teachIdText);
        mSchedListView = (ListView) findViewById(R.id.schedListView);

        //getting the email of the current user
        SharedPreferences prefs = getSharedPreferences(MainActivity.ATTENDANCE_PREFS, MODE_PRIVATE);
        mEmail = prefs.getString(MainActivity.EMAIL_KEY,null);

        context = this;

    }


    @Override
    public void onStart(){
        super.onStart();

        //Retrieving the Teacher Id with the email of the current user

        DatabaseReference db = FirebaseDatabase.getInstance().getReference();
        db.child("teacheraccount"); //

        Query query = db.orderByChild("Email").equalTo(mEmail);

        query.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {

                //transferring datasnaphsot value to TeacherAccount class
                ta = dataSnapshot.getValue(TeacherAccount.class);

                //assign the mTeacherId w/ TeacherAccount teacher id
                mTeacherId = ta.getTeacherId().toString();
                Log.d("teacherID",mTeacherId);

            }

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

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

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

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

        // Setting the adapter

        //Displaying the teacher id value to a textview. (also for checking if datasnaphot did not return null)

        idText.setText(mTeacherId);

        //setting the adapter instance
        mAdapter = new subjSchedAdapter(this, mDatabaseReference,context);

        mSchedListView.setAdapter(mAdapter);

        mSchedListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            // @Override
            public void onItemClick(AdapterView<?> adapter, View view, int position, long arg) {

                SubjectScheds infoPass = mAdapter.getItem(position);
                String sectionTitle = infoPass.sectionname;

                Intent intent = new Intent(SubjectSchedules.this, activity_student_list.class);
                intent.putExtra("SECTION_TITLE", sectionTitle);
                startActivity(intent);
            }
        });

    }

    @Override
    public void onStop(){
        super.onStop();

        mAdapter.cleanup();
    }

    private void showErrorDialog(String message) {
        new AlertDialog.Builder(this)
                .setTitle("Oops")
                .setMessage(message)
                .setPositiveButton(android.R.string.ok, null)
                .setIcon(android.R.drawable.ic_dialog_alert)
                .show();
    }

}

我的基本适配器类:

    package com.example.ronanlina.attendancechecker;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;

import org.w3c.dom.Text;

import java.util.ArrayList;


public class subjSchedAdapter extends BaseAdapter{

    private Activity mActivity;
    private DatabaseReference mDatabaseReference;
    private String mTeacherId;
    private ArrayList<DataSnapshot> mSnapshotList;
    private Query query;

    // 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) {

        }

        @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, Context context){

        mActivity = activity;
        //mTeacherId = subjteacherid;
        mDatabaseReference = ref.child("subjects");

                                                                     //getting the text displayed text from textview (teacher id)
        query = mDatabaseReference.orderByChild("teacherid").equalTo(((SubjectSchedules)context).idText.getText().toString());

        query.addChildEventListener(mListener);

        mSnapshotList = new ArrayList<>();
    }

每次我在手机上运行程序并启动活动时,它都不会在列表视图中显示任何内容,但是当我锁定手机然后将其解锁时,它会显示结果。

编辑:数据库结构

enter image description here

1 个答案:

答案 0 :(得分:1)

要解决此问题,请更改以下代码行:

Query query = db.orderByChild("Email").equalTo(mEmail);

Query query = db.child("teacheraccount").orderByChild("Email").equalTo(mEmail);