Firebase数据更改后,在Android Studio上更新视图

时间:2017-11-05 10:30:37

标签: android listview firebase firebase-realtime-database

我目前正在开发一个应用程序,它在listView中显示所有存储库数据。在我的片段中,我调用了一个名为fetchRepoData的函数,该函数从Firebase数据库获取数据并放入listView。

OnCreateView

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.repo_result_tab, container, false);

        repoListView = (ListView) view.findViewById(R.id.repoList_result);

        radioGroup = (RadioGroup) view.findViewById(R.id.radio_group);
        sortByNameRadio = (RadioButton) view.findViewById(R.id.sort_by_name);
        sortByRelevanceRadio = (RadioButton) view.findViewById(R.id.sort_by_relevance);

        repoListItems = new ArrayList<>();
        repoNameList = new ArrayList<>();
        userNameList = new ArrayList<>();
        descriptionList = new ArrayList<>();

        adapter = new RepoListAdapter(getContext(), repoListItems);
        repoListView.setAdapter(adapter);

        Database = FirebaseDatabase.getInstance().getReference();
        repoReference = Database.child("Repo Search Results");
        .....

fetchRepoData

private void fetchRepoData(String profileUserID) {

    profileUserDatabase = repoReference.child(profileUserID);

    //DatabaseReference reposReference = profileUserDatabase.child("Repos");

    profileUserDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            Toast.makeText(getActivity(), "changed", Toast.LENGTH_SHORT).show();
            for(DataSnapshot snapshot : dataSnapshot.getChildren()) {

                repoNameList.add(snapshot.getValue(RepoInfo.class).getRepoName());
                userNameList.add(snapshot.getValue(RepoInfo.class).getOwner());
                descriptionList.add(snapshot.getValue(RepoInfo.class).getDescription());
            }

            for(int i = 0; i < repoNameList.size(); i++) {
                repoListItems.add(new RepositoryItem(i, repoNameList.get(i), userNameList.get(i), descriptionList.get(i)));
            }
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

但是,每当我尝试更新Firebase中的数据时,listView都不会更新并保持不变。将显示Toast消息“已更改”,但即使从列表中删除项目,视图也保持不变。

我也尝试过把

repoListItems.clear();

在onDataChange中,但它不起作用。

任何解决方案?

2 个答案:

答案 0 :(得分:0)

我的建议是使用Firebase UI。

这是一种简单有效的方式来更新列表视图或者回收者视图ui。

您可以在此处找到更多详细信息:

https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md

答案 1 :(得分:0)

适配器似乎有问题,没有刷新项目。     你可以在适配器中写一个函数,即

  public void swapItems(ArrayList<String> repoListItems) {
            this.repoListItems = itemList;
            notifyDataSetChanged();
        }

change your code like below:

     public void onDataChange(DataSnapshot dataSnapshot) {

        Toast.makeText(getActivity(), "changed", Toast.LENGTH_SHORT).show();
        repoNameList.clear();
        for(DataSnapshot snapshot : dataSnapshot.getChildren()) {

                    repoNameList.add(snapshot.getValue(RepoInfo.class).getRepoName());
                    userNameList.add(snapshot.getValue(RepoInfo.class).getOwner());
                    descriptionList.add(snapshot.getValue(RepoInfo.class).getDescription());
                }

        for(int i = 0; i < repoNameList.size(); i++) {
            repoListItems.add(new RepositoryItem(i, repoNameList.get(i), userNameList.get(i), descriptionList.get(i)));
                }
            adapter.swapItems(repoNameList);
            }

注意:我已经从stackoverflow中读到了上述内容,但是我没有找到要在此处发布的链接。