setItemOnclickListener not working in FirebaseRecyclerAdapter - android

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) {
}

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);
}
}
}
}
}

Android Implement Play and Download Buttons to Download MP3 files from Firebase Storage using URL's stored in JSON and Uploaded to Firebase Database

I'm creating a android app with Recylerview to retrieve list of mp3 files with play and download buttons.
Below is my JSON file :
From the above Json file Image, i want to retrieve those audioLink's in recycler view. I want to play them using Mediaplayer along with a seek bar and also Download them to mobile storage, i don't know how to implement them.Please Help me to code
Here is my Screenshot of a audio item in recyclerview
Here is my Model Class of Audio Items
public class AudioItem {
public String imageLink;
public String categoryId;
public String number;
public long viewCount;
public String audioTitle;
public AudioItem() {
}
public AudioItem(String imageLink, String categoryId, String audioTitle) {
this.imageLink = imageLink;
this.categoryId = categoryId;
this.audioTitle = audioTitle;
}
public String getAudioTitle() {
return audioTitle;
}
public void setAudioTitle(String audioTitle) {
this.audioTitle = audioTitle;
}
public AudioItem(String number) {
this.number = number;
}
public String getImageLink() {
return imageLink;
}
public void setImageLink(String imageLink) {
this.imageLink = imageLink;
}
public String getCategoryId() {
return categoryId;
}
public void setCategoryId(String categoryId) {
this.categoryId = categoryId;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public long getViewCount() {
return viewCount;
}
public void setViewCount(long viewCount) {
this.viewCount = viewCount;
}
}
Here is my List of mp3's AudioItemViewHolder Class
public class ListBRViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
ItemClickListener itemClickListener;
public TextView wallpaperVcount,wallpaperVcountlinear,AudioTitle;
public ImageButton play,download;
public void setItemClickListener(ItemClickListener itemClickListener)
{
this.itemClickListener = itemClickListener;
}
public ListBRViewHolder(View itemView) {
super(itemView);
wallpaperVcount=(TextView)itemView.findViewById(R.id.viewCount);
AudioTitle=(TextView)itemView.findViewById(R.id.brtitle);
download=(ImageButton)itemView.findViewById(R.id.btn_download) ;
play=(ImageButton)itemView.findViewById(R.id.btn_play) ;
wallpaperVcountlinear(TextView)itemView.findViewById(R.id.viewCountLinear);
itemView.setOnClickListener(this);
}
Here is my AudioListView Class
public class NewBRList extends AppCompatActivity {
Query query;
FirebaseRecyclerOptions<AudioItem> options;
FirebaseRecyclerAdapter<AudioItem,ListBRViewHolder> adapter;
RecyclerView recyclerView;
AdView mAdview;
RelativeLayout relativeLayout;
//Room Database
CompositeDisposable compositeDisposable;
RecentsRepository recentsRepository;
FavouritesRepository favouritesRepository;
ImageView image_category;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_newbr_list);
Toolbar toolbar=(Toolbar)findViewById(R.id.toolbar);
toolbar.setTitle(Common.CATEGORY_SELECTED);
setSupportActionBar(toolbar);
relativeLayout=(RelativeLayout)findViewById(R.id.listviewlayoutnewbr) ;
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
image_category=(ImageView)findViewById(R.id.image_newbr);
recyclerView=(RecyclerView)findViewById(R.id.recycler_newbr_list);
recyclerView.setHasFixedSize(true);
recyclerView.setItemViewCacheSize(20);
recyclerView.setDrawingCacheEnabled(true);
recyclerView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
linearLayoutManager.setStackFromEnd(true);
linearLayoutManager.setReverseLayout(true);
recyclerView.setLayoutManager(linearLayoutManager);
loadBackgroundList();
}
private void loadBackgroundList() {
query= FirebaseDatabase.getInstance().getReference(Common.STR_WALLPAPER)
.orderByChild("categoryId").equalTo(Common.CATEGORY_ID_SELECTED);
options=new FirebaseRecyclerOptions.Builder<AudioItem>()
.setQuery(query,AudioItem.class)
.build();
adapter=new FirebaseRecyclerAdapter<AudioItem, ListBRViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull final ListBRViewHolder holder, int position, #NonNull final AudioItem model) {
holder.AudioTitle.setText(model.getAudioTitle());
holder.wallpaperVcountlinear.setText("Views : "+ model.getViewCount());
holder.download.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(NewBRList.this,"Download Test",Toast.LENGTH_SHORT).show();
}
});
holder.play.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(NewBRList.this,"Play Test",Toast.LENGTH_SHORT).show();
}
});
}
#Override
public ListBRViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView= LayoutInflater.from(parent.getContext())
.inflate(R.layout.layout_br_item_linear,parent,false);
int height=parent.getMeasuredHeight()/2;
itemView.setMinimumHeight(height);
return new ListBRViewHolder(itemView);
}
};
adapter.startListening();
recyclerView.setAdapter(adapter);
}
Please Kindly Help me :)
Thank you.

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);
}

How to Pass TextView text from Recyclerview to other Activity in Android

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);
}
});
}

Android Using View Holder

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.

Categories

Resources