在Firebase

时间:2018-05-24 19:38:42

标签: java android firebase firebase-realtime-database

我使用的是ArrayList(在Android中),其中包含Firebase数据库中的值。无论何时添加或删除数据,我都希望更新列表。要做到这一点,我使用了ChildEventListener,但我不确定这是否是一种正确的方法,因为我有时会在从列表中删除元素时遇到错误。将元素添加到列表时完全没有问题。但是当我尝试从列表中删除最后一个元素时,我得到ArrayIndexOutOfBounds异常或其他一些异常,如length=12 index=-1。所以,请仔细阅读我的代码并建议删除元素的更好方法:

    public class Join extends AppCompatActivity {

    DatabaseReference databaseReference;
    ListView listView;
    public static ArrayList<String> keysArrayList; 
    //To store the keys from Firebase
    public static ArrayList<String> namesArrayList; 
    //To store the value or name associated with the key
    ArrayAdapter<String> arrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_join);

    databaseReference = FirebaseDatabase.getInstance().getReference().child(“Queue Codes”);
    listView = (ListView) findViewById(R.id.listViewForMember);
    keysArrayList = new ArrayList<>();
    namesArrayList = new ArrayList<>();
    ArrayAdapter<String> arrayAdapter = new ArrayAdapter<> 
    (this,android.R.layout.activity_list_item,namesArrayList);
    listView.setAdapter(arrayAdapter);

    /* Here I am trying to store Random keys from Firebase in 
    ‘keysArrayList’ and their values in ‘namesArrayList’ and update these 
    lists whenever a value is added or removed from the database*/

     databaseReference.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            String addedKey = dataSnapshot.getKey();
            String addedName = dataSnapshot.getValue(String.class);
            keysArrayList.add(addedKey);               
            namesArrayList.add(addedName);

            arrayAdapter.notifyDataSetChanged();
        }

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

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {
            String removedKey = dataSnapshot.getKey();
            int index = keysArrayList.indexOf(removedKey);
            namesArrayList.remove(index);
            keysArrayList.remove(removedKey);
            arrayAdapter.notifyDataSetChanged();

             /* Here I tried to remove the name from the ‘namesArrayList’ by 
             using the index of removed value from ‘keysArrayList’ as both 
             the lists would be of the same size anytime. I can simply 
             delete the name from the ‘namesArrayList’ by reading the 
             removed value from firebase but gets difficult when I use 
             Custom Array Lists which may contain many other objects. So, I 
             decided to delete the name using key index. */
        }
        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

1 个答案:

答案 0 :(得分:0)

为了解决此问题,您需要首先从适配器中删除该特定元素。如果您不想担心添加或删除元素,我建议您从 post 中查看我的答案,其中我已经解释了如何从Firebase实时数据库中获取数据并显示它使用ListView ArrayAdapter <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="**Your String Value**" /> 。{/ p>