0

I am using an ArrayList (in Android) that contains the values from the Firebase database. Whenever the data is added or deleted, I want the list to be updated. To do this I used ChildEventListener but I am unsure whether this is a proper way of doing it as I face errors sometimes while deleting the elements from the list. There's no problem at all when adding elements to the list. But When I try to delete the last element from the list, I get ArrayIndexOutOfBounds Exception or some other Exception like length=12 index=-1. So, please go through my code and suggest a better way to delete elements:

    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) {

        }
    });
}
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • when you update your arraylist you are adding elements to it.. so at start the current index of the array is different than the last element that you deleted, so thats why you are getting indexoutofbounds, please check https://www.geeksforgeeks.org/understanding-array-indexoutofbounds-exception-in-java/ – Gastón Saillén May 24 '18 at 20:51
  • Thank you. But could you suggest me the changes I need to make in my code? The code works well most of the times. I only get the exception some times. That's why I do not understand where I went wrong. – Vasanth Tulasi May 24 '18 at 21:56

1 Answers1

0

In order to solve this problem, you need to remove that particular element first from the adapter. If you want not to worry about adding or removing elements, I recommend you see my answer from this post, in which I have explained how you can get data from the Firebase Realtime database and display it in a ListView using an ArrayAdapter.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Is there everything alright? Have you solved the issue? – Alex Mamo May 26 '18 at 06:33
  • No. The problem still persists. Even when I notify the ArrayAdapter after removing an element from the list, I get this error sometimes. I believe that there's something wrong with code inside onChildRemoved method that I used. I just can't see what it is. – Vasanth Tulasi Jun 01 '18 at 16:40
  • Have you tried to use the answer from this **[post](https://stackoverflow.com/questions/48622480/showing-firebase-data-in-listview)**? – Alex Mamo Jun 04 '18 at 10:47