我正嘗試構建一個顯示有關圖書信息的Android應用程序(使用Google books API)。我最近嚮應用程序添加了firebase。Android應用程序無法將數據寫入Firebase
此時我可以登錄或註冊應用程序(新用戶出現在FB控制檯中),我可以搜索書籍(具有搜索視圖),並且我爲ListView中列出的每本書都添加了一個按鈕從查詢中創建。接下來,我在按鈕中添加了以下功能:按鈕(如果按下)應該將當前書籍(在Listview中)添加到當前用戶的「庫」中的FireBase中。 我已經在我的自定義AdapterClass寫了這個代碼,所以我可以添加當前圖書(從ListView控件)是這樣的:
CustomAdapter類:
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'
火力地堡規則和數據標籤
{
"rules": {
".read": "auth != null",
".write": "auth != null"
}
}
當我按下按鈕(在ListView中顯示我得到的消息敬酒在火力地堡但我的數據標籤不更新一定BookObject。(因此不書添加到數據庫)。 我在做什麼錯?
注:(僅次於書籍已經顯示和之前我可以按下按鈕),我獲得以下Android Studio中通知:
V/FA: Inactivity, disconnecting from the service
要點與BookObject.java: Gist with BookObject.java
感謝您對我的問題以及任何幫助的關注。
當你將數據添加到數據庫中,你檢查了是否它是否被添加? – UmarZaii
我唯一的檢查方式是檢查Firebase中的數據標籤(每次看起來像照片中的樣子)。我該如何做更好的檢查? –
這不是我的意思。我的意思是你在數據庫中有任何數據嗎?如果你不這樣做。你什麼都做不了。 – UmarZaii