英文:
Why the arraylist's size is 0 even it is full
问题
我有一个数组列表(user_word)。
我将它发送给CardAdapter2_2_2,并且它会显示数组列表中的每个项目。
但是user_word.size()返回0。为什么?
private DatabaseReference databaseReference2 = FirebaseDatabase.getInstance().getReference("kullanici_kelime");
private ArrayList<User_word> user_words = new ArrayList<>();
@Override
public void onStart(){
super.onStart();
databaseReference2.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
user_words.clear();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
User_word user_word = new User_word();
user_word.setMean(postSnapshot.child("mean").getValue(String.class));
user_word.setName(postSnapshot.child("name").getValue(String.class));
user_word.setKey(postSnapshot.child("key").getValue(String.class));
user_word.setId(postSnapshot.child("id").getValue(String.class));
user_word.setDate(postSnapshot.child("date").getValue(Long.class));
user_word.setLevel(postSnapshot.child("level").getValue(int.class));
user_words.add(user_word);
}
adapter.notifyDataSetChanged();
}
});
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_frag3, container, false);
Timestamp ts = new Timestamp(System.currentTimeMillis());
Date date = new Date(ts.getTime());
Log.e("size", String.valueOf(user_words.size())); // 这是我的问题
adapter = new CardAdapter2_2_2(getActivity(), user_words);
rv.setAdapter(adapter);
return view;
}
英文:
I have an arraylist (user_word).
I send it to CardAdapter2_2_2 and it shows every item that is in the arraylist.
But user_word.size() returns 0. Why?
private DatabaseReference databaseReference2 = FirebaseDatabase.getInstance().getReference("kullanici_kelime");
private ArrayList<User_word> user_words= new ArrayList<>();
@Override
public void onStart(){
super.onStart();
databaseReference2.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
user_words.clear();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
User_word user_word= new User_word();
user_word.setMean(postSnapshot.child("mean").getValue(String.class)) ;
user_word.setName(postSnapshot.child("name").getValue(String.class));
user_word.setKey(postSnapshot.child("key").getValue(String.class));
user_word.setId(postSnapshot.child("id").getValue(String.class));
user_word.setDate(postSnapshot.child("date").getValue(Long.class));
user_word.setLevel(postSnapshot.child("level").getValue(int.class));
user_words.add(user_word); }
adapter.notifyDataSetChanged(); } }); }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.fragment_frag3, container,false);
Timestamp ts=new Timestamp(System.currentTimeMillis());
Date date=new Date(ts.getTime());
Log.e("size", String.valueOf(user_words.size()));// thats my problem
adapter= new CardAdapter2_2_2(getActivity(),user_words);
rv.setAdapter(adapter);
return view;
}
答案1
得分: 0
这是因为onCreateView
在onStart
之前以及databaseReference2
调用其ValueEventListener
之前被调用。
英文:
This is because onCreateView
is called prior to onStart
and prior todatabaseReference2
calling its ValueEventListener
s
答案2
得分: -1
- 确保
dataSnapshot.getChildern()
有子项,并进入 for 循环。 - 确保
onDataChange()
被调用。 - 确保在数组已经初始化之后,
onCreateView()
被调用。
无论如何,没有您代码的其他部分很难判断。
英文:
- Make sure dataSnapshot.getChildern() HAS children and it goes into the for loop.
- Make sure that onDataChange() gets called.
- Make sure that onCreateView() gets called after your array is already
initialized.
Anyway it's hard to tell without having the rest of you code.
专注分享java语言的经验与见解,让所有开发者获益!
评论