简体   繁体   中英

ListView will not populate data from Firebase

My JSON:

{
    "Classes": {
        "host101": {
            "classID": "host101",
            "className": "host test",
            "days": ["monday", "wednesday", "friday"],
            "gpsLocation": "999",
            "roster": {
                "7MFUgOkcQbStn1nBFsVR4ZPWbmo2": {
                    "firstName": "Student",
                    "id": "7MFUgOkcQbStn1nBFsVR4ZPWbmo2",
                    "lastName": "test1",
                    "studentEmail": "ss@ss.com"
                }
            },
            "startTime": "2000",
            "teacherID": "Zb0cLGSz0YX5WYFSXPc4TJ5M8mf2"
        }
    },
    "Users": {
        "7MFUgOkcQbStn1nBFsVR4ZPWbmo2": {
            "Classes": {
                "host101": {
                    "classID": "host101",
                    "className": "host test",
                    "days": ["monday", "wednesday", "friday"],
                    "startTime": "2000"
                }
            },
            "firstName": "Student",
            "id": "7MFUgOkcQbStn1nBFsVR4ZPWbmo2",
            "lastName": "test1",
            "studentEmail": "ss@ss.com"
        },
        "Zb0cLGSz0YX5WYFSXPc4TJ5M8mf2": {
            "Hosted Classes": {
                "host101": {
                    "classID": "host101",
                    "className": "host test",
                    "days": ["monday", "wednesday", "friday"],
                    "gpsLocation": "999",
                    "roster": {
                        "7MFUgOkcQbStn1nBFsVR4ZPWbmo2": {
                            "firstName": "Student",
                            "id": "7MFUgOkcQbStn1nBFsVR4ZPWbmo2",
                            "lastName": "test1",
                            "studentEmail": "ss@ss.com"
                        }
                    },
                    "startTime": "2000",
                    "teacherID": "Zb0cLGSz0YX5WYFSXPc4TJ5M8mf2"
                }
            },
            "firstName": "Teacher",
            "id": "Zb0cLGSz0YX5WYFSXPc4TJ5M8mf2",
            "lastName": "test",
            "studentEmail": "tt@tt.com"
        }
    }
}

My code from TeacherClassDetail.class :

public class TeacherClassDetail extends AppCompatActivity {

    //in progress. teacher will set their question here and be able to take attendance and other teacher stuff...
    final DatabaseReference fbRef = FirebaseDatabase.getInstance().getReference();
    final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    Button setQuestion;
    Button removeClass;
    TextView tvDetailName;
    TextView tvID;
    TextView tvStart;
    ListView lvDayList;
    ArrayList<String> dList = new ArrayList<String>();

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

        final ArrayAdapter<String> adapter = new ArrayAdapter<String>
                (this, android.R.layout.simple_list_item_1, dList);

        lvDayList = (ListView) findViewById(R.id.lvDays);
        setQuestion = (Button) findViewById(R.id.bSetQuestion);
        removeClass = (Button) findViewById(R.id.bRemoveClass);
        tvDetailName = (TextView) findViewById(R.id.tvCDetailName);
        tvID = (TextView) findViewById(R.id.tvCDetailID);
        tvStart = (TextView) findViewById(R.id.tvCDetailStart);

    }

    protected void onStart(){
        super.onStart();

        final ArrayAdapter<String> adapter = new ArrayAdapter<String>
                (TeacherClassDetail.this, android.R.layout.simple_list_item_1, dList);

        lvDayList.setAdapter(adapter);

        final String ID = getIntent().getStringExtra("ID");
        Query query = fbRef.child("Users").child(user.getUid()).child("Hosted Classes").child(ID);

        query.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                //getting string values
                Map<String, String> cl = (Map<String, String>) dataSnapshot.getValue();
                //getting arraylist values
                Map<String, ArrayList<String>> days = (Map<String, ArrayList<String>>) dataSnapshot.getValue();
                //getting arraylist of objects(roster)
                Map<String, Map<String, Object>> roster = (Map<String, Map<String, Object>>) dataSnapshot.getValue();

                //assigning that data to variables
                String id = cl.get("classID");
                String className = cl.get("className");
                String gpsLocation = cl.get("gpdLocation");
                String startTime = cl.get("startTime");
                dList = new ArrayList<String>(days.get("days"));
                adapter.notifyDataSetChanged();
                String teacherID = cl.get("teacherID");

                //creating ClassObj for use in student "my classes" list
                ClassObj newClass = new ClassObj(className, id, startTime, dList, gpsLocation);

                tvDetailName.setText("Class Name: " + newClass.getClassName());
                tvID.setText("Class ID: " + newClass.getClassID());
                tvStart.setText("Start Time(MilTime): " + newClass.getStartTime());


            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }
}

The ListView ( lvDayList ) will not populate though the textviews are populating just fine. I have tried initializing the arraylist dList in a few different places. I think it has something to do with how im populating the dList but i am not sure of the correct way to do so if it is done wrong. If that is not the issue than I am kind of drawing a blank here. Any help is appreciated

SOLVED: had to change the line in onDataChange() from dList = new ArrayList<String>(days.get("days")); to dList.addAll(days.get("days"));

Try this code inside onDataChange ,

...
String startTime = cl.get("startTime");

dList.clear();
dList.addAll(days.get("days"));
adapter.notifyDataSetChanged();

String teacherID = cl.get("teacherID");
...

You should not reinitialize the dList because the listview holds a reference for the dataset you used when instantiating the adapter. So, when you call adapter.notifyDataSetChanged() , it uses the existing reference of the list. When you reinitialise the list, it will have no effect. You need to clear the list and add the values to the list and then call adapter.notifyDataSetChanged();. Please refer this SO link for details: https://stackoverflow.com/a/23195575/6452886

I just modified your code for understanding:

public class TeacherClassDetail extends AppCompatActivity {

    //in progress. teacher will set their question here and be able to take attendance and other teacher stuff...
    final DatabaseReference fbRef = FirebaseDatabase.getInstance().getReference();
    final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    Button setQuestion;
    Button removeClass;
    TextView tvDetailName;
    TextView tvID;
    TextView tvStart;
    ListView lvDayList;
    ArrayList<String> dList = new ArrayList<String>();

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

        final ArrayAdapter<String> adapter = new ArrayAdapter<String>
                (this, android.R.layout.simple_list_item_1, dList);

        lvDayList = (ListView) findViewById(R.id.lvDays);
        setQuestion = (Button) findViewById(R.id.bSetQuestion);
        removeClass = (Button) findViewById(R.id.bRemoveClass);
        tvDetailName = (TextView) findViewById(R.id.tvCDetailName);
        tvID = (TextView) findViewById(R.id.tvCDetailID);
        tvStart = (TextView) findViewById(R.id.tvCDetailStart);

    }

    protected void onStart(){
        super.onStart();

        final ArrayAdapter<String> adapter = new ArrayAdapter<String>
                (TeacherClassDetail.this, android.R.layout.simple_list_item_1, dList);

        lvDayList.setAdapter(adapter);

        final String ID = getIntent().getStringExtra("ID");
        Query query = fbRef.child("Users").child(user.getUid()).child("Hosted Classes").child(ID);

        query.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                //getting string values
                Map<String, String> cl = (Map<String, String>) dataSnapshot.getValue();
                //getting arraylist values
                Map<String, ArrayList<String>> days = (Map<String, ArrayList<String>>) dataSnapshot.getValue();
                //getting arraylist of objects(roster)
                Map<String, Map<String, Object>> roster = (Map<String, Map<String, Object>>) dataSnapshot.getValue();

                //assigning that data to variables
                String id = cl.get("classID");
                String className = cl.get("className");
                String gpsLocation = cl.get("gpdLocation");
                String startTime = cl.get("startTime");
                dList.clear();
                dList.addAll(days.get("days”));
                adapter.notifyDataSetChanged();

                              String teacherID = cl.get("teacherID");

                //creating ClassObj for use in student "my classes" list
                ClassObj newClass = new ClassObj(className, id, startTime, dList, gpsLocation);

                tvDetailName.setText("Class Name: " + newClass.getClassName());
                tvID.setText("Class ID: " + newClass.getClassID());
                tvStart.setText("Start Time(MilTime): " + newClass.getStartTime());


            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });

    }
}

Try this code you are multiple time initialized adapter and it has to globally initialize it.

public class TeacherClassDetail extends AppCompatActivity {

        //in progress. teacher will set their question here and be able to take attendance and other teacher stuff...
        final DatabaseReference fbRef = FirebaseDatabase.getInstance().getReference();
        final FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
        Button setQuestion;
        Button removeClass;
        TextView tvDetailName;
        TextView tvID;
        TextView tvStart;
        ListView lvDayList;
        ArrayAdapter<String> adapter;
        ArrayList<String> dList = new ArrayList<String>();

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_teacher_class_detail);
            lvDayList = (ListView) findViewById(R.id.lvDays);
            setQuestion = (Button) findViewById(R.id.bSetQuestion);
            removeClass = (Button) findViewById(R.id.bRemoveClass);
            tvDetailName = (TextView) findViewById(R.id.tvCDetailName);
            tvID = (TextView) findViewById(R.id.tvCDetailID);
            tvStart = (TextView) findViewById(R.id.tvCDetailStart);
            adapter = new ArrayAdapter<String>
                    (this, android.R.layout.simple_list_item_1, dList);
             lvDayList.setAdapter(adapter);

        }

        protected void onStart(){
            super.onStart();



            final String ID = getIntent().getStringExtra("ID");
            Query query = fbRef.child("Users").child(user.getUid()).child("Hosted Classes").child(ID);

            query.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    //getting string values
                    Map<String, String> cl = (Map<String, String>) dataSnapshot.getValue();
                    //getting arraylist values
                    Map<String, ArrayList<String>> days = (Map<String, ArrayList<String>>) dataSnapshot.getValue();
                    //getting arraylist of objects(roster)
                    Map<String, Map<String, Object>> roster = (Map<String, Map<String, Object>>) dataSnapshot.getValue();

                    //assigning that data to variables
                    String id = cl.get("classID");
                    String className = cl.get("className");
                    String gpsLocation = cl.get("gpdLocation");
                    String startTime = cl.get("startTime");
                   // dList = new ArrayList<String>(days.get("days"));
                    dList.clear();//clear your list
                    dList.add(days.get("days")); // add to the list
                    adapter.notifyDataSetChanged();
                    String teacherID = cl.get("teacherID");

                    //creating ClassObj for use in student "my classes" list
                    ClassObj newClass = new ClassObj(className, id, startTime, dList, gpsLocation);

                    tvDetailName.setText("Class Name: " + newClass.getClassName());
                    tvID.setText("Class ID: " + newClass.getClassID());
                    tvStart.setText("Start Time(MilTime): " + newClass.getStartTime());


                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

        }
    }

Lets say you are instantiating and setting the adapter onCreate() method and initializing your list in onStart(). You should instantiate and set the adapter in onResume() as follows:

public void onResume(){
    super.onResume();
        adapter = new ArrayAdapter<String>
                (this, android.R.layout.simple_list_item_1, dList);
         lvDayList.setAdapter(adapter); 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM