我的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"
}
}
}
我的代码来自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) {
}
});
}
}
尽管textview填充得很好,但ListView(lvDayList
)不会填充。我尝试在几个不同的地方初始化arraylist dList
。我认为这与如何填充dList
有关,但如果做错了,我不确定这样做的正确方法。如果那不是问题,那么我在这里画一个空白。任何帮助表示赞赏
答案 0 :(得分:0)
已解决:必须将onDataChange()
中的行从dList = new ArrayList<String>(days.get("days"));
更改为dList.addAll(days.get("days"));
答案 1 :(得分:0)
在onDataChange
,
...
String startTime = cl.get("startTime");
dList.clear();
dList.addAll(days.get("days"));
adapter.notifyDataSetChanged();
String teacherID = cl.get("teacherID");
...
答案 2 :(得分:0)
您不应重新初始化dList,因为listview包含您在实例化适配器时使用的数据集的引用。因此,当您致电adapter.notifyDataSetChanged()
时,它会使用列表的现有引用。重新初始化列表时,它将无效。您需要清除列表并将值添加到列表中,然后调用adapter.notifyDataSetChanged();.有关详细信息,请参阅此SO链接:https://stackoverflow.com/a/23195575/6452886
我刚修改了你的代码以便理解:
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) {
}
});
}
}
答案 3 :(得分:0)
尝试此代码您是多次初始化的适配器,它必须全局初始化它。
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) {
}
});
}
}
答案 4 :(得分:0)
假设您正在实例化并设置适配器onCreate()方法并在onStart()中初始化列表。您应该在onResume()中实例化和设置适配器,如下所示:
public void onResume(){
super.onResume();
adapter = new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, dList);
lvDayList.setAdapter(adapter);
}