Putting data from a subcollection in a second Layout with FirebaseUI - android

I want to open the data of a Firebase subcollection in a new RecyclerView. The Main Activity has a RecyclerView filled with the data from the first collection and a second activity should be opened through a click on one of the items.This second activity would have the second RecyclerView. My main Activity works fine and I can see all my data. But when I click on one of the items, my app crashes.
This is my MainActivity
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference notebookRef = db.collection("Notebook");
private KisteAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FloatingActionButton buttonAddKiste = findViewById(R.id.button_add_kiste);
buttonAddKiste.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, NewKisteActivity.class));
}
});
setUpRecyclerView();
}
private void setUpRecyclerView(){
Query query = notebookRef.orderBy("priority", Query.Direction.DESCENDING);
FirestoreRecyclerOptions<Kiste> options = new FirestoreRecyclerOptions.Builder<Kiste>()
.setQuery(query, Kiste.class)
.build();
adapter = new KisteAdapter(options);
final RecyclerView recyclerView = findViewById(R.id.recycler_view_kiste);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT |ItemTouchHelper.RIGHT) {
#Override
public boolean onMove(#NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, #NonNull RecyclerView.ViewHolder target) {
return false;
}
#Override
public void onSwiped(#NonNull RecyclerView.ViewHolder viewHolder, int direction) {
adapter.deleteKiste(viewHolder.getAdapterPosition());
final DocumentReference docRef = adapter.getSnapshots().getSnapshot(viewHolder.getAdapterPosition()).getReference();
final Kiste kiste = adapter.getSnapshots().getSnapshot(viewHolder.getAdapterPosition()).toObject(Kiste.class);
Snackbar.make(recyclerView, "Item deleted", Snackbar.LENGTH_LONG)
.setAction("Undo", new View.OnClickListener() {
#Override
public void onClick(View v) {
docRef.set(kiste);
}
}).show();
}
#Override
public void onChildDraw(#NonNull Canvas c, #NonNull RecyclerView recyclerView, #NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
new RecyclerViewSwipeDecorator.Builder(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive)
.addBackgroundColor(ContextCompat.getColor(MainActivity.this, R.color.colorAccent))
.addActionIcon(R.drawable.ic_delete_)
.create()
.decorate();
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
}).attachToRecyclerView(recyclerView);
adapter.setOnKisteClickListen(new KisteAdapter.onItemClickListener() {
#Override
public void onKisteClick(DocumentSnapshot documentSnapshot, int position) {
final DocumentReference docRef = documentSnapshot.getReference();
final CollectionReference colRef = documentSnapshot.getReference().collection("A");
final Intent intent = new Intent(MainActivity.this,SecondaryActivity.class);
colRef.get().addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
#Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
if (queryDocumentSnapshots.isEmpty()){
colRef.add(new Item("DFB","SGRDG",5));
/*intent.putExtra("Document Reference",docRef.getId());
intent.putExtra("Collection Reference",colRef.getId());*/
startActivity(intent);
}
else {
startActivity(intent);
}
}
});
}
});
}
#Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
}
this is the Class Kiste, which the first RecyclerView gets
public class Kiste {
private String title;
private int priority;
public Kiste(){
}
public Kiste(String title, int priority){
this.title= title;
this.priority = priority;
}
public String getTitle() {
return title;
}
public int getPriority() {
return priority;
}
}
And this is the adapter
public class KisteAdapter extends FirestoreRecyclerAdapter<Kiste, KisteAdapter.KisteHolder> {
private onItemClickListener listener;
public KisteAdapter(#NonNull FirestoreRecyclerOptions<Kiste> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull KisteHolder holder, int position, #NonNull Kiste model) {
holder.textViewTitle.setText(model.getTitle());
holder.textViewPriority.setText(String.valueOf(model.getPriority()));
}
#NonNull
#Override
public KisteHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.kiste_item,parent,false);
return new KisteHolder(v);
}
public void deleteKiste(int position){
getSnapshots().getSnapshot(position).getReference().delete();
}
class KisteHolder extends RecyclerView.ViewHolder{
TextView textViewTitle;
TextView textViewPriority;
public KisteHolder(#NonNull View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.text_view_title);
textViewPriority = itemView.findViewById(R.id.text_view_priority);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = getAdapterPosition();
if (position != RecyclerView.NO_POSITION && listener!= null){
listener.onKisteClick(getSnapshots().getSnapshot(position),position);
}
}
});
}
}
public interface onItemClickListener {
void onKisteClick(DocumentSnapshot documentSnapshot, int position);
}
public void setOnKisteClickListen (onItemClickListener listener) {this.listener = listener;}
}
Since that worked for me, I tried doing the same with the subcollection (As I was still testing if it would work, I just used one DocumentPath that I knew existed ) but as I said above the app keeps crashing.
Here is the secondActivity
public class SecondaryActivity extends AppCompatActivity {
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference colRef = db.collection("Notebook").document(
"JGBzWBwCASivfAjuF8xE").collection("A");
private ItemAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_secondary);
FloatingActionButton buttonAddItem = findViewById(R.id.button_add_item);
setupRecyclerView();
}
private void setupRecyclerView(){
Query query2 = colRef.orderBy("priority", Query.Direction.DESCENDING);
FirestoreRecyclerOptions<Item> options = new FirestoreRecyclerOptions.Builder<Item>()
.setQuery(query2, Item.class)
.build();
adapter = new ItemAdapter(options);
final RecyclerView recyclerView = findViewById(R.id.recycler_view_item);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
#Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
}
the Class Item:
public class Item {
//private Bitmap imageItem;
private String itemName;
private String itemQuantity;
private int priority;
public Item(){
}
public Item( String itemName, String itemQuantity, int priority){
//this.imageItem = imageViewItem;
this.itemName = itemName;
this.itemQuantity = itemQuantity;
this.priority = priority;
}
/*public Bitmap getImageItem() {
return imageItem;
}*/
public String getItemName() {
return itemName;
}
public String getItemQuantity() {
return itemQuantity;
}
public int getPriority() {
return priority;
}
}
and the adapter :
public class ItemAdapter extends FirestoreRecyclerAdapter<Item, ItemAdapter.ItemHolder> {
public ItemAdapter(#NonNull FirestoreRecyclerOptions<Item> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull ItemHolder holder, int position, #NonNull Item model) {
//holder.imageViewItem.setImageBitmap(model.getImageItem());
holder.textViewTitle.setText(model.getItemName());
holder.textViewQuantity.setText(model.getItemQuantity());
holder.textViewPriority.setText(String.valueOf(model.getPriority()));
}
#NonNull
#Override
public ItemHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_item,parent,false);
return new ItemHolder(v);
}
class ItemHolder extends RecyclerView.ViewHolder{
//ImageView imageViewItem;
TextView textViewTitle;
TextView textViewQuantity;
TextView textViewPriority;
public ItemHolder(#NonNull View itemView){
super(itemView);
//imageViewItem.findViewById(R.id.image_view_item);
textViewTitle.findViewById(R.id.text_view_item_name);
textViewQuantity.findViewById(R.id.text_view_item_quantity);
textViewPriority.findViewById(R.id.text_view_priority2);
}
}
}
I would really appreciate it if someone could help me figure out what the problem is.

Thanks for pointing out the error in the console, now it's much easier for us to see where the problem is without the need of reading your whole code :D
You're getting an error in line 47 of your ItemAdapter class. I can't see exactly which line it is, but I'm certain the error is inside your ViewHolder.
class ItemHolder extends RecyclerView.ViewHolder{
//ImageView imageViewItem;
TextView textViewTitle;
TextView textViewQuantity;
TextView textViewPriority;
public ItemHolder(#NonNull View itemView){
super(itemView);
//imageViewItem.findViewById(R.id.image_view_item);
THESE LINES OF CODE -----------------------------------------------------------------
textViewTitle.findViewById(R.id.text_view_item_name);
textViewQuantity.findViewById(R.id.text_view_item_quantity);
textViewPriority.findViewById(R.id.text_view_priority2);
-------------------------------------------------------------------------------------
}
}
replace them with:
this.textViewTitle = (TextView) itemView.findViewById(R.id.text_view_item_name); // and so on...
You can check out this example:
https://www.javatpoint.com/android-recyclerview-list-example

Related

How to open anothor activity when click on record in RecyclerView linked with Firebase

I made an app that fetches and stores data from the Android application to the Firebase, and I made activity that includes RecyclerView in which the data is displayed, but the problem is that when I click on any record in RecyclerView, I want to open another screen that contains the same data that was pressed, and I Try the code mentioned below, but when you press on RecyclerView, the application stops (and I think the problem is in the process of transferring data when I click on the record)
Please help me to solve the problem
The class code
public class EMP_INFO implements Serializable {
private String Id;
private String Name;
private String City;
public EMP_INFO() {
}
public String getEmployeeID() {
return Id;
}
public void setEmployeeID(String ID) {
this.Id = ID;
}
public String getEmployeeName() {
return Name;
}
public void setEmployeeName(String Name) {
this.Name = Name;
}
public String getEmployeeCity() {
return City;
}
public void setEmployeeCity(String City) {
this.City = City;
}
}
The adapter code
public class EMP_INFO_Adapter extends FirebaseRecyclerAdapter<EMP_INFO, EMP_INFO_Adapter.personsViewholder> {
public EMP_INFO_Adapter(#NonNull FirebaseRecyclerOptions<EMP_INFO> options)
{
super(options);
}
#Override
protected void
onBindViewHolder(#NonNull personsViewholder holder, int position, #NonNull EMP_INFO model)
{
holder.F_ID.setText(model.getEmployeeID());
holder.F_name.setText(model.getEmployeeName());
holder.F_city.setText(model.getEmployeeCity());
Intent intent = new Intent(context, FireBase_Save_data_1.class);
intent.putExtra("FB_id1", String.valueOf(model.getEmployeeID()));
intent.putExtra("FB_name1", String.valueOf(model.getEmployeeName()));
intent.putExtra("FB_city1", String.valueOf(model.getEmployeeCity()));
activity.startActivityForResult(intent, 1);
}
#NonNull
#Override
public personsViewholder
onCreateViewHolder(#NonNull ViewGroup parent, int viewType)
{
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fire_base_my_row, parent, false);
return new EMP_INFO_Adapter.personsViewholder(view);
}
class personsViewholder extends RecyclerView.ViewHolder { TextView F_ID, F_name, F_city;
public personsViewholder(#NonNull View itemView)
{
super(itemView);
F_ID = itemView.findViewById(R.id.F_ID);
F_name = itemView.findViewById(R.id.F_name);
F_city = itemView.findViewById(R.id.F_city);
}
}
}
The RecyclerView activity code
public class FireBase_Recyclerview_2 extends AppCompatActivity {
private RecyclerView recyclerView;
Button add_button;
EMP_INFO_Adapter adapter;
//EMP_INFO_Adapter_3 adapter;
DatabaseReference mbase;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fire_base_recyclerview_2);
add_button = findViewById(R.id.add_button);
recyclerView = findViewById(R.id.recycler1);
// To display the Recycler view linearly
recyclerView.setLayoutManager(new LinearLayoutManager(this));
mbase = FirebaseDatabase.getInstance().getReference("EmployeeInfo") ;
FirebaseRecyclerOptions<EMP_INFO> options = new FirebaseRecyclerOptions.Builder<EMP_INFO>()
.setQuery(mbase, EMP_INFO.class).build();
adapter = new EMP_INFO_Adapter(options);
//adapter = new EMP_INFO_Adapter_3(options);
recyclerView.setAdapter(adapter);
add_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(FireBase_Recyclerview_2.this, FireBase_Save_data_1.class);
startActivity(intent);
}
});
}
#Override protected void onStart()
{
super.onStart();
adapter.startListening();
}
#Override protected void onStop()
{
super.onStop();
adapter.stopListening();
}
}
You may put a click Listener on the item inside your adapter's onBindViewHolder,as shown below.
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Context context = v.getContext();
Intent intent = new Intent(context, FireBase_Save_data_1.class);
intent.putExtra("empId", model.getEmployeeID());
intent.putExtra("empName", model.getEmployeeName());
intent.putExtra("empCity", model.getEmployeeCity());
context.startActivity(intent);
}
});
You can't start activity within onBinViewHolder.move that code to viewholder class.
public class EMP_INFO_Adapter extends FirebaseRecyclerAdapter<EMP_INFO, EMP_INFO_Adapter.personsViewholder> {
public EMP_INFO_Adapter(#NonNull FirebaseRecyclerOptions<EMP_INFO> options)
{
super(options);
}
#Override
protected void
onBindViewHolder(#NonNull personsViewholder holder, int position, #NonNull EMP_INFO model)
{
holder.F_ID.setText(model.getEmployeeID());
holder.F_name.setText(model.getEmployeeName());
holder.F_city.setText(model.getEmployeeCity());
}
#NonNull
#Override
public personsViewholder
onCreateViewHolder(#NonNull ViewGroup parent, int viewType)
{
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.fire_base_my_row, parent, false);
return new EMP_INFO_Adapter.personsViewholder(view);
}
class personsViewholder extends RecyclerView.ViewHolder implements View.OnClickListener
TextView F_ID, F_name, F_city;
public personsViewholder(#NonNull View itemView)
{
super(itemView);
F_ID = itemView.findViewById(R.id.F_ID);
F_name = itemView.findViewById(R.id.F_name);
F_city = itemView.findViewById(R.id.F_city);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
int id=getAdapterPosition();
Intent intent = new Intent(context, FireBase_Save_data_1.class);
intent.putExtra("FB_id1", String.valueOf(model.get(id).getEmployeeID()));
intent.putExtra("FB_name1", String.valueOf(model.get(id).getEmployeeName()));
intent.putExtra("FB_city1", String.valueOf(model.get(id).getEmployeeCity()));
activity.startActivityForResult(intent, 1);
}
}
}
}
}

Recyclerview is not showing any data which i have in firebase. Iam able to retrieve the data

HISTOROBJECT.CLASS
public class HistoryObject{
private String mName;
private String mImageUrl;
public HistoryObject(){
}
public HistoryObject(String Name, String Image) {
this.user = Name;
this.msg = Image;
}
private String user, msg;
public String getMsg() {
return msg;
}
public void setMsg(String Name) {
this.msg = Name;
}
public String getUser() {
return user;
}
public void setUser(String Image) {
this.user = Image;
}
}
HISTORYADAPTER.CLASS
public class HistoryAdapter extends RecyclerView.Adapter<HistoryAdapter.ImageViewHolder> {
private Context mContext;
private List<HistoryObject> mUploads;
public HistoryAdapter (Context cx, List<HistoryObject> uploads){
mContext = cx;
mUploads = uploads;
}
#NonNull
#Override
public ImageViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.listofitems, parent, false);
return new ImageViewHolder(v); }
#Override
public void onBindViewHolder(#NonNull ImageViewHolder holder, int position) {
HistoryObject historyObject = mUploads.get(position);
holder.textView.setText(historyObject.getUser());
Picasso.get().load(historyObject.getMsg())
.placeholder(R.mipmap.ic_launcher)
.fit()
.centerCrop()
.into(holder.spapic);
}
#Override
public int getItemCount() {
return mUploads.size() ;
}
public class ImageViewHolder extends RecyclerView.ViewHolder{
public TextView textView;
public ImageView spapic;
public ImageViewHolder(View itemView) {
super(itemView);
textView =(TextView)itemView.findViewById(R.id.nameofspa);
spapic =(ImageView) itemView.findViewById(R.id.spapic);
}
}
}
HOMEPAGE.CLASS
mDatabaseRef = FirebaseDatabase.getInstance().getReference("BeautyCenters");
mDatabaseRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
// Log.d("SPAA", "VALUE IS " +dataSnapshot) ;
Log.d("SPAA", "postsnap IS " +postSnapshot) ;
HistoryObject upload = postSnapshot.getValue(HistoryObject.class);
mUploads.add(upload);
}
mHistoryAdapter = new HistoryAdapter(HomePage.this, mUploads);
mHistoryRecyclerView.setAdapter(mHistoryAdapter);
mHistoryAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
Iam able to retrieve data and can see all list in logcat but it is not displaying in recyclerview. I also want to retrieve the images from the firebase. iam able to show the adapter but the text is not showing in that adapter instead it is blank and also the picture
I have faced this problem before.
Try moving:
mHistoryRecyclerView.setAdapter(mHistoryAdapter);
mHistoryAdapter.notifyDataSetChanged();
out of the listener class to the bottom of onCreate.
If that doesn't work make sure you have this code added:
#Override
public void onStart() {
super.onStart();
if (mAdapter != null) {
mAdapter.startListening();
}
}
If it is still not working then provide a link of your project and I will play around with it.

RecyclerView not showing data until turn off and turn on screen

I am developing an app that shows in a RecyclerView an image and a text that comes from firebase.
When I open the application the recyclerview does not appear but if I turn off and then turn on the screen the recyclerview appears correctly with the image and text obtained from firebase. I need help please
ViewHolder:
public class ViewHolderServicio extends RecyclerView.ViewHolder {
View mView;
public ViewHolderServicio(#NonNull View itemView) {
super(itemView);
mView = itemView;
}
public void setDetails(Context ctx, String nombreServicio, String imagenPerfil) {
TextView mNombreServicio = mView.findViewById(R.id.NombreServicio);
ImageView mImagenPerfil = mView.findViewById(R.id.imageViewServicio);
mNombreServicio.setText(nombreServicio);
Picasso.get().load(imagenPerfil).into(mImagenPerfil);
}
}
Model:
public class ModelServicio {
String idUsuario, nombreServicio, descripcion, imagenPerfil;
public ModelServicio(){}
public String getIdUsuario() {
return idUsuario;
}
public void setIdUsuario(String idUsuario) {
this.idUsuario = idUsuario;
}
public String getNombreServicio() {
return nombreServicio;
}
public void setNombreServicio(String nombreServicio) {
this.nombreServicio = nombreServicio;
}
public String getDescripcion() {
return descripcion;
}
public void setDescripcion(String descripcion) {
this.descripcion = descripcion;
}
public String getImagenPerfil() {
return imagenPerfil;
}
public void setImagenPerfil(String imagenPerfil) {
this.imagenPerfil = imagenPerfil;
}
}
class activity:
public class DetalleServicio extends AppCompatActivity {
RecyclerView RVbot;
FirebaseDatabase mFirebaseDatabase;
DatabaseReference mRef;
FirebaseRecyclerAdapter<ModelServicio, ViewHolderServicio> firebaseRecyclerAdapter;
FirebaseRecyclerOptions<ModelServicio> options;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detalle_servicio);
RVbot = findViewById(R.id.RVBot);
RVbot.setHasFixedSize(true);
RVbot.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
mFirebaseDatabase = FirebaseDatabase.getInstance();
mRef = mFirebaseDatabase.getReference("Servicios");
mostrarDatos();
}
private void mostrarDatos() {
options = new FirebaseRecyclerOptions.Builder<ModelServicio>().setQuery(mRef, ModelServicio.class).build();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<ModelServicio, ViewHolderServicio>(options) {
#Override
protected void onBindViewHolder(#NonNull ViewHolderServicio holder, int position, #NonNull ModelServicio model) {
holder.setDetails(getApplicationContext(), model.getNombreServicio(), model.getImagenPerfil());
}
#NonNull
#Override
public ViewHolderServicio onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.servicioitem, viewGroup, false);
ViewHolderServicio viewHolder = new ViewHolderServicio(itemView);
return viewHolder;
}
};
RVbot.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
}
#Override
protected void onStart() {
super.onStart();
firebaseRecyclerAdapter.startListening();
RVbot.setAdapter(firebaseRecyclerAdapter);
}
}
Have you tried
firebaseRecyclerAdapter.notifyDataSetChanged();
after calling an Adapter?

Recycler View Is So Slow when using if statement for ( i - 0 )

I am currently working on a chat app. In chat activity, I wanted to show profile picture only on the first item and when I used Runnable (onItemChanged) it slowed down my recycler view very much and now it looks like I added motion blur to my recycler view and it is very very lazy.
Here is a gif of my chat activity:
https://i.gyazo.com/f0489ca7fe9ad28519061c0ff8963fb4.gif
Im not actually sure that it is Runnable that slows down my recycler view but when I remove :
if (i > 0)
if (mMessageList.get(i - 0).getFrom().equals(mMessageList.get(i).getFrom())) {
makeDPInvisible(i - 0);
}
if (invisibleIds.contains(i)) {
viewHolder.displayName.setVisibility(View.GONE);
viewHolder.profileImage.setVisibility(View.INVISIBLE);
}
and
private void makeDPInvisible(final int i) {
invisibleIds.add(i);
recyclerView.post(new Runnable(){
#Override
public void run() {
notifyItemRangeChanged(i);
}
});
}
my recycler is very smooth and without any problems...
Here is my MessaageAdapter.java
/**
* Created by Aryojj on 24/07/17.
*/
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageViewHolder>{
private FirebaseAuth mAuth;
private ArrayList<Integer> invisibleIds = new ArrayList<>();
private RecyclerView recyclerView;
private List<Messages> mMessageList;
private DatabaseReference mUserDatabase;
public MessageAdapter(List<Messages> mMessageList) {
this.mMessageList = mMessageList;
}
#Override
public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.message_single_layout ,parent, false);
mAuth = FirebaseAuth.getInstance();
return new MessageViewHolder(v);
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
this.recyclerView = recyclerView;
}
public class MessageViewHolder extends RecyclerView.ViewHolder {
public TextView messageText;
public CircleImageView profileImage;
public TextView displayName;
public ImageView messageImage;
public MessageViewHolder(View view) {
super(view);
messageText = (TextView) view.findViewById(R.id.message_text_layout);
profileImage = (CircleImageView) view.findViewById(R.id.message_profile_layout);
displayName = (TextView) view.findViewById(R.id.name_text_layout);
messageImage = (ImageView) view.findViewById(R.id.message_image_layout);
}
}
#Override
public void onBindViewHolder(final MessageViewHolder viewHolder, int i) {
String message_sender_id = mAuth.getCurrentUser().getUid();
Messages c = mMessageList.get(i);
String from_user = c.getFrom();
String message_type = c.getType();
if (i > 0)
if (mMessageList.get(i - 0).getFrom().equals(mMessageList.get(i).getFrom())) {
makeDPInvisible(i - 0);
}
if (invisibleIds.contains(i)) {
viewHolder.displayName.setVisibility(View.GONE);
viewHolder.profileImage.setVisibility(View.INVISIBLE);
}
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(from_user);
mUserDatabase.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
String name = dataSnapshot.child("name").getValue().toString();
String image = dataSnapshot.child("thumb_image").getValue().toString();
viewHolder.displayName.setText(name);
Picasso.with(viewHolder.profileImage.getContext()).load(image)
.placeholder(R.drawable.default_avatar).into(viewHolder.profileImage);
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
if (message_type.equals("text")) {
viewHolder.messageText.setText(c.getMessage());
viewHolder.messageImage.setVisibility(View.INVISIBLE);
} else {
viewHolder.messageText.setVisibility(View.INVISIBLE);
Picasso.with(viewHolder.profileImage.getContext()).load(c.getMessage())
.networkPolicy(NetworkPolicy.OFFLINE).placeholder(R.drawable.default_avatar).into(viewHolder.messageImage);
}
}
private void makeDPInvisible(final int i) {
invisibleIds.add(i);
recyclerView.post(new Runnable(){
#Override
public void run() {
notifyItemRangeChanged(i);
}
});
}
#Override
public int getItemCount() {
return mMessageList.size();
}
}

RecyclerView is showing nothing

I have some items in my adapter but nothing is shown in the RecyclerView.
Adapter
public class WorkOrderAdapter extends RecyclerView.Adapter<WorkOrderViewHolder> {
private List<WorkOrder> orders = new LinkedList<>();
public void setData(List<WorkOrder> orders) {
this.orders = orders;
}
#Override
public WorkOrderViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_workorder, parent, false);
return new WorkOrderViewHolder(view);
}
#Override
public void onBindViewHolder(WorkOrderViewHolder holder, int position) {
WorkOrder order = orders.get(position);
holder.bind(order);
}
#Override
public int getItemCount() {
return orders.size();
}
}
ViewHolder
public class WorkOrderViewHolder extends RecyclerView.ViewHolder {
private TextView title;
private TextView description;
private TextView date;
public WorkOrderViewHolder(View view) {
super(view);
title = (TextView) view.findViewById(R.id.title_textview);
description = (TextView) view.findViewById(R.id.description_textview);
date = (TextView) view.findViewById(R.id.date_textview);
}
public void bind(WorkOrder order) {
title.setText("Test");
description.setText("Test");
date.setText("Test");
}
}
Activity (Using AndroidAnnotations)
#EActivity(R.layout.activity_workorders)
#OptionsMenu(R.menu.activity_workorders)
public class WorkOrdersActivity extends ToolbarActivity {
#ViewById(R.id.orders_recyclerview)
RecyclerView ordersList;
List<WorkOrder> orders = new LinkedList<>();
private WorkOrderAdapter adapter;
{
adapter = new WorkOrderAdapter();
orders.add(new WorkOrder());
orders.add(new WorkOrder());
orders.add(new WorkOrder());
adapter.setData(orders);
}
#AfterViews
public void initViews() {
ordersList.setAdapter(adapter);
}
}
Please add the LayoutManager to the RecyclerView and try again
ordersList.setLayoutManager(new LinearLayoutManager(this));
ordersList.setAdapter(adapter);

Categories

Resources