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);
}
Related
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) {
}
When i upgrade my firebase-ui version to 3.2.2 the firebaserecycleradapter doesn't show any output the code of mine. at first when i got multidex error then i upgrade all library version to lastest version on dependency of app lvl gradle and after upgrade the problem is found the Firebaserecycleradapter method then i search it and write after run the app the list view are not displaying plz help me.
onStart():
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerOptions <Users> options = new FirebaseRecyclerOptions.Builder<Users>()
.setQuery(mUserDatabase,Users.class)
.build();
FirebaseRecyclerAdapter<Users, UsersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(options) {
#NonNull
#Override
public UsersViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.user_list_layout, parent, false);
return new UsersViewHolder(view);
}
#Override
protected void onBindViewHolder(#NonNull UsersViewHolder holder, int position, #NonNull Users model) {
holder.setName(model.getName());
holder.setStatus(model.getStatus());
holder.setRIDNo(model.getRidno());
holder.setUserImage(model.getThumb_image(), getApplicationContext());
//to get key
final String user_id = getRef(position).getKey();
holder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent profileIntent = new Intent(UsersActivity.this, ProfileActivity.class);
profileIntent.putExtra("user_id", user_id);
startActivity(profileIntent);
}
});
}
};
mUsersList.setAdapter(firebaseRecyclerAdapter);
}
and the viewholder is:
//User list viewholder
public static class UsersViewHolder extends RecyclerView.ViewHolder {
static View mView;
private CircleImageView mUserView;
public UsersViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public static void setName(String name){
TextView userNameView = (TextView) mView.findViewById(R.id.user_single_name);
userNameView.setText(name);
}
public static void setStatus(String status){
TextView userStatusView = (TextView) mView.findViewById(R.id.user_single_status);
userStatusView.setText(status);
}
public static void setRIDNo(String ridno){
TextView userRIDNoView = (TextView) mView.findViewById(R.id.user_single_ridno);
userRIDNoView.setText(ridno);
}
public static void setUserImage(String thumb_image, Context ctx){
CircleImageView userImageView = (CircleImageView) mView.findViewById(R.id.user_single_image);
Picasso.with(ctx).load(thumb_image).placeholder(R.drawable.default_avatar).into(userImageView);
}
}
In order to make your code work, please consider using the following steps.
Remove the static from the declaration of your UsersViewHolder class. Should be only:
public class UsersViewHolder extends RecyclerView.ViewHolder {/* ... */}
Remove the static keyword also from the mView field and all your methods. Your holder class should look like this:
public class UsersViewHolder extends RecyclerView.ViewHolder {
private View mView;
private CircleImageView mUserView;
public UsersViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setName(String name){
TextView userNameView = (TextView) mView.findViewById(R.id.user_single_name);
userNameView.setText(name);
}
public void setStatus(String status){
TextView userStatusView = (TextView) mView.findViewById(R.id.user_single_status);
userStatusView.setText(status);
}
public void setRIDNo(String ridno){
TextView userRIDNoView = (TextView) mView.findViewById(R.id.user_single_ridno);
userRIDNoView.setText(ridno);
}
public void setUserImage(String thumb_image, Context ctx){
CircleImageView userImageView = (CircleImageView) mView.findViewById(R.id.user_single_image);
Picasso.with(ctx).load(thumb_image).placeholder(R.drawable.default_avatar).into(userImageView);
}
}
Move all the code from the onStart() method inside onCreate() method except this line of code:
super.onStart();
Make your firebaseRecyclerAdapter varaible global:
private FirebaseRecyclerAdapter<Users, UsersViewHolder> firebaseRecyclerAdapter;
Remove FirebaseRecyclerAdapter<Blog, BlogViewHolder> from the onCreate() method.
Add the following lines of code in the onStart() and onStop() methods.
#Override
protected void onStart() {
super.onStart();
firebaseRecyclerAdapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
if(firebaseRecyclerAdapter != null) {
firebaseRecyclerAdapter.stopListening();
}
}
If you are using authentication, please also don't forget to add this line of code inside onStart() method:
firebaseAuth.addAuthStateListener(authStateListener);
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_image);
I created a simple application which contain RecyclerView TextView and Images loaded from the Firebase.
Now I want to pass the text from TextView to other activity.
The TextView is in RecyclerView:
The code:
FirebaseRecyclerAdapter<Blog,BlogViewHolder> firebaseRecyclerAdapter=new
FirebaseRecyclerAdapter<Blog, BlogViewHolder>(
Blog.class,
R.layout.blog_row,
BlogViewHolder.class,
mDatabase
) {
#Override
protected void populateViewHolder(final BlogViewHolder viewHolder,
Blog model, final int position) {
final String post_keyy=getRef(position).getKey();
viewHolder.setTitle(model.getTitle());
viewHolder.setDesc(model.getDesc());
viewHolder.setImag(getApplicationContext(),model.getImage());
}
};
mBlogList.setAdapter(firebaseRecyclerAdapter);
}
public static class BlogViewHolder extends RecyclerView.ViewHolder
{
View mView;
TextView post_title,post_desc;
ImageView post_imge;
public BlogViewHolder(View itemView) {
super(itemView);
mView=itemView;
}
public void setTitle(String title)
{
post_title=(TextView)mView.findViewById(R.id.posttitle);
post_title.setText(title);
}
public void setDesc(String desc)
{
post_desc=(TextView)mView.findViewById(R.id.post_desc);
post_desc.setText(desc);
}
public void setImag(Context ctx,String image)
{
post_imge=(ImageView) mView.findViewById(R.id.postimg);
Picasso.with(ctx).load(image).into(post_imge);
}
}
Now I want to pass post_title textview text to other activity.
You should try to add the onClickListener in the onBindViewHolder method of your RecyclerAdapter class:
#Override
public void onBindViewHolder(BlogViewHolder holder, int position) {
//Set the OnClickListener on whatever view you want to trigger when clicked.
holder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//You can get the context from the view itself.
Intent intent = new Intent(view.getContext(), theOtherActivityName.class);
//Put your intent extras.
intent.putExtra("blog_id",post_title.getText().toString());
view.getContext().startActivity(intent);
}
});
}
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.