recyclerview llenar con java android firebase-database android-recyclerview android-cardview

java - llenar - La vista de reciclador no llena ningún dato en mi pantalla



llenar un recyclerview con firebase (0)

Mi actividad principal muestra un RecyclerView vacío. Mi CardView no se muestra.

Los datos se almacenan en una base de datos de Firebase pero no se muestran por alguna razón.

También busqué en la web pero nada me ayudó. Probé todo lo que aparece en la web, así como en Stack Overflow.

MainActivity.java

public class MainActivity1 extends AppCompatActivity { private RecyclerView mQuestionList; private CardView mCardVieww; private DatabaseReference mDatabase; private FirebaseAuth mAuth; private FirebaseAuth.AuthStateListener mAuthListener; private DatabaseReference mDatabaseUsers; private LinearLayoutManager layoutManager; private DatabaseReference mDatabaseCurrentUsers; private DatabaseReference mDatabaseLike; private boolean mProcessLike = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main1); mAuth=FirebaseAuth.getInstance(); mAuthListener=new FirebaseAuth.AuthStateListener() { @Override public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) { if (firebaseAuth.getCurrentUser()== null){ Intent loginIntent=new Intent(MainActivity1.this,LoginActivity.class); loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(loginIntent); } } }; mDatabase= FirebaseDatabase.getInstance().getReference().child("Question"); mDatabaseUsers=FirebaseDatabase.getInstance().getReference().child("Users"); mDatabaseLike=FirebaseDatabase.getInstance().getReference().child("Likes"); mDatabaseUsers.keepSynced(true); mDatabaseLike.keepSynced(true); mDatabase.keepSynced(true); mQuestionList=(RecyclerView) findViewById(R.id.question_list); layoutManager=new LinearLayoutManager(this); layoutManager.setReverseLayout(true); layoutManager.setStackFromEnd(true); mQuestionList.setHasFixedSize(true); mQuestionList.setLayoutManager(layoutManager); checkUserExist(); } @Override protected void onStart() { super.onStart(); mAuth.addAuthStateListener(mAuthListener); FirebaseRecyclerAdapter <Question,QuestionViewHolder> fireRecyclerAdapter = new FirebaseRecyclerAdapter <Question,QuestionViewHolder>( Question.class, R.layout.question, QuestionViewHolder.class, mDatabase ){ @Override protected void populateViewHolder(QuestionViewHolder viewHolder,Question model, int position){ final String post_key = getRef(position).getKey() viewHolder.setTitle(model.getTitle()); viewHolder.setDesc(model.getDesc()); viewHolder.setImage(getApplicationContext(),model.getImage()); viewHolder.setUsername(model.getUsername()); viewHolder.setLikeBtn(post_key); viewHolder.mView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Toast.makeText(MainActivity1.this,post_key,Toast.LENGTH_LONG).show(); Intent singleQuestionIntent=new Intent(MainActivity1.this,QuestionSingleActivity.class); singleQuestionIntent.putExtra("question_id",post_key); startActivity(singleQuestionIntent); } }); viewHolder.mLikeBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mProcessLike = true; mDatabaseLike.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { if (mProcessLike) { if (dataSnapshot.child(post_key).hasChild(mAuth.getCurrentUser().getUid())) { mDatabaseLike.child(post_key).child(mAuth.getCurrentUser().getUid()).removeValue(); mProcessLike = false; } else { mDatabaseLike.child(post_key).child(mAuth.getCurrentUser().getUid()).setValue("RandomValue"); mProcessLike = false; } } } @Override public void onCancelled(DatabaseError databaseError) { } }); } }); } }; mQuestionList.setHasFixedSize(true); mQuestionList.setLayoutManager(layoutManager); mQuestionList.setAdapter(fireRecyclerAdapter); } //cgeck user exist or not private void checkUserExist() { if (mAuth.getCurrentUser() != null) { final String user_id = mAuth.getCurrentUser().getUid(); mDatabaseUsers.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { if (!dataSnapshot.hasChild(user_id)) { Intent setupIntent = new Intent(MainActivity1.this, SetupActivity.class); setupIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(setupIntent); } } @Override public void onCancelled(DatabaseError databaseError) { } }); } } public static class QuestionViewHolder extends RecyclerView.ViewHolder{ View mView; ImageButton mLikeBtn; DatabaseReference mDatabaseLike; FirebaseAuth mAuth; public QuestionViewHolder(View itemView) { super(itemView); mView=itemView; mLikeBtn=(ImageButton) mView.findViewById(R.id.likeBtn); mDatabaseLike= FirebaseDatabase.getInstance().getReference().child("Likes"); mAuth=FirebaseAuth.getInstance(); mDatabaseLike.keepSynced(true); } public void setLikeBtn(final String post_key){ mDatabaseLike.addValueEventListener(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { if (dataSnapshot.child(post_key).hasChild(mAuth.getCurrentUser().getUid())){ mLikeBtn.setImageResource(R.mipmap.thumb_grey); }else{ mLikeBtn.setImageResource(R.mipmap.thumb_grey); } } @Override public void onCancelled(DatabaseError databaseError) { } }); } public void setTitle(String title){ TextView post_title=(TextView) mView.findViewById(R.id.post_title); post_title.setText(title); } public void setDesc(String desc){ TextView post_desc=(TextView) mView.findViewById(R.id.post_desc); post_desc.setText(desc); } public void setUsername(String username){ TextView post_username=(TextView) mView.findViewById(R.id.post_username); post_username.setText(username); } public void setImage(final Context ctx, final String image){ final ImageView post_image=(ImageView) mView.findViewById(R.id.post_image); //Picasso.with(ctx).load(image).into(post_image); Picasso.with(ctx).load(image).networkPolicy(NetworkPolicy.OFFLINE).into(post_image, new Callback() { @Override public void onSuccess() { } @Override public void onError() { Picasso.with(ctx).load(image).into(post_image); } }); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main_menu,menu); return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { if(item.getItemId()==R.id.action_add){ startActivity(new Intent(MainActivity1.this,PostActivity.class)); } if(item.getItemId()==R.id.action_logout){ logout(); } if(item.getItemId()==R.id.action_Qp){ startActivity(new Intent(MainActivity1.this,MainActivity2.class)); } return super.onOptionsItemSelected(item); } private void logout() { mAuth.signOut(); } }

Question.java

public class Question { private CardView mCardView; private String title; private String desc; private String image; private String username; public Question(){ } public Question(String title, String desc, String image,String username) { this.title = title; this.desc = desc; this.image = image; this.username = username; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } public String getImage() { return image; } public void setImage(String image) { this.image = image; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }

activity_main1.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.akhil.rtmnuforum.MainActivity1"> <android.support.v7.widget.RecyclerView android:id="@+id/question_list" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/background"> </android.support.v7.widget.RecyclerView> </LinearLayout>

question.xml

<?xml version="1.0" encoding="utf-8"?> <android.support.v7.widget.CardView 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:id="@+id/cardview" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="20dp" android:visibility="visible" tools:context="com.example.akhil.rtmnuforum.MainActivity1"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:id="@+id/post_image" android:layout_width="match_parent" android:layout_height="wrap_content" android:adjustViewBounds="true" android:scaleType="centerCrop" android:src="@mipmap/add_btn" /> <TextView android:id="@+id/post_title" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="15dp" android:text="Post Title Goes here" android:textSize="16dp" android:textStyle="bold" /> <TextView android:id="@+id/post_desc" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="15dp" android:paddingLeft="15dp" android:paddingRight="15dp" android:text="Question Description Goes here" /> <TextView android:id="@+id/post_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingBottom="15dp" android:paddingLeft="15dp" android:paddingRight="15dp" android:text="username" android:textSize="12dp" android:textStyle="bold" /> <ImageButton android:id="@+id/likeBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="10dp" android:background="@color/cardview_light_background" app:srcCompat="@mipmap/ic_thumb_up_black_24dp" /> </LinearLayout> </android.support.v7.widget.CardView>