为什么数组列表的大小是0,即使它已经装满了。

huangapple 未分类评论44阅读模式
英文:

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(&quot;kullanici_kelime&quot;);
    private ArrayList&lt;User_word&gt; user_words= new ArrayList&lt;&gt;();
        @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(&quot;mean&quot;).getValue(String.class)) ;
                    user_word.setName(postSnapshot.child(&quot;name&quot;).getValue(String.class));
                    user_word.setKey(postSnapshot.child(&quot;key&quot;).getValue(String.class));
                    user_word.setId(postSnapshot.child(&quot;id&quot;).getValue(String.class));
                    user_word.setDate(postSnapshot.child(&quot;date&quot;).getValue(Long.class));
                    user_word.setLevel(postSnapshot.child(&quot;level&quot;).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(&quot;size&quot;, String.valueOf(user_words.size()));// thats my problem
        adapter= new CardAdapter2_2_2(getActivity(),user_words);
        rv.setAdapter(adapter);
                return view;
    }

答案1

得分: 0

这是因为onCreateViewonStart之前以及databaseReference2调用其ValueEventListener之前被调用。

英文:

This is because onCreateView is called prior to onStart and prior todatabaseReference2 calling its ValueEventListeners

答案2

得分: -1

  1. 确保 dataSnapshot.getChildern() 有子项,并进入 for 循环。
  2. 确保 onDataChange() 被调用。
  3. 确保在数组已经初始化之后,onCreateView() 被调用。

无论如何,没有您代码的其他部分很难判断。

英文:
  1. Make sure dataSnapshot.getChildern() HAS children and it goes into the for loop.
  2. Make sure that onDataChange() gets called.
  3. 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.

huangapple
  • 本文由 发表于 2020年4月9日 05:07:39
  • 转载请务必保留本文链接:https://java.coder-hub.com/61110044.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定