Data not showing up in recyclerview from firebase using firebase ui - android

I am using firebase ui to load data from firebase database to recycler view but the data is not showing up inside the recycler view.
This is my fragment
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.FirebaseDatabase;
import com.infinity.houseestimatorandmodeler.R;
import com.infinity.houseestimatorandmodeler.databinding.FragmentBuildersBinding;
public class BuilderFragment extends Fragment {
private BuilderAdapter adapter;
public BuilderFragment() {
// Required empty public constructor
}
#Override
public void onStart() {
super.onStart();
adapter.startListening();
}
#Override
public void onStop() {
super.onStop();
adapter.stopListening();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
FragmentBuildersBinding databinding= DataBindingUtil.inflate(inflater, R.layout.fragment_builders, container, false);
FirebaseRecyclerOptions<buildersModel> options =
new FirebaseRecyclerOptions.Builder<buildersModel>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("builders"), buildersModel.class)
.build();
adapter = new BuilderAdapter(options);
databinding.buildersRecyclerView.setAdapter(adapter);
return databinding.getRoot();
}
}`
this is my adapter class that is adapting the data for showing in the recycler view
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.infinity.houseestimatorandmodeler.R;
public class BuilderAdapter extends FirebaseRecyclerAdapter<buildersModel, BuilderAdapter.BuilderViewHolder> {
public BuilderAdapter(#NonNull FirebaseRecyclerOptions<buildersModel> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull BuilderViewHolder holder, int i, #NonNull buildersModel builder) {
holder.name.setText(builder.getName());
holder.builderClass.setText(builder.getBuilderClass());
holder.address.setText(builder.getAddress());
}
#NonNull
#Override
public BuilderViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.builders_card, parent, false);
return new BuilderViewHolder(view);
}
class BuilderViewHolder extends RecyclerView.ViewHolder{
TextView name, builderClass, address;
public BuilderViewHolder(#NonNull View itemView) {
super(itemView);
name = itemView.findViewById(R.id.nameofbuilder);
builderClass = itemView.findViewById(R.id.classOfBuilder);
address = itemView.findViewById(R.id.address);
}
}
}
this is my model class for data coming from firebase `
class buildersModel
{
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getBuilderClass() {
return builderClass;
}
public void setBuilderClass(String builderClass) {
this.builderClass = builderClass;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public buildersModel(String address, String builderClass, String name) {
this.address = address;
this.builderClass = builderClass;
this.name = name;
}
String address;
String builderClass;
String name;}
this is my firebase database
card view for individual element in recycler view
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#drawable/rounded_cards"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_margin="20dp"
android:src="#drawable/ic_builder" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/rounded_cards">
<TextView
android:id="#+id/nameofbuilder"
android:layout_marginTop="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/baloo"
android:textSize="20dp"
android:padding="2dp"
android:text="Abdul Shakoor"/>
<TextView
android:id="#+id/classOfBuilder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textSize="15sp"
android:textColor="#342C2C"
android:padding="2dp"
android:text="Location Islambad"/>
<TextView
android:id="#+id/address"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textColor="#342C2C"
android:padding="2dp"
android:text="Address : I10 Islamabad near Rahimia mosque"/>
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
recycler view layout
<layout 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"
>
<data>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="250dp"
android:src="#drawable/ic_builders"
android:background="#75CDDC"
/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/buildersRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
</LinearLayout>

You didn't set the RecyclerView LayoutManger, you can either set it in you layout
<androidx.recyclerview.widget.RecyclerView
....
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
Or in the fragment
databinding.buildersRecyclerView.setLayoutManager(new LinearLayoutManager(requireActivity()));

Related

Recycler View leaving huge space before printing next card view

I'm trying to show all the card views using recycler view in main activity. Although I did not set a huge margin between card views, my recycler view shows a huge space to print the next card view. please see my attached image, you'll understand what I'm trying to say. I've attached all the code here. Can you please advise how to solve this problem? Thank you.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
tools:listitem="#layout/note_card" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="50dp"
android:layout_marginRight="50dp"
android:layout_marginBottom="50dp"
android:clickable="true"
app:layout_constraintBottom_toBottomOf="#+id/recyclerView"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="#drawable/add" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.cardview.widget.CardView
android:id="#+id/cardView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="7dp"
app:cardBackgroundColor="#color/green"
app:cardCornerRadius="5dp"
app:cardElevation="5dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textViewTitleCard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="TextView"
android:textSize="20sp" />
<TextView
android:id="#+id/textViewDescriptionCard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_marginTop="16dp"
android:text="TextView"
android:textSize="16sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
package com.destructivepaul.quicknote;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.List;
public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.NoteHolder>{
private List<MyNote> myNoteList = new ArrayList<>();
// private Context context;
public void setMyNoteList(List<MyNote> myNoteList) {
this.myNoteList = myNoteList;
notifyDataSetChanged();
Log.i("info","note updated on adapter. note list size: "+myNoteList.size());
}
//public void setContext(Context context) {
// this.context = context;
// notifyDataSetChanged();
// Log.i("info","context updated on adapter");
// }
#NonNull
#Override
public NoteHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.note_card
,parent,false);
return new NoteHolder(view);
}
#Override
public void onBindViewHolder(#NonNull NoteHolder holder, int position) {
MyNote myNote=myNoteList.get(position);
holder.textViewTitle.setText(myNote.getNote_title());
holder.textViewDescription.setText(myNote.getNote_description());
}
#Override
public int getItemCount() {
return myNoteList.size();
}
public class NoteHolder extends RecyclerView.ViewHolder{
private TextView textViewTitle, textViewDescription;
private CardView cardView;
public NoteHolder(#NonNull View itemView) {
super(itemView);
textViewTitle=itemView.findViewById(R.id.textViewTitleCard);
textViewDescription=itemView.findViewById(R.id.textViewDescriptionCard);
cardView=itemView.findViewById(R.id.cardView);
Log.i("info","card design found on adapter");
}
}
}
package com.destructivepaul.quicknote;
import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private FloatingActionButton fab;
private RecyclerView recyclerView;
private MyNoteViewModel myNoteViewModel;
private ActivityResultLauncher<Intent> activityResultLauncherForAddNote;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//resisterActivity
resisterActivityForAddNote();
fab=findViewById(R.id.fab);
recyclerView=findViewById(R.id.recyclerView);
NoteAdapter adapter=new NoteAdapter();
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
Log.i("info","set the adapter");
myNoteViewModel = new ViewModelProvider.AndroidViewModelFactory(getApplication())
.create(MyNoteViewModel.class);
myNoteViewModel.getAllNotes().observe(MainActivity.this, new Observer<List<MyNote>>() {
#Override
public void onChanged(List<MyNote> myNotes) {
adapter.setMyNoteList(myNotes);
Log.i("info","adapter is called to update data");
}
});
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent =new Intent(MainActivity.this,CreateNoteActivity.class);
//resister activity launcher
activityResultLauncherForAddNote.launch(intent);
Log.i("info","called resister activity launcher on fab");
}
});
}
public void resisterActivityForAddNote(){
activityResultLauncherForAddNote=registerForActivityResult(new ActivityResultContracts.StartActivityForResult()
, new ActivityResultCallback<ActivityResult>() {
#Override
public void onActivityResult(ActivityResult result) {
int resultCode=result.getResultCode();
Intent data = result.getData();
if (resultCode==RESULT_OK && data !=null) {
String title = data.getStringExtra("title");
String description = data.getStringExtra("description");
String colorName = data.getStringExtra("color");
MyNote myNote = new MyNote(title, description, colorName);
Log.i("info", "new note created on activity result");
myNoteViewModel.insert(myNote);
Log.i("info", "note saved to database");
}
}
});
}
}
I've solved my problem by myself. The problem was on card layout design(parent layout height should be wrap content instead of match parent).

How to show the recyclerView data from firebase realtime database in fragments. Android studio

fragment_leaderboard.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
android:orientation="vertical"
tools:context=".LeaderboardsFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
row_leaderboard.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"?
android:layout_height="wrap_content"
android:background="#color/cream">
<TextView
android:id="#+id/index"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:fontFamily="#font/gotham_bold"
android:text="#1"
android:textColor="#color/brown"
android:textSize="15sp"
app:layout_constraintBottom_toBottomOf="#+id/imageView7"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/imageView7" />
<ImageView
android:id="#+id/imageView7"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#+id/index"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/menu_profile2" />
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:fontFamily="#font/gotham_bold"
android:text="Name of Winner"
android:textColor="#color/brown"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="#+id/imageView7"
app:layout_constraintStart_toEndOf="#+id/imageView7"
app:layout_constraintTop_toTopOf="#+id/imageView7" />
<TextView
android:id="#+id/coins"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:fontFamily="#font/gotham_bold"
android:text="1230"
android:textColor="#color/brown"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="#+id/name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/name" />
</android.constraintlayout.widget.ConstraintLayout>
LeaderBoardAdapter.java
package com.android.myquiz;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.android.myquiz.databinding.RowLeaderboardBinding;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
public class LeaderBoardAdapter extends
RecyclerView.Adapter<LeaderBoardAdapter.LeaderboardViewHolder> {
Context context;
ArrayList<User> users;
public LeaderBoardAdapter(Context context, ArrayList<User> users){
this.context = context;
this.users = users;
}
#NonNull
#NotNull
#Override
public LeaderboardViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType)
{
View view = LayoutInflater.from(context).inflate(R.layout.row_leaderboard, parent,
false);
return new LeaderboardViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull LeaderboardViewHolder holder, int position) {
User user = users.get(position);
holder.binding.name.setText(user.getName1());
holder.binding.coins.setText(String.valueOf(user.getCoins()));
holder.binding.index.setText(String.format("#%d", position+1));
}
#Override
public int getItemCount() {
return users.size();
}
public class LeaderboardViewHolder extends RecyclerView.ViewHolder {
TextView id;
RowLeaderboardBinding binding;
public LeaderboardViewHolder(#NonNull View itemView) {
super(itemView);
binding = RowLeaderboardBinding.bind(itemView);
}
}
}
LeaderboardFragments.java
package com.android.myquiz;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.android.myquiz.databinding.FragmentLeaderboardsBinding;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.QuerySnapshot;
import org.w3c.dom.Document;
import java.util.ArrayList;
public class LeaderboardsFragment extends Fragment {
public LeaderboardsFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
FragmentLeaderboardsBinding binding;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
// Inflate the layout for this fragment
binding = FragmentLeaderboardsBinding.inflate(inflater, container,false);
FirebaseFirestore database = FirebaseFirestore.getInstance();
final ArrayList<User> users = new ArrayList<>();
final LeaderBoardAdapter adapter = new LeaderBoardAdapter(getContext(), users);
binding.recyclerView.setAdapter(adapter);
binding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
database.collection("users")
.orderBy("coins", Query.Direction.DESCENDING).get().addOnSuccessListener(new
OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for (DocumentSnapshot snapshot : queryDocumentSnapshots){
User user = snapshot.toObject(User.class);
users.add(user);
}
adapter.notifyDataSetChanged();
}
});
return binding.getRoot();
}
}
this is my code I want to retrieve data from the firebase real-time database in recyclerView in fragments.
Okay, so looks like you are able to fetch the data from firebase, but are making a mistake in setting the data.
When you are calling adapter.notifyDataSetChanged(), you need to take care that you are passing the new users list, to the adapter.
You need to create a method inside your adapter class,
public void setUsersList(ArrayList<User> users) {
this.users = users;
notifyDatasetChanged();
}
and call it inside your fragment like, adapter.setUsersList(users), instead of calling adapter.notifyDataSetChanged().

Firestore RecyclerView not displaying the items in the app

I am using firestore recyclerview, but the items is not displayed on the app. I don't know what the problem is. I used is t before in other projects but still i don't get the mistake I made.
Display Fragment:
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
import butterknife.BindView;
import butterknife.ButterKnife;
public class HospitalFragment extends Fragment {
#BindView(R.id.hospitalList)
RecyclerView hospitalList;
CollectionReference medcenters;
FirebaseFirestore ref;
HospitalAdapter hospitalAdapter;
private String TAG="TAG";
public HospitalFragment() {
// Required empty public constructor
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment_hospital, container, false);
ButterKnife.bind(this,view);
return view;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.e(TAG, "onViewCreated: "+hospitalList);
rvSetUp();
dbSetup();
}
private void dbSetup() {
ref=FirebaseFirestore.getInstance();
medcenters=ref.collection("MedicalCenters");
Query query=medcenters.orderBy("name");
FirestoreRecyclerOptions<RVCell> options=new FirestoreRecyclerOptions.Builder<RVCell>()
.setQuery(query,RVCell.class)
.build();
hospitalAdapter=new HospitalAdapter(options,getContext());
hospitalList.setAdapter(hospitalAdapter);
}
private void rvSetUp() {
hospitalList.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager=new LinearLayoutManager(getContext());
hospitalList.setLayoutManager(layoutManager);
}
#Override
public void onStart() {
super.onStart();
hospitalAdapter.startListening();
}
#Override
public void onStop() {
super.onStop();
hospitalAdapter.stopListening();
}
}
Adapter. This is the adapter which extends recyclerview Adapter :
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
public class HospitalAdapter extends FirestoreRecyclerAdapter<RVCell,HospitalAdapter.ViewHolder> {
Context context;
public HospitalAdapter(#NonNull FirestoreRecyclerOptions<RVCell> options, Context context){
super(options);
this.context=context;
}
#Override
protected void onBindViewHolder(#NonNull ViewHolder holder, int position, #NonNull RVCell model)
{
holder.location.setText(model.getLocation());
holder.title.setText(model.getMCName());
holder.phone.setText(model.getPhno());
holder.time.setText(model.getTime());
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.rv_cell, parent, false);
return new ViewHolder(v);
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView title,location,time,phone;
public ViewHolder(#NonNull View itemView) {
super(itemView);
title=itemView.findViewById(R.id.title);
location=itemView.findViewById(R.id.location);
time=itemView.findViewById(R.id.time);
phone=itemView.findViewById(R.id.phno);
}
}
}
Cell. This is the POJO class :
public class RVCell {
String MCName,location,time,phno,picurl,locurl;
public String getMCName() {
return MCName;
}
public void setMCName(String MCName) {
this.MCName = MCName;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getPhno() {
return phno;
}
public void setPhno(String phno) {
this.phno = phno;
}
public String getPicurl() {
return picurl;
}
public void setPicurl(String picurl) {
this.picurl = picurl;
}
public String getLocurl() {
return locurl;
}
public void setLocurl(String locurl) {
this.locurl = locurl;
}
}
Main Layout. RecyclerView Layout :
<FrameLayout 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=".HospitalFragment">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/hospitalList"/>
</FrameLayout>
Cell layout. This is the layout used to diplay the items in the recycler view:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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="wrap_content"
android:layout_marginTop="8dp">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Location"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/container"
android:background="#color/card_bg_color" />
<RelativeLayout
android:id="#+id/container"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:background="#drawable/card_bg"
android:paddingBottom="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:id="#+id/location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/title"
android:layout_alignStart="#+id/title"
android:layout_alignLeft="#id/title"
android:layout_marginTop="8dp"
android:layout_marginRight="16dp"
android:lineSpacingExtra="8dp"
android:text="Location:"
android:textColor="#color/content_text_color" />
<TextView
android:id="#+id/time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/location"
android:layout_alignStart="#+id/location"
android:layout_alignLeft="#id/location"
android:layout_marginTop="8dp"
android:layout_marginRight="16dp"
android:lineSpacingExtra="8dp"
android:text="Time:"
android:textColor="#color/content_text_color" />
<TextView
android:id="#+id/phno"
android:layout_width="333dp"
android:layout_height="wrap_content"
android:layout_below="#id/location"
android:layout_alignStart="#+id/time"
android:layout_alignLeft="#id/time"
android:layout_marginStart="-2dp"
android:layout_marginLeft="-2dp"
android:layout_marginTop="34dp"
android:layout_marginRight="16dp"
android:lineSpacingExtra="8dp"
android:text="Phone:"
android:textColor="#color/content_text_color" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="40dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="20dp"
android:text="Simple Title Text "
android:textColor="#color/colorPrimary"
android:textStyle="bold" />
</RelativeLayout>
<ImageView
android:id="#+id/img_user"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
app:layout_constraintEnd_toStartOf="#+id/container"
app:layout_constraintStart_toStartOf="#+id/container"
app:layout_constraintTop_toTopOf="#+id/container"
app:srcCompat="#mipmap/ic_launcher" />
</androidx.constraintlayout.widget.ConstraintLayout>
This the code. I searched a lot I did not find any mistakes. I also used the listeners of the recycler adapter. If any body can help it would be nice.
Which information has your TAG?
It has to have Name of your Activity, try this:
private static final String TAG = „HospitalFragment“;

android - Retrieve images from firebase with two apps

I am new to app development. I have two apps linked in firebase, one for sending image to Firebase Storage and image url to Firebase Realtime Database and one for retrieving all images with RecyclerView. The first app is running fine. But the second doesn't do anything. It only shows the layout arrivals.
Code of arrivals.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="gr.packagename.name.MainActivity"
android:background="#drawable/background">
<ImageView
android:layout_width="wrap_content"
android:layout_height="100dp"
android:src="#drawable/logo"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/arrivals_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="144dp" />
</RelativeLayout>
Code of model.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<ImageView
android:layout_width="100dp"
android:layout_height="200dp"
android:id="#+id/clothesimage"
android:src="#drawable/men"/>
<TextView
android:layout_width="74dp"
android:layout_height="wrap_content"
android:id="#+id/categoryimage"
android:text="category"
android:layout_alignBottom="#+id/clothesimage"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="18sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
Code of arrivals.java
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.Picasso;
public class arrivals extends AppCompatActivity {
DatabaseReference mdatabase;
RecyclerView rv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.arrivals);
mdatabase = FirebaseDatabase.getInstance().getReference().child("Images");
rv = (RecyclerView) findViewById(R.id.arrivals_list);
rv.setHasFixedSize(true);
rv.setLayoutManager(new LinearLayoutManager(this));
}
#Override
protected void onStart(){
super.onStart();
FirebaseRecyclerAdapter<Images, ImageViewHolder> firebaseadapter = new FirebaseRecyclerAdapter<Images, ImageViewHolder>(
Images.class,
R.layout.list_content,
ImageViewHolder.class,
mdatabase
) {
#Override
protected void populateViewHolder(ImageViewHolder viewHolder, Images model, int position) {
viewHolder.setCategory(model.getCategory());
viewHolder.setImageurl(getApplicationContext(), model.getImageurl());
}
};
rv.setAdapter(firebaseadapter);
}
public static class ImageViewHolder extends RecyclerView.ViewHolder{
View mView;
public ImageViewHolder(View itemView){
super(itemView);
mView = itemView;
}
public void setCategory(String category){
TextView categoryimg = (TextView) mView.findViewById(R.id.categoryimage);
categoryimg.setText(category);
}
public void setImageurl(Context ctx, String imageurl){
ImageView img = (ImageView) mView.findViewById(R.id.clothesimage);
Picasso.with(ctx).load(imageurl).into(img);
}
}
}
Code of Images.java
public class Images {
private String imageurl;
private String category;
public Images() {
}
public String getImageurl() {
return imageurl;
}
public void setImageurl(String imageurl) {
this.imageurl = imageurl;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
}

RecyclerView inside RecyclerView scroll in same direction

I have 2 recycler views, let's call then recycler1 and recycler2 (purple background). The squares with yellow background are elements of recycler2 which has an instance inside of each element of recycler1. What I'm trying to achieve is that it should scroll elements of recycler2 on purple area fling and recycler1 outside it. I made it with horizontal scroll (which is very easy) but can't do it vertically. I tried to set and reset nested scrolling and behaviour and whatever but can't make it work.
Below the code (a lot of it):
InnerItem Layout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout android:layout_height="50dp"
android:layout_width="60dp"
android:background="#38f"
android:padding="4dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:background="#ffff00"
android:id="#+id/tag_text_view"
android:text="Some Content"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
Nested recycler item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#cfcfcf"
>
<TextView
android:id="#+id/first_name_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="10dp"
android:textSize="20sp"
android:text="New Text"
android:background="#1ad"
/>
<TextView
android:id="#+id/last_name_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Text"
android:gravity="center"
android:layout_margin="10dp"
android:textSize="16sp"
android:padding="10dp"
android:background="#d65"
/>
<android.support.v7.widget.RecyclerView
android:id="#+id/nested_recycler"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#659"
>
</android.support.v7.widget.RecyclerView>
</LinearLayout>
Activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
InnerItem
package com.yrs.androidltx.features.adapterviews.nestedrecycler;
public class InnerItem {
String tag;
public InnerItem(String tag) {
this.tag = tag;
}
}
InnerItem Adapter
package com.yrs.androidltx.features.adapterviews.nestedrecycler;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.yrs.androidltx.R;
import java.util.List;
public class InnerItemAdapter extends RecyclerView.Adapter<InnerItemViewHolder> {
List<InnerItem> innerItemList;
public InnerItemAdapter(List<InnerItem> innerItemList) {
this.innerItemList = innerItemList;
}
#Override
public InnerItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater
.from(parent.getContext())
.inflate(R.layout.inner_item, parent, false);
InnerItemViewHolder viewHolder = new InnerItemViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(InnerItemViewHolder holder, int position){
holder.tagTextVIew.setText(innerItemList.get(position).tag);
}
#Override
public int getItemCount() {
return innerItemList.size();
}
}
InnerItem ViewHolder
package com.yrs.androidltx.features.adapterviews.nestedrecycler;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import com.yrs.androidltx.R;
public class InnerItemViewHolder extends RecyclerView.ViewHolder {
TextView tagTextVIew;
public InnerItemViewHolder(View itemView) {
super(itemView);
tagTextVIew = (TextView) itemView.findViewById(R.id.tag_text_view);
}
}
Person
package com.yrs.androidltx.features.adapterviews.nestedrecycler;
public class Person {
private String firstName;
private String lastName;
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
Person ViewHolder
package com.yrs.androidltx.features.adapterviews.nestedrecycler;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import com.yrs.androidltx.R;
public class PersonViewHolder extends RecyclerView.ViewHolder {
//Keep package visibility
TextView firstNameTextView;
TextView lastNameTextView;
RecyclerView nestedRecyclerView;
public PersonViewHolder(View itemView) {
super(itemView);
firstNameTextView = (TextView) itemView.findViewById(R.id.first_name_textView);
lastNameTextView = (TextView) itemView.findViewById(R.id.last_name_textView);
nestedRecyclerView = (RecyclerView) itemView.findViewById(R.id.nested_recycler);
}
}
Person Adapter
package com.yrs.androidltx.features.adapterviews.nestedrecycler;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.yrs.androidltx.R;
import java.util.ArrayList;
import java.util.List;
public class PersonAdapter extends RecyclerView.Adapter<PersonViewHolder> {
List<Person> personList;
public PersonAdapter(List<Person> personList) {
this.personList = personList;
}
#Override
public PersonViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater
.from(parent.getContext())
.inflate(R.layout.nested_recycler_item, parent, false);
PersonViewHolder personViewHolder = new PersonViewHolder(view);
return personViewHolder;
}
#Override
public void onBindViewHolder(PersonViewHolder holder, int position) {
holder.firstNameTextView.setText(personList.get(position).getFirstName());
holder.lastNameTextView.setText(personList.get(position).getLastName());
holder.nestedRecyclerView.setLayoutManager(new GridLayoutManager(holder.nestedRecyclerView.getContext(), 3, GridLayoutManager.VERTICAL, false ));
holder.nestedRecyclerView.setAdapter(new InnerItemAdapter(initTagList()));
}
#Override
public int getItemCount() {
return personList.size();
}
private List<InnerItem> initTagList() {
List<InnerItem> tagList = new ArrayList<>();
tagList.add(new InnerItem("android"));
tagList.add(new InnerItem("iOS"));
tagList.add(new InnerItem("windows"));
tagList.add(new InnerItem("linux"));
tagList.add(new InnerItem("NetBSD"));
tagList.add(new InnerItem("NetWare"));
//repeat few times...
return tagList;
}
}
Main activity
package com.yrs.androidltx.features.adapterviews.nestedrecycler;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.yrs.androidltx.R;
import com.yrs.androidltx.activities.base.BaseActivity;
import java.util.ArrayList;
import java.util.List;
public class WithNestedRecyclerActivity extends BaseActivity {
private RecyclerView recyclerView;
private PersonAdapter personAdapter;
private RecyclerView.LayoutManager layoutManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_with_recycler);
recyclerView = (RecyclerView) findViewById(R.id.recycler);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
personAdapter = new PersonAdapter(initDataSet());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(personAdapter);
}
private List<Person> initDataSet() {
List<Person> personList = new ArrayList<>();
personList.add(new Person("Ned","Flanders"));
personList.add(new Person("Homer","Simpson"));
personList.add(new Person("Bart","Simpson"));
personList.add(new Person("Mr","Burns"));
//... repeat few times
return personList;
}
}

Categories

Resources