ValueEventListener不按预期工作 [英] ValueEventListener not Working as expected

查看:108
本文介绍了ValueEventListener不按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在AutoCompleteTextView的下拉菜单中显示存储在Firebase中的数据。为此,我使用 ValueEventListener 。根据 ValueEventListener 的文档,


您可以使用onDataChange()方法读取静态快照在给定路径上的
内容,就像它们在事件发生时所存在的一样。
当这个监听器被连接时,这个方法被触发一次,并且每次数据(包括子节点)发生变化时,
也会再次发生变化。



不幸的是,在我的情况下,只有当数据被改变时(即添加新数据时),才会触发 onDataChange()。这意味着AutoCompleteTextView不会显示下拉菜单,而不会更改Firebase中的数据。我想要的是当监听器被调用并且每次数据改变时,第一次触发 onDataChange()。我想知道我哪里错了。下面的代码出现在片段的 onCreateView

  daTags.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
//基本上,这就是说:对于dataSnapshot中的每个DataSnapshot * Data *,执行方法内部的操作
for(DataSnapshot tagNameSnapshot :dataSnapshot.getChildren()){
//通过输入想要得到的字符串的关键字获得建议
String ValueTagName = tagNameSnapshot.child(getResources()。getString(R.string。 (Value.Target.TagName))。getValue(String.class);
//将ValueTagName(从上面的Key的Firebase中抽取的值)添加到TagList
//最好使用List,因为您不知道dataSnapshot.getChildren()返回的迭代器的大小初始化数组
tagList.add(ValueTagName) ;

//初始化AutoCompleteTextView并定义Adapter
ArrayAdapter< String> adapterAutoComplete = new ArrayAdapter<>(getActivity(),android.R.layout.simple_list_item_1,tagList);
actv_tagName.setAdapter(adapterAutoComplete);

//使用dataSnapshot获取TagsCount并在TextView中显示TagsCount
TagsCount = dataSnapshot.getChildrenCount()+;
tv_tagsCount.setText(TagsCount);
}
});

谢谢

解决方案我认为我明白了这个问题。为了使它工作,我将不得不在 For循环之外移动以下代码行

  ArrayAdapter<字符串> adapterAutoComplete = new ArrayAdapter<>(getActivity(),android.R.layout.simple_list_item_1,tagList); 
actv_tagName.setAdapter(adapterAutoComplete);

在For循环中,每个循环都会更新适配器。将上面的代码放在 For 之外,就可以解决问题。


I am trying to display the data stored in Firebase, in AutoCompleteTextView's Dropdown. For this purpose, I am using the ValueEventListener. According to the documentation of ValueEventListener,

You can use the onDataChange() method to read a static snapshot of the contents at a given path, as they existed at the time of the event. This method is triggered once when the listener is attached and again every time the data, including children, changes.

Unfortunately, in my case onDataChange() is triggered only when data the is changed(that is, when new data is added). This means the AutoCompleteTextView doesn't display the dropdown without any change to the data in Firebase. What I want, is for the onDataChange() to trigger for the first time when the Listener is called and every time the data changes. I would like to know where I am going wrong. The following code appears inside onCreateView of Fragment

daTags.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            //Basically, this says "For each DataSnapshot *Data* in dataSnapshot, do what's inside the method.
            for (DataSnapshot tagNameSnapshot : dataSnapshot.getChildren()) {
                //Get the suggestion by childing the key of the string you want to get.
                String ValueTagName = tagNameSnapshot.child(getResources().getString(R.string.Child_AppData_Tags_TagName)).getValue(String.class);
                //Add ValueTagName (Value pulled from Firebase for the above Key) to TagList
                //Is better to use a List, because you don't know the size of the iterator returned by dataSnapshot.getChildren() to initialize the array
                tagList.add(ValueTagName);

                //Initialize AutoCompleteTextView and define Adapter
                ArrayAdapter<String> adapterAutoComplete = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, tagList);
                actv_tagName.setAdapter(adapterAutoComplete);

                //Get TagsCount using dataSnapshot and display TagsCount in TextView
                TagsCount = dataSnapshot.getChildrenCount() + "";
                tv_tagsCount.setText(TagsCount);
            }
        });

Thanks

解决方案

I think I understood the problem. To make it work, I will have to move the following lines of code outside the For loop

ArrayAdapter<String> adapterAutoComplete = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, tagList);
actv_tagName.setAdapter(adapterAutoComplete);

When inside the For loop, the adapter gets updated for every loop. Placing the above code outside For loop, overcomes the issue.

这篇关于ValueEventListener不按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆