简体   繁体   English

从Firebase中删除,notifyDataSetChanged(); 不刷新ListView

[英]Deleting from Firebase, notifyDataSetChanged(); doesn't refresh ListView

Basically after deleting Object from Firebase, notifyDataSetChanged() does not update it in realtime. 基本上从Firebase中删除Object后, notifyDataSetChanged()不会实时更新它。 I tried to remove object from adapter itself but it also fails. 我试图从适配器本身删除对象,但它也失败了。

Coming back to this fragment shows updated list but i think it's because of child added. 回到这个片段显示更新的列表,但我认为这是因为添加了孩子。 Here's my code. 这是我的代码。

public class ArchiveFragment extends Fragment {

    public static ArchiveFragment newInstance(Beer beer) {
        ArchiveFragment archiveFragment = new ArchiveFragment();
        if (beer != null) {
            Bundle args = new Bundle();
            args.putSerializable("beer", beer);
            archiveFragment.setArguments(args);
        }
        return archiveFragment;
    }

    public ArchiveFragment(){}

    private ArrayList<Beer> beerDB = new ArrayList<>();
    private ListView beerList;
    private RecyclerView beerRec;
    private DatabaseReference mDatabase;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.archive_fragment, container, false);
        final Activity activity = getActivity();
        FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
        beerList = (ListView)view.findViewById(R.id.beerList);
        final ArrayAdapter<Beer> arrayAdapter = new ArrayAdapter<Beer>(getActivity(), android.R.layout.simple_list_item_1,beerDB);
        mDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(user.getUid()).child("BeerList");
        beerList.setAdapter(arrayAdapter);

        beerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Object deletedBeer = beerList.getItemAtPosition(i);
                Beer delBeer = (Beer) deletedBeer;
                deleteBeer(delBeer.getKey());
            }
        });

        mDatabase.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                Beer value = dataSnapshot.getValue(Beer.class);
                value.setKey(dataSnapshot.getKey());
                beerDB.add(value);

                arrayAdapter.notifyDataSetChanged();
            }

            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {}

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

                String key = dataSnapshot.getKey();
                beerDB.remove(key);
                arrayAdapter.notifyDataSetChanged();
            }

            @Override
            public void onChildMoved(DataSnapshot dataSnapshot, String s) {}

            @Override
            public void onCancelled(DatabaseError databaseError) {}
        });

        return view;
    }

    public void deleteBeer(String key){
        mDatabase.child(key).removeValue();
    }
}

You're removing the item from your ArrayList using the remove function which is incorrect. 您正在使用不正确的remove函数从ArrayListremove该项。 The remove function takes the index of the item in your ArrayList which needs to be removed. remove函数获取ArrayList中需要删除的项的索引。 But here, when you get the key from the dataSnapshot it doesn't match any index of your ArrayList and hence the item is not actually removed from your ArrayList . 但是在这里,当您从dataSnapshot获取密钥时,它与您的ArrayList任何索引都不匹配,因此该项目实际上并未从您的ArrayList删除。 So you need to change your onChildRemoved function like this. 所以你需要像这样改变你的onChildRemoved函数。

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {

    String key = dataSnapshot.getKey();

    for (int i = 0; i < beerDB.size(); i++) {
        // Find the item to remove and then remove it by index
        if (beerDB.get(i).key.equals(key)) {
            beerDB.remove(i);
            break;
        } 
    }

    arrayAdapter.notifyDataSetChanged();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM