Android Using View Holder - android

I try to show in my app using firebase but not return image or the other data
please help me I use it after no any error but now I do not use this method
public class PaylasAnaSayfa extends AppCompatActivity {
private RecyclerView mBlogList;
public DatabaseReference mDatabase;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paylas_ana_sayfa);
mDatabase= FirebaseDatabase.getInstance().getReference().child("Resimler");
mBlogList= (RecyclerView) findViewById(R.id.resim_list);
mBlogList.setHasFixedSize(true);
mBlogList.setLayoutManager(new LinearLayoutManager(this));
if (mDatabase==null){
Toast.makeText(this, "veri tabanı hatası", Toast.LENGTH_SHORT).show();
}else {
FirebaseRecyclerAdapter<PaylasModel, PaylasHolder> adapter = new FirebaseRecyclerAdapter<PaylasModel, PaylasHolder>(
PaylasModel.class,
R.layout.resim_row,
PaylasHolder.class,
mDatabase
) {
#Override
protected void populateViewHolder(PaylasHolder viewHolder, PaylasModel model, int position) {
viewHolder.setImage(getApplicationContext(), model.getImgImage());
viewHolder.setBaslik(model.getImgBaslik());
viewHolder.setAciklama(model.getImgAciklama());
viewHolder.setKullanici(model.getImgKullaniciAdi());
viewHolder.setDers(model.getImgDers());
}
};
mBlogList.setAdapter(adapter);
}
}
private static class PaylasHolder extends RecyclerView.ViewHolder {
View mView;
public PaylasHolder(View itemView) {
super(itemView);
mView=itemView;
}
public void setBaslik(String baslik){
TextView tBaslik= (TextView) mView.findViewById(R.id.row_img_baslik);
tBaslik.setText(baslik);
}
public void setAciklama(String aciklama){
TextView tAciklama= (TextView) mView.findViewById(R.id.row_img_aciklama);
tAciklama.setText(aciklama);
}
public void setKullanici(String kullanici){
TextView tKullanici= (TextView) mView.findViewById(R.id.row_img_kullanici);
tKullanici.setText(kullanici);
}
public void setDers(String ders){
TextView tDers= (TextView) mView.findViewById(R.id.row_img_ders);
tDers.setText(ders);
}
public void setImage(final Context context, final String image){
final ImageView tImage= (ImageView) mView.findViewById(R.id.row_img_image);
Picasso.with(context).load(image).networkPolicy(NetworkPolicy.OFFLINE).into(tImage, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
Picasso.with(context).load(image).into(tImage);
}
});
}
}
}
using view holder do not return data
The word in square brackets is the alt text, which gets displayed if the browser can't show the image. Be sure to include meaningful alt text for screen-reading software.

First you don't need to initialize views everytime in the methods of your ViewHolder.RecyclerView reuses the view from the view pool.
private static class PaylasHolder extends RecyclerView.ViewHolder {
View mView;
TextView tBaslik;
public PaylasHolder(View itemView) {
super(itemView);
mView=itemView;
tBaslik = mView.findViewById(R.id.row_img_baslik);
//intialize all you view here once and for all.
}
public void setBaslik(String baslik){
tBaslik.setText(baslik);
}
public void setAciklama(String aciklama){
tAciklama.setText(aciklama);
}
public void setKullanici(String kullanici){
tKullanici.setText(kullanici);
}
public void setDers(String ders){
tDers.setText(ders);
}
public void setImage(final Context context, final String image){
Picasso.with(context).load(image).networkPolicy(NetworkPolicy.OFFLINE).into(tImage, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
Picasso.with(context).load(image).into(tImage);
}
});
}
}
If you are very much sure about the size of the recyclerview then only use setHasFixedSize(true) otherwise it will not request layout upon the addition of data to the adapter.

Related

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?

setItemOnclickListener not working in FirebaseRecyclerAdapter

Im new to FirebASE and Stuck on setItemclickListener , here are some snippets shown please help me. Mainly struck in the FirebaseRecycler where the setItemClickListener is the interface and used for Onclick of the Firebaserecy
Leavereqests.java
#Override
//LeaveReqests.java
leavereq= database.getReference("LeafForm");
recyclerview = (RecyclerView)findViewById(R.id.recycler_hod);
recyclerview.setHasFixedSize(true);
LinearLayoutManager manager = new LinearLayoutManager(this);
recyclerview.setLayoutManager(manager);
loadhod();
}
private void loadhod() {
adapter = new FirebaseRecyclerAdapter<LeaveReq, HodViewHolder>(LeaveReq.class,R.layout.hod_login_list
,HodViewHolder.class,leavereq) {
#Override
protected void populateViewHolder(HodViewHolder viewHolder, LeaveReq model, int position) {
viewHolder.name.setText(model.getName());
clickitem = model;
viewHolder.setItemClickListener(new ItemClickListener() { //the non working part
#Override
public void onClick(View view, int position) {
Intent hodlist = new Intent(LeaveRequests.this,HodReqList.class);
hodlist.putExtra("hodid",adapter.getRef(position).getKey());
startActivity(hodlist);
}
});
}
};
recyclerview.setAdapter(adapter);}}
HodViewholder.java
public class HodViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public Button name;
private ItemClickListener itemClickListener;
public HodViewHolder(View itemView) {
super(itemView);
name=(Button)itemView.findViewById(R.id.name_of_faculty);
itemView.setOnClickListener(this);
}
public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
#Override
public void onClick(View view) {
itemClickListener.onClick(view,getAdapterPosition(),false);
}}
LeaveReq.java is the class which has getter of the name.
public class LeaveReq {
private String name;
public LeaveReq() {
}
public LeaveReq(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Item Click Listener Interface
public interface ItemClickListener {
void onClick(View view, int position);
}
the list is being populated but the click action is not working
Try something like that
The functions such as the RecycleView buttons should be placed into the HodViewHolder class so that the RecycleView can identify the tools that are clicked by the button
//public static class ViewHolder extends RecyclerView.ViewHolder {..
View mView;
//public ViewHolder(View itemView) {...
super(itemView);
mView = itemView;
viewHolder.mView .setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {....}
I hope to help you..
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<adapter, PandaViewHolder> MyAdapter = new FirebaseRecyclerAdapter <adapter, PandaViewHolder>(
adapter.class,
R.layout.replay_panda,
HodViewHolder.class,
mDatabase
) {
#Override
protected void populateViewHolder(final PandaViewHolder viewHolder, adapter model, int position) {
final String position_key = getRef( position ).getKey().toString();
viewHolder.setTitle(model.getTitle());
//// Here
//#################################
viewHolder.mView.setOnClik{....}
//#################################
}
};
RecyclerView.setAdapter( MyAdapter );
}
public static class HodViewHolder extends RecyclerView.ViewHolder {
View mView;
DatabaseReference mDatabase1231223;
FirebaseAuth mAuth;
public PandaViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
// post text view
public void setTitle(String title) {
}

FirebaseRecyclerAdapter and Glide 4.2 with the new android 3.0 no works

In last days i update my android studio to 2.3 to 3.0 and then of that i have some problems with proyect and one of these is this:
I have one Holdermaps and Modelmaps class for retrieve the images from firebase, and one design_rowl_maps.layout how content of my images, and for last one activity_maps.xml this have the recyclerview what content all image that show to UI.
The problem is before update all is working ok, but after update the image dont charge more. I try to change somethings in the code with new code of the library of glide a FirebaseRecyclerAdapter but dont work. So this is my code:
Holder:
public class MapsHolder extends RecyclerView.ViewHolder {
View mView;
public MapsHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setPathName(String pathname) {
TextView post_title = mView.findViewById(R.id.text_view_rowl_letters);
post_title.setText(pathname);
}
public void setPathRoute(final Context ctx, final String pathrouth) {
ImageView post_image = mView.findViewById(R.id.image_view_rowl_letters);
Glide.with(ctx).load(pathrouth).into(post_image);
post_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setupDialog(ctx, pathrouth);
}
});
}
private void setupDialog(Context context, String path) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.dialog_image_full, null);
builder.setView(view);
final AlertDialog alertDialog = builder.create();
alertDialog.setCancelable(true);
ImageView img_photo = view.findViewById(R.id.img_photo);
TextView lbl_close = view.findViewById(R.id.lbl_close);
//Glide.with(context).load(R.drawable.ic_alert).into(img_photo);
Glide.with(context).load(path).into(img_photo);
lbl_close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
alertDialog.show();
}
Model
public class MapsHolder extends RecyclerView.ViewHolder {
View mView;
public MapsHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setPathName(String pathname) {
TextView post_title = mView.findViewById(R.id.text_view_rowl_letters);
post_title.setText(pathname);
}
public void setPathRoute(final Context ctx, final String pathrouth) {
ImageView post_image = mView.findViewById(R.id.image_view_rowl_letters);
Glide.with(ctx).load(pathrouth).into(post_image);
post_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setupDialog(ctx, pathrouth);
}
});
}
Class
public class Maps extends AppCompatActivity {
public Context ctx;
private DatabaseReference dbImages;
FirebaseRecyclerAdapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
setupRecycler();
//mAdapter.startListening();
}
private void setupRecycler() {
dbImages = FirebaseDatabase.getInstance().getReference().child("tblPathUploadsMaps");
Query uploadMaps = dbImages.orderByKey();
RecyclerView recyclerView = findViewById(R.id.rvMaps);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
FirebaseRecyclerOptions<MapsModel> savedQuery =
new FirebaseRecyclerOptions.Builder<MapsModel>().setQuery(uploadMaps, MapsModel.class).build();
mAdapter =
new FirebaseRecyclerAdapter<MapsModel, MapsHolder>(savedQuery)
{
#Override
public MapsHolder onCreateViewHolder(ViewGroup parent, int i) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.design_rowl_maps, parent, false);
return new MapsHolder(view);
}
#Override
protected void onBindViewHolder(MapsHolder holder, int position, MapsModel model) {
holder.setPathName(model.getPathname());
holder.setPathRoute(ctx, model.getPathroute());
}
};
recyclerView.setAdapter(mAdapter);
}
I do not what else to do, because i intent with all but i cant solve the problem. Thanks you for payme atention.
I resolved of this way: Glide.with(mView.getContext()).load(pathrouth).into(post_ima‌​ge);

Adding new image on the top of recyclerview

I am trying to show the new updates on the top. I am using firebase and recyclerview.
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Feed,FeedViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Feed, FeedViewHolder>(
Feed.class,R.layout.news_feed_row,FeedViewHolder.class,mDatabase
) {
#Override
protected void populateViewHolder(FeedViewHolder viewHolder, Feed model, int position) {
viewHolder.setdesc(model.getDesc());
viewHolder.setimage(getApplicationContext(),model.getImage());
}
};
mNews_Feed_list.setAdapter(firebaseRecyclerAdapter);
}
public static class FeedViewHolder extends RecyclerView.ViewHolder{
View mView;
public FeedViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setdesc(String desc){
TextView post_desc_value = (TextView) mView.findViewById(R.id.post_desc_card_view);
post_desc_value.setText(desc);
}
public void setimage(final Context ctx, final String image){
final ImageView post_image = (ImageView) mView.findViewById(R.id.post_image_card_view);
Glide.with(ctx).load(image).diskCacheStrategy(DiskCacheStrategy.ALL).into(post_image);
}
}
Add this method to your adapter and call it to add new item to the recycler view
public void addItem(Feed model){
<your list name>.add(0, model);
notifyItemInserted(0);
mRecyclerView.smoothScrollToPosition(0);
}

Update value on Recyclerview click listener

i develop app using firebase to upload files and view it in Recyclerview
when i click on any item should take me to another activity and view the file in webView the issue is when i click on any item take the first file uploaded to firebase , i need when i click in any item take the file for this item
public class postShows extends AppCompatActivity {
private RecyclerView postlist;
private DatabaseReference mdatabase;
private FirebaseAuth auth;
ImageView imagepostl;
TextView textname;
String ma,mmmmmm;
PostUtils m;
Context context;
public static String imageURL, fileType,fileNAme;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_shows);
mdatabase = FirebaseDatabase.getInstance().getReference().child(PostNew.item);
auth = FirebaseAuth.getInstance();
imagepostl = (ImageView) findViewById(R.id.imagefilesource);
textname = (TextView) findViewById(R.id.textfilename);
postlist = (RecyclerView) findViewById(R.id.postRecycle);
postlist.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
postlist.setLayoutManager(linearLayoutManager);
}
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<PostUtils, PostViewHolder> firebaseRecyclerAdapter = new
FirebaseRecyclerAdapter<PostUtils, PostViewHolder>(
PostUtils.class,
R.layout.post_row,
PostViewHolder.class,
mdatabase
)
{
#Override
protected void populateViewHolder(PostViewHolder viewHolder, PostUtils m, int position) {
fileType = m.getfiletype();
fileNAme=m.getFileName();
// viewHolder.setDesc(m.getDescription());
if (m.getfiletype().equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document")) {
// imagepostl.setImageResource(R.drawable.word);
viewHolder.setFileName(m.getFileName());
viewHolder.setWord(imagepostl);
mmmmmm= viewHolder.setImageURL(ma,m);
} else if (m.getfiletype().equals("application/pdf")) {
viewHolder.setFileName(m.getFileName());
viewHolder.setPDF(imagepostl);
mmmmmm= viewHolder.setImageURL(ma,m);
} else if (m.getfiletype().contains("image")) {
viewHolder.setimage(imagepostl);
mmmmmm= viewHolder.setImageURL(ma,m);
} else {
viewHolder.setFileName(m.getFileName());
{
viewHolder.setimage(imagepostl);
}
}
}
#Override
public PostViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
PostViewHolder viewHolder = super.onCreateViewHolder(parent, viewType);
viewHolder.setOnClickListener(new PostViewHolder.ClickListener() {
#Override
public void onItemClick(View view, int position) {
Intent intent = new Intent(Kasittestbank.getAppContext(), Kasit.class);
intent.putExtra("imageURL",mmmmmm);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Kasittestbank.getAppContext().startActivity(intent);
}
#Override
public void onItemLongClick(View view, int position) {
Toast.makeText(Kasittestbank.getAppContext(), "Item long clicked at " + position, Toast.LENGTH_SHORT).show();
}
});
return viewHolder;
}
};
postlist.setAdapter(firebaseRecyclerAdapter);
}
public static class PostViewHolder extends RecyclerView.ViewHolder {
View mview;
Bitmap bitmap;
FileOpen fileOpen;
public PostViewHolder(View itemView) {
super(itemView);
mview = itemView;
mview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mClickListener.onItemClick(v, getAdapterPosition());
}
});
}
private PostViewHolder.ClickListener mClickListener;
//Interface to send callbacks...
public interface ClickListener {
public void onItemClick(View view, int position);
public void onItemLongClick(View view, int position);
}
public void setOnClickListener(PostViewHolder.ClickListener clickListener) {
mClickListener = clickListener;
}
public void setFileName(String name) {
TextView textName = (TextView) mview.findViewById(R.id.textfilename);
textName.setText(name);
}
public void setWord(ImageView imageResourses) {
ImageView imageView = (ImageView) mview.findViewById(R.id.imagefilesource);
imageView.setImageResource(R.drawable.word);
}
public void setPDF(ImageView imageResourses) {
ImageView imageView = (ImageView) mview.findViewById(R.id.imagefilesource);
imageView.setImageResource(R.drawable.pdf);
}
public void setimage(ImageView imageResourses) {
ImageView imageView = (ImageView) mview.findViewById(R.id.imagefilesource);
imageView.setImageResource(R.drawable.image);
}
public String setImageURL(String url, PostUtils maa){
url=maa.getImageUrl();
return url;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.logut:
Logut();
startActivity(new Intent(getApplicationContext(), LoginActivity.class));
}
return super.onOptionsItemSelected(item);
}
public void Logut() {
auth.signOut();
finish();
}
}
When you define a recyclerView, one method you should override is onBindViewHolder. Inside of that method is where you define the content of each element of the recyclerView.
So if you want to each row can be clickable, think you need something like this:
#Override
public void onBindViewHolder(PostViewHolder postViewHolder, int i) {
postViewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Definde what happend when the item is clicked
//To identify which row, you can use postViewHolder.getAdapterPosition();
}
});
}
Let me know if works.

Categories

Resources