I am able to retrieve text data successfully from firebase, but i got some difficulty in doing the same for images. I am using a recycler view and below is my recycler adapter:
public class RecyclerviewAdapter extends RecyclerView.Adapter<RecyclerviewAdapter.MyHolder>{
List<Listdata> listdata;
public RecyclerviewAdapter(List<Listdata> listdata) {
this.listdata = listdata;
}
#Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.myview,parent,false);
MyHolder myHolder = new MyHolder(view);
return myHolder;
}
public void onBindViewHolder(MyHolder holder, int position) {
Listdata data = listdata.get(position);
holder.vname.setText(data.getName());
holder.vemail.setText(data.getEmail());
holder.vaddress.setText(data.getAddress());
}
#Override
public int getItemCount() {
return listdata.size();
}
class MyHolder extends RecyclerView.ViewHolder{
TextView vname , vaddress,vemail;
ImageView thumbnail;
public MyHolder(View itemView) {
super(itemView);
vname = (TextView) itemView.findViewById(R.id.vname);
thumbnail = (ImageView) itemView.findViewById(thumbnail);
vemail = (TextView) itemView.findViewById(R.id.vemail);
vaddress = (TextView) itemView.findViewById(R.id.vaddress);
}
}
}
and here is activity code that initialises the process:
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
list = new ArrayList<>();
// StringBuffer stringbuffer = new StringBuffer();
for(DataSnapshot dataSnapshot1 :dataSnapshot.getChildren()){
Userdetails userdetails = dataSnapshot1.getValue(Userdetails.class);
Listdata listdata = new Listdata();
String name=userdetails.getName();
String email=userdetails.getEmail();
String address=userdetails.getAddress();
listdata.setName(name);
listdata.setEmail(email);
listdata.setAddress(address);
list.add(listdata);
// Toast.makeText(MainActivity.this,""+name,Toast.LENGTH_LONG).show();
}
RecyclerviewAdapter recycler = new RecyclerviewAdapter(list);
RecyclerView.LayoutManager layoutmanager = new LinearLayoutManager(InvestmentDrawer.this);
recyclerview.setLayoutManager(layoutmanager);
recyclerview.setItemAnimator( new DefaultItemAnimator());
recyclerview.setAdapter(recycler);
}
#Override
public void onCancelled(DatabaseError error) {
// Failed to read value
// Log.w(TAG, "Failed to read value.", error.toException());
}
});
Glide.with(this)
.load(imageData)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(ivImgGlide);
}
I would like to use the url entered in the database to load the images dynamically. Using glide or picasso. Below is the Listdata class
public class Listdata {
public String name;
public String address;
public String email;
public String imageurl;
public String getImageUrl() {
return imageurl;
}
public void SetImageUrl(String imageurl) {
this.imageurl = imageurl;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
You can use Picasso or glide inside your onBindViewHolder
public class RecyclerviewAdapter extends RecyclerView.Adapter<RecyclerviewAdapter.MyHolder>{
List<Listdata> listdata;
Context context;
public RecyclerviewAdapter(List<Listdata> listdata,Context context) {
this.listdata = listdata;
this.context = context;
}
#Override
public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.myview,parent,false);
MyHolder myHolder = new MyHolder(view);
return myHolder;
}
public void onBindViewHolder(MyHolder holder, int position) {
Listdata data = listdata.get(position);
holder.vname.setText(data.getName());
holder.vemail.setText(data.getEmail());
holder.vaddress.setText(data.getAddress());
Glide.with(context)
.load(data.getImageUrl())
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(holder.thumbnail);
//with Picasso
Picasso.with(context)
.load(data.getImageUrl())
.into(holder.thumbnail);
// you need to define a method getImageUrl() to get the image url in Listdata
}
#Override
public int getItemCount() {
return listdata.size();
}
class MyHolder extends RecyclerView.ViewHolder{
TextView vname , vaddress,vemail;
ImageView thumbnail;
public MyHolder(View itemView) {
super(itemView);
vname = (TextView) itemView.findViewById(R.id.vname);
thumbnail = (ImageView) itemView.findViewById(thumbnail);
vemail = (TextView) itemView.findViewById(R.id.vemail);
vaddress = (TextView) itemView.findViewById(R.id.vaddress);
}
}
}
Remember you need to pass context from your parent activity into your adapter
In you onDataChanged you need to set the Image url using SetImageUrl()
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
list = new ArrayList<>();
// StringBuffer stringbuffer = new StringBuffer();
for(DataSnapshot dataSnapshot1 :dataSnapshot.getChildren()){
Userdetails userdetails = dataSnapshot1.getValue(Userdetails.class);
Listdata listdata = new Listdata();
String name=userdetails.getName();
String email=userdetails.getEmail();
String address=userdetails.getAddress();
String imageUrl = userdetails.getImageUrl();
listdata.setName(name);
listdata.setEmail(email);
listdata.setAddress(address);
listdata.SetImageUrl(imageUrl);
list.add(listdata);
// Toast.makeText(MainActivity.this,""+name,Toast.LENGTH_LONG).show();
}
RecyclerviewAdapter recycler = new RecyclerviewAdapter(list,getApplicationContext());
RecyclerView.LayoutManager layoutmanager = new LinearLayoutManager(InvestmentDrawer.this);
recyclerview.setLayoutManager(layoutmanager);
recyclerview.setItemAnimator( new DefaultItemAnimator());
recyclerview.setAdapter(recycler);
}
Related
This question already has answers here:
recyclerview No adapter attached; skipping layout
(38 answers)
Closed 1 year ago.
Don't know why it says "No adapter attached"
I set the layout manager and the recycler view, in middle of showProducts(); method i also set the adapter to the recycler view and still says: "No adapter attached" Can't find where is the issue, i'll gladly accept some help. Thanks folks!
Nothing shows on screen for the following code:
Main activity:
public class MainActivity extends AppCompatActivity {
private RecyclerView rView;
private RecyclerView.LayoutManager layoutManager;
private List<Product> products;
private ProductAdapter productAdapter;
private DatabaseReference mDataBase;
private CardView cardView;
//Product
private String id;
private String name;
private String author;
private String genre;
private String description;
private double price;
private int stock;
private String image;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
products = new ArrayList<Product>();
mDataBase = FirebaseDatabase.getInstance().getReference();
cardView = (CardView) findViewById(R.id.cardView);
layoutManager = new LinearLayoutManager(this);
rView = (RecyclerView) findViewById(R.id.recyclerView);
rView.setHasFixedSize(true);
rView.setLayoutManager(layoutManager);
showProducts();
registerForContextMenu(rView);
}
private void showProducts() {
mDataBase.child("Products").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
products.clear();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
name = ds.child("title").getValue().toString();
author = ds.child("author").getValue().toString();
genre = ds.child("genre").getValue().toString();
description = ds.child("description").getValue().toString();
price = Double.parseDouble(ds.child("price").getValue().toString());
stock = Integer.parseInt(ds.child("stock").getValue().toString());
image = ds.child("image").getValue().toString();
id = ds.getKey();
Product product = new Product(id, name, author, genre, description, price, stock, image);
products.add(product);
}
productAdapter = new ProductAdapter(products, R.layout.recycler_view, new ProductAdapter.OnItemClickListener() {
#Override
public void onItemClick(Product product, int position) {
}
}, new ProductAdapter.OnButtonClickListener() {
#Override
public void onButtonClick(Product product, int position) {
}
});
}
rView.setAdapter(productAdapter);
}
// Observa como pasamos el activity, con this. Podríamos declarar
// Activity o Context en el constructor y funcionaría pasando el mismo valor, this
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
Adapter:
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ViewHolder>{
private Context context;
private List<Product> products;
private int layout;
private OnItemClickListener itemClickListener;
private OnButtonClickListener btnClickListener;
public ProductAdapter(List<Product> products, int layout, OnItemClickListener itemListener, OnButtonClickListener btnListener) {
this.products = products;
this.layout = layout;
this.itemClickListener = itemListener;
this.btnClickListener = btnListener;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(layout, parent, false);
context = parent.getContext();
ViewHolder vh = new ViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.bind(products.get(position), itemClickListener, btnClickListener);
}
#Override
public int getItemCount() {
return products.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView name;
public TextView author;
public TextView genre;
public TextView description;
public TextView price;
public TextView stock;
public ImageView image;
public Button btnDelete;
public ViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.textViewProductName);
author = (TextView) itemView.findViewById(R.id.textViewProductAuthor);
genre = (TextView) itemView.findViewById(R.id.textViewProductGenre);
description = (TextView) itemView.findViewById(R.id.textViewProductDescription);
price = (TextView) itemView.findViewById(R.id.textViewProductPrice);
stock = (TextView) itemView.findViewById(R.id.textViewProductStock);
image = (ImageView) itemView.findViewById(R.id.imageViewProduct);
btnDelete = (Button) itemView.findViewById(R.id.buttonDeleteCity);
}
public void bind(final Product product, final OnItemClickListener itemListener, final OnButtonClickListener btnListener) {
name.setText(product.getName());
description.setText(product.getDescription());
price.setText((int) product.getPrice());
Picasso.get().load(product.getImage()).fit().into(image);
btnDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btnListener.onButtonClick(product, getAdapterPosition());
}
});
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
itemListener.onItemClick(product, getAdapterPosition());
}
});
}
}
public interface OnItemClickListener {
void onItemClick(Product product, int position);
}
public interface OnButtonClickListener {
void onButtonClick(Product product, int position);
}
}
Bind Adapter in onCreate
Use it like this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
products = new ArrayList<Product>();
mDataBase = FirebaseDatabase.getInstance().getReference();
cardView = (CardView) findViewById(R.id.cardView);
layoutManager = new LinearLayoutManager(this);
rView = (RecyclerView) findViewById(R.id.recyclerView);
rView.setHasFixedSize(true);
rView.setLayoutManager(layoutManager);
rView.setAdapter(productAdapter);
showProducts();
registerForContextMenu(rView);
}
private void showProducts() {
mDataBase.child("Products").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
products.clear();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
name = ds.child("title").getValue().toString();
author = ds.child("author").getValue().toString();
genre = ds.child("genre").getValue().toString();
description = ds.child("description").getValue().toString();
price = Double.parseDouble(ds.child("price").getValue().toString());
stock = Integer.parseInt(ds.child("stock").getValue().toString());
image = ds.child("image").getValue().toString();
id = ds.getKey();
Product product = new Product(id, name, author, genre, description, price, stock, image);
products.add(product);
}
productAdapter = new ProductAdapter(products, R.layout.recycler_view, new ProductAdapter.OnItemClickListener() {
#Override
public void onItemClick(Product product, int position) {
}
}, new ProductAdapter.OnButtonClickListener() {
#Override
public void onButtonClick(Product product, int position) {
}
});
}
productAdapter.notifyDataStateChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
I had ask same question 1 day ago and my question got closed so i ask it again.
Don't know why it says "No adapter attached"
I set the layout manager and the recycler view, in middle of showProducts(); method i also set the adapter to the recycler view and still says: "No adapter attached" Can't find where is the issue, i'll gladly accept some help. Thanks folks!
Nothing shows on screen for the following code:
Main activity:
public class MainActivity extends AppCompatActivity {
private RecyclerView rView;
private RecyclerView.LayoutManager layoutManager;
private List<Product> products;
private ProductAdapter productAdapter;
private DatabaseReference mDataBase;
private CardView cardView;
//Product
private String id;
private String name;
private String author;
private String genre;
private String description;
private double price;
private int stock;
private String image;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
products = new ArrayList<Product>();
mDataBase = FirebaseDatabase.getInstance().getReference();
cardView = (CardView) findViewById(R.id.cardView);
layoutManager = new LinearLayoutManager(this);
rView = (RecyclerView) findViewById(R.id.recyclerView);
rView.setHasFixedSize(true);
rView.setLayoutManager(layoutManager);
showProducts();
registerForContextMenu(rView);
}
private void showProducts() {
mDataBase.child("Products").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
products.clear();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
name = ds.child("title").getValue().toString();
author = ds.child("author").getValue().toString();
genre = ds.child("genre").getValue().toString();
description = ds.child("description").getValue().toString();
price = Double.parseDouble(ds.child("price").getValue().toString());
stock = Integer.parseInt(ds.child("stock").getValue().toString());
image = ds.child("image").getValue().toString();
id = ds.getKey();
Product product = new Product(id, name, author, genre, description, price, stock, image);
products.add(product);
}
productAdapter = new ProductAdapter(products, R.layout.recycler_view, new ProductAdapter.OnItemClickListener() {
#Override
public void onItemClick(Product product, int position) {
}
}, new ProductAdapter.OnButtonClickListener() {
#Override
public void onButtonClick(Product product, int position) {
}
});
}
rView.setAdapter(productAdapter);
}
// Observa como pasamos el activity, con this. Podríamos declarar
// Activity o Context en el constructor y funcionaría pasando el mismo valor, this
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
}
Adapter:
public class ProductAdapter extends RecyclerView.Adapter<ProductAdapter.ViewHolder>{
private Context context;
private List<Product> products;
private int layout;
private OnItemClickListener itemClickListener;
private OnButtonClickListener btnClickListener;
public ProductAdapter(List<Product> products, int layout, OnItemClickListener itemListener, OnButtonClickListener btnListener) {
this.products = products;
this.layout = layout;
this.itemClickListener = itemListener;
this.btnClickListener = btnListener;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(layout, parent, false);
context = parent.getContext();
ViewHolder vh = new ViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.bind(products.get(position), itemClickListener, btnClickListener);
}
#Override
public int getItemCount() {
return products.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView name;
public TextView author;
public TextView genre;
public TextView description;
public TextView price;
public TextView stock;
public ImageView image;
public Button btnDelete;
public ViewHolder(View itemView) {
super(itemView);
name = (TextView) itemView.findViewById(R.id.textViewProductName);
author = (TextView) itemView.findViewById(R.id.textViewProductAuthor);
genre = (TextView) itemView.findViewById(R.id.textViewProductGenre);
description = (TextView) itemView.findViewById(R.id.textViewProductDescription);
price = (TextView) itemView.findViewById(R.id.textViewProductPrice);
stock = (TextView) itemView.findViewById(R.id.textViewProductStock);
image = (ImageView) itemView.findViewById(R.id.imageViewProduct);
btnDelete = (Button) itemView.findViewById(R.id.buttonDeleteCity);
}
public void bind(final Product product, final OnItemClickListener itemListener, final OnButtonClickListener btnListener) {
name.setText(product.getName());
description.setText(product.getDescription());
price.setText((int) product.getPrice());
Picasso.get().load(product.getImage()).fit().into(image);
btnDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btnListener.onButtonClick(product, getAdapterPosition());
}
});
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
itemListener.onItemClick(product, getAdapterPosition());
}
});
}
}
public interface OnItemClickListener {
void onItemClick(Product product, int position);
}
public interface OnButtonClickListener {
void onButtonClick(Product product, int position);
}
}
Move this line inside if condition rView.setAdapter(productAdapter); in your showProducts function
like below
private void showProducts() {
mDataBase.child("Products").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
products.clear();
for (DataSnapshot ds : dataSnapshot.getChildren()) {
name = ds.child("title").getValue().toString();
author = ds.child("author").getValue().toString();
genre = ds.child("genre").getValue().toString();
description = ds.child("description").getValue().toString();
price = Double.parseDouble(ds.child("price").getValue().toString());
stock = Integer.parseInt(ds.child("stock").getValue().toString());
image = ds.child("image").getValue().toString();
id = ds.getKey();
Product product = new Product(id, name, author, genre, description, price, stock, image);
products.add(product);
}
productAdapter = new ProductAdapter(products, R.layout.recycler_view, new ProductAdapter.OnItemClickListener() {
#Override
public void onItemClick(Product product, int position) {
}
}, new ProductAdapter.OnButtonClickListener() {
#Override
public void onButtonClick(Product product, int position) {
}
}); rView.setAdapter(productAdapter); }
}
// Observa como pasamos el activity, con this. Podríamos declarar
// Activity o Context en el constructor y funcionaría pasando el mismo valor, this
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
}
ProductAdapter productAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
products = new ArrayList<Product>();
mDataBase = FirebaseDatabase.getInstance().getReference();
cardView = (CardView) findViewById(R.id.cardView);
productAdapter = new ProductAdapter(products, R.layout.recycler_view, new ProductAdapter.OnItemClickListener() {
#Override
public void onItemClick(Product product, int position) {
}
}, new ProductAdapter.OnButtonClickListener() {
#Override
public void onButtonClick(Product product, int position) {
}
});
layoutManager = new LinearLayoutManager(this);
rView = (RecyclerView) findViewById(R.id.recyclerView);
rView.setHasFixedSize(true);
rView.setLayoutManager(layoutManager);
rView.setAdapter(productAdapter);
showProducts();
registerForContextMenu(rView);
}
make sure you initialize your adapter in your onCreate as I did and for your list do this in the onDataChange
`Product product = new Product(id, name, author, genre,
description, price, stock, image);
products.add(product);
productAdapter.notifyDataSetChanged();`
I want display pictures from firebase realtime database. (with Encryted image(String))
like Encrypted Image is "photo"
I run this app recyclerview didn't appear with error code
No adapter attached; skipping layout
ItemObject.java
public class ItemObject {
private String title;
private String Photo;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getPhoto() {
return Photo;
}
public void setPhoto(String photo){
this.Photo=photo;
}
public ItemObject(String title, String photo) {
this.title = title;
this.Photo=photo;
}
public ItemObject() {}
}
Recyclerview adpater MyAdapter.java
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.RecyclerViewHolders> {
private Context context;
private ArrayList<ItemObject> mItem;
private AdapterView.OnItemClickListener mListener = null;
public class RecyclerViewHolders extends RecyclerView.ViewHolder
implements View.OnCreateContextMenuListener {
TextView title;
ImageView imageView;
// private CardView cardView;
public RecyclerViewHolders(#NonNull View itemView) {
super(itemView);
this.title = itemView.findViewById(R.id.title_listitem);
this.imageView = itemView.findViewById(R.id.photo_listitem);
//
itemView.setOnCreateContextMenuListener(this);
}
private final MenuItem.OnMenuItemClickListener onEditMenu = new MenuItem.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case 1001:
mItem.remove(getAdapterPosition());
notifyItemRemoved(getAdapterPosition());
notifyItemRangeChanged(getAdapterPosition(), mItem.size());
break;
}
return true;
}
};
#Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
MenuItem Delete = menu.add(Menu.NONE,1001,1,"삭제");
Delete.setOnMenuItemClickListener(onEditMenu);
}
}
public MyAdapter(ArrayList<ItemObject> item){
this.context = context;
this.mItem = item;
}
#NonNull
#Override
public RecyclerViewHolders onCreateViewHolder(#NonNull ViewGroup viewGroup, int viewType) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.listitem,viewGroup,false);
RecyclerViewHolders viewHolders=new RecyclerViewHolders(view);
return viewHolders;
}
#Override
public void onBindViewHolder(#NonNull RecyclerViewHolders holder, int position) {
/* Glide.with(context)
.load(mItem.get(position).getPhoto())
.into(holder.imageView);
*/
ItemObject itemObject = mItem.get(position);
String photo = itemObject.getPhoto();
holder.title.setTextSize(TypedValue.COMPLEX_UNIT_SP,10);
holder.title.setGravity(Gravity.CENTER);
holder.title.setText(itemObject.getTitle());
holder.imageView.setImageURI(Uri.parse(photo));
// Glide.with(this.context).load(mItem.get(position).getPhoto()).centerCrop().into(holder.imageView);
}
public void setmItem(ArrayList<ItemObject> mItem) {
this.mItem = mItem;
}
public void deleteItem(int position) {
mItem.remove(position);
notifyItemRemoved(position);
}
#Override
public int getItemCount() {
return mItem.size();
}
}
MainUpload.java
mRecyclerView = findViewById(R.id.recyclerview_main_list);
int numberOfColumns = 3;
GridLayoutManager mGridLayoutManager = new GridLayoutManager(getApplication(),numberOfColumns);
mRecyclerView.setLayoutManager(mGridLayoutManager);
mRecyclerView.setHasFixedSize(true);
reference = FirebaseDatabase.getInstance().getReference();
mItem = new ArrayList<>();
//clearAll();
loadPhoto();
DividerItemDecoration dividerItemDecoration=new DividerItemDecoration(mRecyclerView.getContext(),mGridLayoutManager.getOrientation());
mRecyclerView.addItemDecoration(dividerItemDecoration);
mRecyclerView.addOnItemTouchListener(new RecyclerTouchListener(getApplicationContext(), mRecyclerView, new ClickListener() {
#Override
public void onClick(View view, int position) {
ItemObject itemObject = mItem.get(position);
Intent intent = new Intent(getApplicationContext(), DetailActivity.class);
intent.putExtra("title",itemObject.getTitle());
intent.putExtra("photo",itemObject.getPhoto());
startActivity(intent);
}
#Override
public void onLongClick(View view, int position) {
}
}));
function loadPhoto()
private void loadPhoto() {
FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
String key = FirebaseDatabase.getInstance().getInstance().getReference("users")
.child(currentUser.getUid()).child("Object").getKey();
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("users")
.child(currentUser.getUid()).child("Object").child(key);
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
mRecyclerView = findViewById(R.id.recyclerview_main_list);
int numberOfColumns = 3;
GridLayoutManager mGridLayoutManager = new GridLayoutManager(getApplication(),numberOfColumns);
mRecyclerView.setLayoutManager(mGridLayoutManager);
mRecyclerView.setHasFixedSize(true);
ItemObject itemObject = new ItemObject();
//여기서 에러
String En1 = snapshot.child("photo").getValue().toString();
byte []En2 = En1.getBytes();
AESCoderAndriod aesCoderAndriod = new AESCoderAndriod();
try {
byte []Dn1 = aesCoderAndriod.decrypt(seed,En2);
Bitmap Dn2 = byteArrayToBitmap(Dn1);
Uri Dn3 = getImageUri(getApplicationContext(),Dn2);
itemObject.setPhoto(Dn3.toString());
itemObject.setTitle(snapshot.child("title").getValue().toString());
mItem.add(itemObject);
} catch (Exception e) {
e.printStackTrace();
}
// itemObject.setPhoto(snapshot.child("Photo").getValue().toString());
}
//myAdapter= new MyAdapter(getApplicationContext(),mItem);
// mRecyclerView.setAdapter(myAdapter);
myAdapter = new MyAdapter(mItem);
mRecyclerView.setAdapter(myAdapter);
myAdapter.notifyDataSetChanged();
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
what is the problem? I connect adapter and recyclerview, and set gridmanager.
You should set your adapter at first, Start with empty recycler view, when data loads you should notify your adapter about the change.
mRecyclerView.setLayoutManager(mGridLayoutManager);
mRecyclerView.setHasFixedSize(true);
mItem = new ArrayList<>();
myAdapter = new MyAdapter(mItem);
mRecyclerView.setAdapter(myAdapter);
loadPhoto();
You function already include notify data change method. You don't need to set new layout manager on data load method
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
ItemObject itemObject = new ItemObject();
//여기서 에러
String En1 = snapshot.child("photo").getValue().toString();
byte []En2 = En1.getBytes();
AESCoderAndriod aesCoderAndriod = new AESCoderAndriod();
try {
byte []Dn1 = aesCoderAndriod.decrypt(seed,En2);
Bitmap Dn2 = byteArrayToBitmap(Dn1);
Uri Dn3 = getImageUri(getApplicationContext(),Dn2);
itemObject.setPhoto(Dn3.toString());
itemObject.setTitle(snapshot.child("title").getValue().toString());
mItem.add(itemObject);
} catch (Exception e) {
e.printStackTrace();
}
// itemObject.setPhoto(snapshot.child("Photo").getValue().toString());
}
myAdapter.notifyDataSetChanged();
}
I was trying to load data from firebase into my recycler view. There are two values that I am retrieving first is Name and second is the ImageUrl. When i run my app the value from the name is displaying on the recyclerview but the image is still blank. Somehow picasso is failing to load the image into the CircleImageView.
Below is my ViewHolderclass
public static class CategoryViewHolder extends RecyclerView.ViewHolder {
View mView;
CircleImageView categoryImageView;
TextView categoryName;
public CategoryViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setDisplayName(String name){
categoryName = (TextView) mView.findViewById(R.id.Category_name);
categoryName.setText(name);
}
public void setUserImage(String thumb_image, Context ctx){
categoryImageView = (CircleImageView)
mView.findViewById(R.id.Category_pic);
Picasso.with(ctx).load(thumb_image).placeholder(R.drawable.
default_avatar)
.into( categoryImageView);
// Picasso.with(ctx) .load(thumb_image) .into(categoryImageView);
}
}
this is my model class
public class CategoryItems {
private String Name;
private String ImageUrl;
public CategoryItems(String title, String imageURL) {
this.Name = title;
this.ImageUrl = imageURL;
}
publ ic CategoryItems(){
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getImageUrl() {
return ImageUrl;
}
public void setImageUrl(String imageUrl) {
ImageUrl = imageUrl;
}
}
this is my FirebaseREcyclerViewAdapter
public void onStart() {
super.onStart();
startListening();
}
public void startListening(){
Query query = FirebaseDatabase.getInstance()
.getReference()
.child("Catlist")
.limitToLast(500);
FirebaseRecyclerOptions<CategoryItems> options =
new FirebaseRecyclerOptions.Builder<CategoryItems>()
.setQuery(query, CategoryItems.class)
.build();
FirebaseRecyclerAdapter adapter = new
FirebaseRecyclerAdapter<CategoryItems, CategoryViewHolder>(options) {
#Override
public CategoryViewHolder onCreateViewHolder(ViewGroup parent, int
viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.category_items, parent, false);
return new CategoryViewHolder(view);
}
#Override
protected void onBindViewHolder(final CategoryViewHolder holder, int
position, final CategoryItems model) {
holder.setDisplayName(model.getName());
holder.setUserImage(model.getImageUrl(), getContext());
// holder.setUserStatus(model.getStatus());
Log.d(model.getImageUrl().toString(), "onBindViewHolder: ");
}
};
mUsersList.setAdapter(adapter);
adapter.startListening();
}
my recycler is displaying names only and i tried to log the imageurls and its displaying the url fine
Try enabling logging in Picasso to look at errors and warning, it will definitely show some error in your case which you can resolve.
Picasso
.with(context)
.setLoggingEnabled(true)
....
For more information check out this link
I am trying to fetch data from my Parse database and display it into a RecyclerView. From my knowledge I cannot see what I have done wrong and when I run the app not errors occur, and when opening this very fragment the app doesn't crash just shows a blank screen.
Any idea what is the problem?
Businesses
package zafir.com.app;
import com.parse.ParseClassName;
import com.parse.ParseObject;
#ParseClassName("Businesses")
public class Businesses extends ParseObject
{
private String Name;
public String getName()
{
return getString("Name");
}
public void setName(String name)
{
put("Name", name);
}
public String getCategory()
{
return getString("Category");
}
public void setCategory(String category)
{
put("Category", category);
}
public String getEmail()
{
return getString("Email");
}
public void setEmail(String email)
{
put("Email", email);
}
public String getLocation()
{
return getString("Location");
}
public void setLocation(String location)
{
put("Location", location);
}
public String getPhone()
{
return getString("Phone");
}
public void setPhone(String phone)
{
put("Phone", phone);
}
public String getWebsite()
{
return getString("Website");
}
public void setWebsite(String website)
{
put("Website", website);
}
}
RecyclerAdapter
public static class ViewHolder extends RecyclerView.ViewHolder
{
public TextView zName;
public TextView zPhone;
public TextView zEmail;
public TextView zWebsite;
public TextView zLocation;
public TextView zCategory;
public ViewHolder(View itemView)
{
super(itemView);
zName = (TextView) itemView.findViewById(R.id.name);
zPhone = (TextView) itemView.findViewById(R.id.phone);
zEmail = (TextView) itemView.findViewById(R.id.email);
zWebsite = (TextView) itemView.findViewById(R.id.website);
zLocation = (TextView) itemView.findViewById(R.id.location);
zCategory = (TextView) itemView.findViewById(R.id.category);
}
}
public RecyclerAdapter(Context context,List<Businesses> data)
{
inflater=LayoutInflater.from(context);
this.data= data;
}
#Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View recView = inflater.inflate(R.layout.recycler_layout, parent, false);
ViewHolder ViewHolder = new ViewHolder(recView);
return ViewHolder;
}
#Override
public void onBindViewHolder(RecyclerAdapter.ViewHolder viewHolder, int position)
{
Businesses businesses = data.get(position);
TextView name = viewHolder.zName;
name.setText(businesses.getName());
TextView phone = viewHolder.zPhone;
phone.setText(businesses.getPhone());
TextView email = viewHolder.zEmail;
email.setText(businesses.getEmail());
TextView website = viewHolder.zWebsite;
website.setText(businesses.getWebsite());
TextView location = viewHolder.zLocation;
location.setText(businesses.getLocation());
TextView category = viewHolder.zCategory;
category.setText(businesses.getCategory());
}
#Override
public int getItemCount()
{
return data.size();
}
}
Categories(Fragment)
public class Categories extends Fragment
{
List<Businesses> data = new ArrayList<>();
private RecyclerView zRecyclerView;
private RecyclerAdapter zAdapter;
private RecyclerView.LayoutManager zLayoutManager;
public Categories()
{
// Required empty public constructor
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable
Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.fragment_categories,container, false);
zRecyclerView = (RecyclerView) rootView.findViewById(R.id.recview_categories);
zRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
getData();
return super.onCreateView(inflater, container, savedInstanceState);
}
public void getData()
{
final List<Businesses> data = new ArrayList<>();
ParseQuery<Businesses> query = new ParseQuery<>("Businesses");
query.findInBackground(new FindCallback<Businesses>()
{
#Override
public void done(List<Businesses> list, ParseException e)
{
if(e == null)
{
for(Businesses businesses : list)
{
Businesses bizList = new Businesses();
bizList.setNames(businesses.getNames());
bizList.setPhone(businesses.getPhone());
bizList.setEmail(businesses.getEmail());
bizList.setWebsite(businesses.getWebsite());
bizList.setLocation(businesses.getLocation());
bizList.setCategory(businesses.getCategory());
data.add(bizList);
}
zAdapter = new RecyclerAdapter(getActivity(), data);
zRecyclerView.setAdapter(zAdapter);
}
}
});
}
}
Everything looks good. You just have to call notifyDataSetChanged() on zAdapter whenever you are changing the data. So change your code like this:
zAdapter = new RecyclerAdapter(getActivity(), data);
zRecyclerView.setAdapter(zAdapter);
zAdapter.notifyDataSetChanged();