数据已成功添加到Firebase(通过firebase控制台确认)。但是当我尝试提取数据时,我得到的是NULL值。
package com.example.shaur.nimblenavigationdrawer;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class all_books extends Fragment {
FirebaseDatabase db = FirebaseDatabase.getInstance();
DatabaseReference rootRef = db.getReference();
DatabaseReference bookRef = rootRef.child("BookAds");
RecyclerView recyclerView;
List<Books_getter_function> bookList;
String b_name,b_price,b_desc,negotiate,contact,email;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_all_books, container, false);
bookList = new ArrayList<>();
final BookAdapter adapter = new BookAdapter(getActivity(),bookList);
recyclerView = view.findViewById(R.id.recyclerView_books);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(adapter);
//This hardcoded data is being displayed correctly.
//bookList.add(new Books_getter_function("Ghatak 2008","200","test@gmail.com","Excellent book must buy","1233214390",R.drawable.book_blue_icon));
bookRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String value = String.valueOf(dataSnapshot.getValue());
//LOG IS WORKING FINE
Log.i("Book",value);
try {
JSONObject object = new JSONObject(value);
b_name = object.getString("BookName");
b_price = object.getString("BookPrice");
contact = object.getString("ContactNumber");
b_desc = object.getString("Description");
negotiate = object.getString("Negotiable");
email = object.getString("Email");
} catch (JSONException e) {
e.printStackTrace();
}
bookList.add(new Books_getter_function(b_name,b_price,email,b_desc,contact,R.drawable.book_blue_icon));
adapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return view;
}
}
LOG值正确:{UserId = IVXm0BS3NWZnsp7e6qz0W3bsYz2,BookPrice = 122,Description = excellent,ContactNumber = 9899999999,Negotiable = true,BookName = HK das,Email = test@gmail.com}
JSONObject对String b_name的值为PASS,b_price为null。为什么会这样? 我已经记录了值字符串,它给了我正确的输出,现在我只需要提取数据。
我已经验证了其他所有正确添加的硬编码数据,以检查是否显示并且它完全正常工作。
JSONObject从FireBase提取的Data是这里的问题。
答案 0 :(得分:1)
在firebase中,数据库由JSON组成,但您不需要使用JSONObject
来获取数据。您可以简单地执行此操作,而不是: -
b_name = object.getString("BookName");
b_price = object.getString("BookPrice");
contact = object.getString("ContactNumber");
b_desc = object.getString("Description");
negotiate = object.getString("Negotiable");
email = object.getString("Email");
这样做:
String b_name=dataSnapshot.child("BookName").getValue().toString();
String b_price=dataSnapshot.child("BookPrice").getValue().toString();
String contact=dataSnapshot.child("ContactNumber").getValue().toString();
String b_desc=dataSnapshot.child("Description").getValue().toString();
String negotiate=dataSnapshot.child("Negotiable").getValue().toString();
String email=dataSnapshot.child("Email").getValue().toString();
child()
是数据库中的属性