sjso
sjso

Reputation: 297

Not getting correct data in ListView

I have an Arraylist checkOutBook containing keys. Its printing correctly to log message. However - i only want the books with matching the key of checkOutBook in the listview, But however im getting all books in database.

In the onPostResume method, Im checking the each key of the firebase realtime database with the checkOutBook Sting list, If only true, it should be added. But its getting true for each of the key in the database, however that is not present in the checkOutBook array. I dont know whats the issue here. enter image description here

private ArrayList<String>  checkOutBook;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        ...

        checkOutBook = new ArrayList<>();
        Intent intent = getIntent();

        checkOutBook =intent.getStringArrayListExtra("value");

        Log.i("vvvv",checkOutBook.toString());

 ....

    }

@Override
    protected void onPostResume() {
        super.onPostResume();
        Log.i("yyy","onresume");
        mAdapter.clear();


        //to check if the university ID is already registered
        mDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Log.i("GTA","HHH");
                for (DataSnapshot postSnapshot: dataSnapshot.getChildren()) {
                    if(checkOutBook.contains(postSnapshot.getKey()));
                    {
                        Log.i("RRR","HHH");
                        BooksWithKey book = new BooksWithKey(postSnapshot.getKey(), postSnapshot.child("bookName").getValue().toString(), postSnapshot.child("author").getValue().toString(),
                                postSnapshot.child("copies").getValue().toString(), postSnapshot.child("publisher").getValue().toString(), postSnapshot.child("yearPublish").getValue().toString(), postSnapshot.child("callNumber").getValue().toString());
                        mAdapter.add(book);
                    }
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                // Getting Post failed, log a message
                Log.w("TAG", "loadPost:onCancelled", databaseError.toException());

            }
        });


    }

Upvotes: 0

Views: 53

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

To solve this, combine this three lines of code:

private ArrayList<String>  checkOutBook;
checkOutBook = new ArrayList<>();
checkOutBook =intent.getStringArrayListExtra("value");

in

ArrayList<String> checkOutBook = intent.getStringArrayListExtra("value");

Move this new line of code in your onDataChange() method right after this line: Log.i("GTA","HHH");.

Upvotes: 1

Related Questions