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

时间:2017-06-23 14:53:52

标签: android

基本上从Firebase删除对象后,notifyDataSetChanged()不会实时更新它。我试图从适配器本身删除对象,但它也失败了。

回到这个片段会显示更新的列表,但我认为这是因为添加了孩子。这是我的代码。

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();
    }
}

1 个答案:

答案 0 :(得分:0)

您正在使用不正确的remove功能从remove删除该项目。 ArrayList函数会获取dataSnapshot中需要删除的项的索引。但是,在这里,当您从ArrayList获取密钥时,它与ArrayList的任何索引都不匹配,因此该项目实际上并未从您的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(); } 功能。

$('#go').on('click', function() {
  var nonempty = $('.number').filter(function() {
    return (this.value) != 0;
    // return isNaN(this.value);
  })

  if (nonempty.length == '') {
    alert('Fail');
  } else {
    alert('Pass');
  }
})