Reputation: 140
i tried to retrieving data from Firebase to recycler-view on my Android project.
first, here's my structure database on firebase
then, i write Ph class
public class Ph {
public String date;
public String nilai;
public String time;
public Ph(){
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getNilai() {
return nilai;
}
public void setNilai(String nilai) {
this.nilai = nilai;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
to save data, i create PhAdapter class.
public class PhAdapter extends RecyclerView.Adapter<PhAdapter.PhViewHolder> {
private ArrayList<Ph> dataList;
public int rowLayout;
private Context context;
public static class PhViewHolder extends RecyclerView.ViewHolder {
private TextView tvNilai, tvDate, tvTime;
private LinearLayout phLayout;
public PhViewHolder(View itemview) {
super(itemview);
phLayout = (LinearLayout) itemview.findViewById(R.id.ph_view);
tvNilai = (TextView) itemview.findViewById(R.id.tv_ph);
tvTime = (TextView) itemview.findViewById(R.id.tv_time);
tvDate = (TextView) itemview.findViewById(R.id.tv_date);
}
}
public PhAdapter(ArrayList<Ph> dataList, Context context) {
this.dataList = dataList;
this.context = context;
}
@Override
public PhAdapter.PhViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(rowLayout, parent, false);
return new PhViewHolder(view);
}
@Override
public void onBindViewHolder(final PhViewHolder holder, final int position) {
holder.tvNilai.setText(dataList.get(position).getNilai());
holder.tvTime.setText(dataList.get(position).getTime());
holder.tvDate.setText(dataList.get(position).getDate());
}
@Override
public int getItemCount() {
return dataList.size();
}
}
the last one, my MainActivity class
public class MainActivity extends AppCompatActivity {
private static final String TAG = MainActivity.class.getSimpleName();
TextView txtNilai, txtDate, txtTime;
Button cekdata;
private DatabaseReference mDatabase;
//get reference database
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view1);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
cekdata = (Button)findViewById(R.id.btn_cekdata);
txtNilai = (TextView)findViewById(R.id.tv_ph);
txtDate = (TextView)findViewById(R.id.tv_date);
txtTime = (TextView)findViewById(R.id.tv_time);
//firebase
mDatabase = FirebaseDatabase.getInstance().getReference("https://raspi-ph.firebaseio.com/Fakultas Teknik/ph");
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
cekdata.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ValueEventListener postListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Ph ph = dataSnapshot.getValue(Ph.class);
}
@Override
public void onCancelled(DatabaseError databaseError) {
//Getting Ph failed, log a message
Log.w(TAG, "loadPost:onCancelled", databaseError.toException());
}
};
mDatabase.addValueEventListener(postListener);
}
});
}
}
but, when i run the project to emulator, i got error java.lang.NoSuchErrorMethod. the error cause by line 42 on MainActivity, is a line to get reference database.
please help me. there's something error in my code?
the error message:
07-09 08:45:54.124 6296-6296/com.example.titiarimba.raspi_ph E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.titiarimba.raspi_ph, PID: 6296 java.lang.NoSuchMethodError: No virtual method zzbqo()Z in class Lcom/google/firebase/FirebaseApp; or its super classes (declaration of 'com.google.firebase.FirebaseApp' appears in /data/app/com.example.titiarimba.raspi_ph-2/split_lib_dependencies_apk.apk) at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source) at com.google.firebase.database.FirebaseDatabase.getInstance(Unknown Source) at com.example.titiarimba.raspi_ph.MainActivity.onCreate(MainActivity.java:43) at android.app.Activity.performCreate(Activity.java:5990) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) at android.app.ActivityThread.access$800(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Upvotes: 0
Views: 2049
Reputation: 1833
I came across a similar error (not the same) with Firebase
after implementing FirebaseAuth.
After trying a lot, I removed the FirebaseCore
dependency and it worked.
Upvotes: 0
Reputation: 11
check your dependencies, make sure your Fire base core tally's with your fire base database.
For eExample
implementation 'com.google.firebase:firebase-core:16.0.1' implementation 'com.google.firebase:firebase-database:16.0.1'
not
implementation 'com.google.firebase:firebase-core:16.0.1' implementation 'com.google.firebase:firebase-database:10.0.1'
Upvotes: 0
Reputation: 140
this question is solve. i downloading google-service.json from firebase console and add it to project/app folder :)
Upvotes: 0
Reputation: 13129
Try to remove your valueEventListener and do this instead
first Remove this listener
ValueEventListener postListener = new ValueEventListener() {...
change this
mDatabase = FirebaseDatabase.getInstance().getReference("https://raspi-ph.firebaseio.com/Fakultas Teknik/ph");
to this
mDatabase = FirebaseDatabase.getInstance().getReference();
and then inside your onClick use that reference to access your data. With this for inside your onDataChange you can access all your values from the different keys
mDatabase.child("Fakultas Teknik").child("ph").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot childSnapshot : dataSnapshot.getChildren()){
Ph ph = childSnapshot.getValue(Ph.class);
String date = ph.getDate();
Log.i("Date:",""+date);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getApplicationContext(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
Or you can try to add your reference with getReferenceFromUrl()
mDatabase = FirebaseDatabase.getInstance().getReferenceFromUrl("https://raspi-ph.firebaseio.com/Fakultas Teknik/ph");
Upvotes: 1