我的recyclerview没有显示来自firebase的任何数据。我没有看到任何错误,所以我不知道为什么。我从stackoverflow成员处遵循FirebaseUI指南。但仍然没有奏效。我用的是android 3.0.1。对不起,如果我的代码那么糟糕我这个案子有点疯狂
HomeActivity.java
public class HomeActivity extends AppCompatActivity {
boolean isOpen = false;
private FirebaseAuth auth;
private FirebaseAuth.AuthStateListener authListener;
private ProgressBar progressBar;
private FloatingActionButton fab_plus, fab_logout, fab_create, fab_editacc;
private Animation fabOpen, fabClose, fabRClockwise, fabRantiClockwise;
private RecyclerView recyclerviewku;
private DatabaseReference myDatabase;
private FirebaseUser CUser;
private Query query;
private List<Journal> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fab_plus = findViewById(R.id.fab_plus);
fab_logout = findViewById(R.id.fab_logout);
fab_create = findViewById(R.id.fab_create);
fab_editacc = findViewById(R.id.fab_editacc);
fabOpen = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fab_open);
fabClose = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fab_close);
fabRClockwise = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate_clockwise);
fabRantiClockwise = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate_anticlockwise);
auth = FirebaseAuth.getInstance();
recyclerviewku = (RecyclerView) findViewById(R.id.journal_list);
recyclerviewku.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
recyclerviewku.setLayoutManager(linearLayoutManager);
myDatabase = FirebaseDatabase.getInstance().getReference().child("journal");
//get current user
authListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = firebaseAuth.getCurrentUser();
if (user == null) {
// user auth state is changed - user is null
// launch login activity
Intent intent = new Intent(HomeActivity.this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
}
};
fab_plus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isOpen) {
fab_create.startAnimation(fabClose);
fab_logout.startAnimation(fabClose);
fab_editacc.startAnimation(fabClose);
fab_plus.startAnimation(fabRantiClockwise);
fab_create.setClickable(false);
fab_logout.setClickable(false);
isOpen = false;
} else {
fab_create.startAnimation(fabOpen);
fab_logout.startAnimation(fabOpen);
fab_editacc.startAnimation(fabOpen);
fab_plus.startAnimation(fabRClockwise);
fab_create.setClickable(true);
fab_logout.setClickable(true);
isOpen = true;
}
}
});
fab_logout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
signOut();
}
});
fab_create.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(HomeActivity.this, AddActivity.class);
startActivity(intent);
}
});
}
public class JournalViewHolder extends RecyclerView.ViewHolder {
View mView;
public JournalViewHolder(View itemView) {
super(itemView);
itemView = mView;
}
public void setActivity(String activity) {
TextView title = (TextView) mView.findViewById(R.id.journal_title);
title.setText(activity);
}
public void setStart(String start) {
TextView mulai = (TextView) mView.findViewById(R.id.journal_timestart);
mulai.setText(start);
}
public void setEnd(String end) {
TextView akhir = (TextView) mView.findViewById(R.id.journal_timeend);
akhir.setText(end);
}
}
public void signOut() {
auth.signOut();
}
@Override
protected void onStart() {
super.onStart();
auth.addAuthStateListener(authListener);
query = FirebaseDatabase.getInstance()
.getReference()
.child("journal");
FirebaseRecyclerOptions<Journal> options =
new FirebaseRecyclerOptions.Builder<Journal>()
.setQuery(query, Journal.class)
.build();
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Journal, JournalViewHolder>(options) {
@Override
public JournalViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.journal_row, parent, false);
return new JournalViewHolder(view);
}
@Override
protected void onBindViewHolder(JournalViewHolder holder, int position, Journal model) {
holder.setActivity(model.getActivity());
holder.setStart(model.getStart());
holder.setEnd(model.getEnd());
}
};
recyclerviewku.setAdapter(adapter);
}
public void onStop() {
super.onStop();
if (authListener != null) {
auth.removeAuthStateListener(authListener);
}
}
}
Journal.java
public class Journal {
private String codejob;
private String category;
private String codecategory;
private String activity;
private String description;
private String sapsomp;
private String month;
private String start;
private String end;
private String hours;
private String venue;
private String vendor;
private String unittype;
private String remark;
public Journal(){}
public Journal(String codejob, String codecategory, String category, String activity, String description, String sapsomp, String month, String start, String end, String hours, String venue, String vendor, String unittype, String remark){
this.codejob = codejob;
this.category = category;
this.codecategory = codecategory;
this.activity = activity;
this.description = description;
this.sapsomp = sapsomp;
this.month = month;
this.start = start;
this.end = end;
this.hours = hours;
this.venue = venue;
this.vendor = vendor;
this.unittype = unittype;
this.remark = remark;
}
public String getCodejob(){
return codejob;
}
public void setCodejob(String codejob){
this.codejob = this.codejob;
}
public String getCategory(){
return category;
}
public void setCategory(String category){
this.category = this.category;
}
public String getCodecategory(){
return codecategory;
}
public void setCodecategory(String codecategory){
this.codecategory = codecategory;
}
public String getActivity(){
return activity;
}
public void setActivity(String activity){
this.activity = activity;
}
public String getDescription(){
return description;
}
public void setDescription(String description){
this.description = description;
}
public String getSapsomp(){
return sapsomp;
}
public void setSapsomp(String sapsomp){
this.sapsomp = sapsomp;
}
public String getMonth(){
return month;
}
public void setMonth(String month){
this.month = month;
}
public String getStart(){
return start;
}
public void setStart(String start){
this.start = start;
}
public String getEnd(){
return end;
}
public void setEnd(String end){
this.end = end;
}
public String getHours(){
return hours;
}
public void setHours(String hours){
this.hours = hours;
}
public String getVenue(){
return venue;
}
public void setVenue(String venue){
this.venue = venue;
}
public String getVendor(){
return vendor;
}
public void setVendor(String vendor){
this.vendor = vendor;
}
public String getUnittype(){
return unittype;
}
public void setUnittype(String unittype){
this.unittype = unittype;
}
public String getRemark(){
return remark;
}
public void setRemark(String remark){
this.remark = remark;
}
}
activity_main.xml中
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="id.sch.smktelkom_mlg.pw.utjournal.HomeActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="300dp"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleTextAppearance="@android:color/transparent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="300dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="245dp"
android:background="@drawable/bg_header"
android:orientation="vertical">
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/pprofile"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_gravity="center"
android:layout_marginTop="16dp"
android:src="@mipmap/ic_launcher"
app:civ_border_color="#fff"
app:civ_border_width="2dp"
app:srcCompat="@mipmap/ic_launcher_round" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Aska Ivan"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="4dp"
android:text="80111315"
android:textColor="#fff" />
</LinearLayout>
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="300dp"
android:layout_height="80dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="200dp"
card_view:cardBackgroundColor="#fff">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="2">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginLeft="12dp"
android:text="Branch"
android:textColor="#4a4a4a" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:text="Area"
android:textColor="#4a4a4a" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="6dp"
android:layout_marginLeft="12dp"
android:text="BLP"
android:textColor="#4a4a4a"
android:textSize="18dp"
android:textStyle="bold" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:text="TTA BALIKPAPAN"
android:textColor="#4a4a4a"
android:textSize="18dp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="16dp"
android:clipToPadding="false"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="@+id/journal_list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_create"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginBottom="180dp"
android:layout_marginRight="@dimen/fab_margin"
android:elevation="6dp"
android:src="@drawable/ic_create_black_24dp"
android:tint="#fff"
android:visibility="invisible"
app:backgroundTint="@color/fab1"
app:pressedTranslationZ="12dp" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_editacc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginBottom="100dp"
android:layout_marginRight="@dimen/fab_margin"
android:elevation="6dp"
android:src="@drawable/ic_person_black_24dp"
android:tint="#fff"
android:visibility="invisible"
app:backgroundTint="@color/fab3"
app:pressedTranslationZ="12dp" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_logout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginBottom="@dimen/fab_margin"
android:layout_marginRight="100dp"
android:elevation="6dp"
android:src="@drawable/ic_exit_to_app_black_24dp"
android:tint="#fff"
android:visibility="invisible"
app:backgroundTint="@color/fab2"
app:pressedTranslationZ="12dp" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab_plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginBottom="16dp"
android:layout_marginRight="@dimen/fab_margin"
android:elevation="6dp"
android:src="@drawable/ic_add_black_24dp"
android:tint="#fff"
app:pressedTranslationZ="12dp" />
</android.support.design.widget.CoordinatorLayout>
journal_row.xml
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"enter code here
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:background="@drawable/layout_border"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/journal_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Judul Journal"
android:textColor="#4a4a4a"
android:textSize="18sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/journal_timestart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="1/12/2018"
android:textSize="14sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text=" to "
android:textSize="14sp" />
<TextView
android:id="@+id/journal_timeend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="1/13/2018"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>