在Firebase RecyclerView中添加或删除子项时重复列表行

时间:2018-05-08 21:44:28

标签: android firebase android-recyclerview

当在firebase实时数据库中添加或删除子项时,我得到一个重复的列表行。添加新子项时,重复的一行是旧行。会是什么?

我避免直接​​到达目标孩子,因为我计划从不同的部分检索更多的数据,所以我循环数据快照。我认为这是我个案中最好的选择。

  

DataSnapshot child:dataSnapshot.getChildren()

这就是我的计划:

Os osData = child.child("StaticData").child("os").getValue(Os.class);
Ram ramData = child.child("StaticData")..child("RAM").getValue(Ram.class)
...
...

这是代码。

public class HomeActivity extends AppCompatActivity {

    private static final String TAG = "HomeActivity";
    SharedPreferences sharedPref;
    private String uid;
    private DatabaseReference databaseReference;
    private RelativeLayout emptyListPlaceHolder;

    List<Os> myList = new ArrayList<>();
    RecyclerView rv;
    HomeAdapter homeAdapter;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);
        rv = findViewById(R.id.recycler_view);
        databaseReference = FirebaseDatabase.getInstance().getReference();
        databaseReference.keepSynced(true);
        emptyListPlaceHolder = findViewById(R.id.home_activity_empty_placeholder);

        getSharedPref(); // Gets uid 

        getHomeData();

    }

    private void getHomeData() {

        DatabaseReference userStatsRef = databaseReference.child("stats").child(uid);

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

                int i = 0;

                emptyListPlaceHolder.setVisibility(View.GONE);
                for (DataSnapshot child : dataSnapshot.getChildren()) {
                   Os osData = child
                            .child("StaticData")
                            .child("os")
                            .getValue(Os.class);

                    myList.add(i, osData);

                    i++;

                } // END for loop
                rv.setVisibility(View.VISIBLE);
                rv.setLayoutManager(new LinearLayoutManager(HomeActivity.this));
                rv.setHasFixedSize(false);
                homeAdapter = new HomeAdapter(myList);
                rv.setAdapter(homeAdapter);

            } // END onDataChange

            @Override
            public void onCancelled(DatabaseError error) {
                Log.w(TAG, "Failed to read value.", error.toException());


            }
        });

    }

}

1 个答案:

答案 0 :(得分:2)

找到解决方案。我需要在osList.clear();覆盖方法中添加onDataChange

 private void getHomeData() {

        DatabaseReference userStatsRef = databaseReference.child("stats").child(uid);

        userStatsRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                osList.clear(); // This line solved the issue.

                int i = 0;
                ....
                ....