Reputation: 81
I want to display a registration history in parentsHomeActivity, where all this data will be taken in the firebase database. I have created adapter coding and also recent_registration_list_layout.xml to display 2 attributes only:-
Student Name (fullName)
Tuition Name that has been registered (tuitionName).
There is no red error, crash or any logcat. The problem I encountered was, my adapter was not displayed in my apps. Really hope someone can teach me, new beginner in Android Studio. Thanks in advance.
Database structure shows below, I only want to display the red circle attribute only:-
Adapter coding shows below:-
public class RegistrationHistoryAdapter extends RecyclerView.Adapter<RegistrationHistoryAdapter.ChildrenViewHolder>
{
private Context mCtx;
private List<StudentRegistration> childrenList;
public RegistrationHistoryAdapter(Context mCtx, List<StudentRegistration> childrenList)
{
this.mCtx = mCtx;
this.childrenList = childrenList;
}
@NonNull
@Override
public ChildrenViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
LayoutInflater inflater = LayoutInflater.from(mCtx);
View view = inflater.inflate(R.layout.recent_registration_list_layout, parent, false);
return new ChildrenViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ChildrenViewHolder holder, final int position)
{
final StudentRegistration studentRegistration = childrenList.get(holder.getAdapterPosition());
holder.tuitioNameView.setText(studentRegistration.getTuitioname());
holder.studNameView.setText(studentRegistration.getFullname());
}
@Override
public int getItemCount() {
return childrenList.size();
}
class ChildrenViewHolder extends RecyclerView.ViewHolder
{
TextView tuitioNameView, studNameView;
CardView childrenLayout;
public ChildrenViewHolder(View itemView)
{
super(itemView);
tuitioNameView = itemView.findViewById(R.id.tvTuitionName);
studNameView = itemView.findViewById(R.id.tvFullName);
childrenLayout = itemView.findViewById(R.id.cvChildren);
}
}
}
parentsHomeActivity coding shows below:-
databaseReference.child("Student Registration").child(mAuth.getUid()).addValueEventListener(new ValueEventListener()
{
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
{
//Iterating through all the values in database
mChildrenList = new ArrayList<>();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren())
{
StudentRegistration studentRegistration = postSnapshot.getValue(StudentRegistration.class);
mChildrenList.add(0, studentRegistration);
}
//Creating adapter
mAdapters = new RegistrationHistoryAdapter(getApplicationContext(), mChildrenList);
//Adding adapter to recyclerview
mRecyclerView.setAdapter(mAdapters);
mAdapters.notifyItemInserted(0);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
{
}
});
//other method
mRecyclerView.setHasFixedSize(true); //set fixed size for element in recycler view
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
Recycleview layout in xml shows below:-
<android.support.v7.widget.RecyclerView
android:id="@+id/rvChildren"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:background="@drawable/roundedbutton8"/>
Upvotes: 0
Views: 228
Reputation: 2178
You should store data this way
String current_user = FirebaseAuth.getInstance().getCurrentUser().getUid();
mDatabase.child("Student Registration").child(current_user).child(uid).setValue(studentRegistration).addOnCompleteListener(new OnCompleteListener<Void>()
{
@Override
public void onComplete(@NonNull Task<Void> task)
{
if(task.isSuccessful())
{
Toast.makeText(RegisterActivity.this, "Successfully registered!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(RegisterActivity.this, FinishActivity.class));
finish();
}
else
{
Toast.makeText(RegisterActivity.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
loadingBar.dismiss();
}
});
Then for fetching data. use this code
String current_user = FirebaseAuth.getInstance().getCurrentUser().getUid();
databaseReference.child("Student Registration").child(current_user).addValueEventListener(new ValueEventListener()
{
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
{
//Iterating through all the values in database
mChildrenList = new ArrayList<>();
for (DataSnapshot postSnapshot : dataSnapshot.getChildren())
{
StudentRegistration studentRegistration = postSnapshot.getValue(StudentRegistration.class);
mChildrenList.add(studentRegistration);
}
//Creating adapter
mAdapters = new RegistrationHistoryAdapter(getApplicationContext(), mChildrenList);
//Adding adapter to recyclerview
mRecyclerView.setAdapter(mAdapters);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
{
}
});
In your Adapter change onBindViewHolder
with this
@Override
public void onBindViewHolder(@NonNull final ChildrenViewHolder holder, final int position)
{
holder.tuitioNameView.setText(childrenList.get(holder.getAdapterPosition()).getTuitioname());
holder.studNameView.setText(childrenList.get(holder.getAdapterPosition()).getFullname());
}
Upvotes: 1
Reputation: 110
Retrive firebase data with this method:
ref.child("Student Registeration").addListenerForSingleValueEvent(new
ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.getValue != null) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
//get your object data from ds and add it to array list
}
//set adapter now
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
// ...
}
});
Upvotes: 0
Reputation: 123
Change From
for (DataSnapshot postSnapshot : dataSnapshot.getChildren())
{
StudentRegistration studentRegistration = postSnapshot.getValue(StudentRegistration.class);
mChildrenList.add(0, studentRegistration);
}
To
for (DataSnapshot postSnapshot : dataSnapshot.getChildren())
{
StudentRegistration studentRegistration = postSnapshot.getValue(StudentRegistration.class);
mChildrenList.add(studentRegistration);
}
And From
mAdapters.notifyItemInserted(0);
To
mAdapters.notifyDataSetChanged();
Upvotes: 0