Catalin Ghita
Catalin Ghita

Reputation: 826

Android App can't write data to Firebase

I am trying to build an android app that displays info about books (using Google books API).I have recently added firebase to the app.

At this moment I can log in or register to the app(new users appear in FB console), I can search for books (with search view) and I have added a button for every book listed in the ListView that is created from the query.

Next, I added functionality to the button as it follows: the button (if pressed) should add the current book(in the Listview) to FireBase in the current user's "library". I have written this code in my custom AdapterClass so I can add the current book (from the ListView) like this:

CustomAdapter Class:

public class BookAdapter extends ArrayAdapter<BookObject> implements View.OnClickListener {
BookObject mCurrentBook;
public BookAdapter(Activity context, List<BookObject> books) {
    super(context, 0, books); // 2nd par is 0 because we inflate manually
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
   ...
    }

    BookObject currentBook = getItem(position);
    mCurrentBook = currentBook;

    TextView TitleTextView = (TextView) listItemView.findViewById(R.id.Title_text_view);
    TitleTextView.setText(currentBook.getTitle());

   ...
    ImageView ThumbnailImageView = (ImageView) listItemView.findViewById(R.id.book_image_id);
    Picasso.with(UserActivity.mMainActivity).load(currentBook.getImageURL()).into(ThumbnailImageView);

    Button SaveBookbutton = (Button) listItemView.findViewById(R.id.button_id);
    SaveBookbutton.setOnClickListener(this);

    return listItemView;
}

@Override
public void onClick(View v) {

    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    String uid = user.getUid();

    DatabaseReference bookRef = FirebaseDatabase
            .getInstance()
            .getReference(Constants.FIREBASE_CHILD_BOOKS) // this constant is "books"
            .child(uid);

    DatabaseReference pushRef = bookRef.push();
    String pushId = pushRef.getKey();
    mCurrentBook.setPushId(pushId);
    pushRef.setValue(mCurrentBook);

    Toast.makeText(getContext(), "Saved", Toast.LENGTH_SHORT).show();

 }
}

Build.gradle (app)

apply plugin: 'com.android.application'

android {
    ...
}

 dependencies {
 ...
compile 'com.android.support:appcompat-v7:26.+'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.google.firebase:firebase-database:11.0.2'
compile 'com.google.firebase:firebase-auth:11.0.2'
testCompile 'junit:junit:4.12'
}
apply plugin: 'com.google.gms.google-services'

Firebase rules and Data Tab

{
"rules": {
".read": "auth != null",
".write": "auth != null"
         }
}

Data tab in Firebase

When I press the button(on a certain BookObject displayed in ListView I get the toast message yet my Data Tab in Firebase doesn't update.(therefore doesn't add the book to the database). What am I doing wrong?

Note:(after the books have been displayed and before I am able to press the button)I get following notification in Android Studio:

V/FA: Inactivity, disconnecting from the service

Gist with BookObject.java: Gist with BookObject.java

Thank you for any interest in my question and for any help as well.

Upvotes: 0

Views: 928

Answers (1)

UmarZaii
UmarZaii

Reputation: 1351

Declare a global variable in the CustomAdapterClass

private String mTitle;
private String mSubTitle;
private String mAuthors;

Retrieve all the data and store them in the string

BookObject currentBook = getItem(position);
mTitle = currentBook.getTitle();
mSubTitle = currentBook.getSubTitle();
mAuthors = currentBook.getAuthors();

And use code below in OnClickmethod

DatabaseReference pushRef = bookRef.push();
String pushId = pushRef.getKey();
mCurrentBook.setPushId(pushId);
pushRef.child("Title").setValue(mTitle);
pushRef.child("SubTitle").setValue(mSubTitle);
pushRef.child("Authors").setValue(mAuthors);

Upvotes: 1

Related Questions