well im doing a recycler view with a database on firebase . that is working perfectly... now i want to implement a share button but i cant make the button reference each img. i found a tutorial to do so but when i try to implement this line (if i remove it the apps goes well but doest reference to the button position)
public BlogViewHolder(View itemView) {
...
button = (Button)mView.findViewById(R.id.share);
...
the app crashes when i open the activity
im only using toast to testing.
public class NoticiasActivity extends AppCompatActivity {
private RecyclerView mBlogList;
FirebaseDatabase database;
DatabaseReference myRef;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_noticias);
//Recycler View
mBlogList = (RecyclerView)findViewById(R.id.blog_list);
mBlogList.setHasFixedSize(true);
mBlogList.setLayoutManager(new LinearLayoutManager(this));
// Send a Query to the database
database = FirebaseDatabase.getInstance();
myRef = database.getReference("Data");
}
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<ModelClass, BlogViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<ModelClass, BlogViewHolder>(
ModelClass.class,
R.layout.design_row,
BlogViewHolder.class,
myRef) {
#Override
protected void populateViewHolder(BlogViewHolder viewHolder, ModelClass model,int position) {
viewHolder.setTitle(model.getTitle());
viewHolder.setImage(getApplicationContext(), model.getImage());
viewHolder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "esto es un boton"+ view.getNextFocusUpId(), Toast.LENGTH_SHORT).show();
}
});
}
};
mBlogList.setAdapter(firebaseRecyclerAdapter);
}
//View Holder For Recycler View
public static class BlogViewHolder extends RecyclerView.ViewHolder {
View mView;
Button button ;
public BlogViewHolder(View itemView) {
super(itemView);
button = (Button)mView.findViewById(R.id.share);
mView= itemView;
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// click en la imagen hace algo
//añadir luego
Toast.makeText(v.getContext(), "hola", Toast.LENGTH_SHORT).show();
}
});
}
public void setTitle(String title){
TextView post_title = (TextView)mView.findViewById(R.id.titleText);
post_title.setText(title);
}
public void setImage(Context ctx , String image){
ImageView post_image = (ImageView)mView.findViewById(R.id.imageViewy);
// We Need TO pass Context
Picasso.with(ctx).load(image).into(post_image);
}
}
}
You need to switch these lines
button = (Button)mView.findViewById(R.id.share);
mView= itemView;
Or find the button from the itemView
Related
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);
I'm simply implementing a RecyclerView for showing chat between two. Everything is working fine but there is one problem when I am scrolling up and again scrolling down the direction of sender message goes left(GRAVITY) which I have set only for the received ones. Please look the code below hope u will understand.
Screenshot before scrolling.
Screenshot after scrolling.
Here's my RecycleActivity.java
public class RecycleActivity extends AppCompatActivity {
RecyclerView recyclerView;
DatabaseReference databaseReference,temp;
EditText editText;
Button btnSend;
public static String nameOFUser = "";
LinearLayoutManager linearLayoutManager;
SharedPreferences sharedPreferences;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainer);
sharedPreferences = getSharedPreferences("MyLoginPref",MODE_PRIVATE);
nameOFUser = sharedPreferences.getString("name","");
Log.e("name",nameOFUser);
recyclerView = (RecyclerView) findViewById(R.id.recyclerid);
recyclerView.setHasFixedSize(true);
linearLayoutManager= new LinearLayoutManager(this);
linearLayoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(linearLayoutManager);
databaseReference = FirebaseDatabase.getInstance().getReference().child("msg");
editText = (EditText) findViewById(R.id.edittextmessagetosend);
btnSend = (Button) findViewById(R.id.button41);
btnSend.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
temp=databaseReference.push();
temp.child("name").setValue(nameOFUser);
temp.child("message").setValue(editText.getText().toString().trim());
editText.setText("");
recyclerView.setLayoutManager(linearLayoutManager);
}
});
}
#Override
protected void onStart() {
super.onStart();
final FirebaseRecyclerAdapter<MessageBlog,MessageHolder> firebaseRecyclerAdapter
= new FirebaseRecyclerAdapter<MessageBlog, MessageHolder>(
MessageBlog.class,R.layout.message_row,MessageHolder.class,databaseReference
) {
#Override
protected void populateViewHolder(MessageHolder viewHolder, MessageBlog model, int position) {
String name = model.getName();
viewHolder.setSender(name);
viewHolder.SetMessage(model.getMessage());
viewHolder.setDirection(name);
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
recyclerView.smoothScrollToPosition(firebaseRecyclerAdapter.getItemCount());
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
The ViewHolder.java
public static class MessageHolder extends RecyclerView.ViewHolder{
View view;
LinearLayout lay,linearLayoutback;
CardView cardViewreal;
DatabaseReference mdatabase;
public MessageHolder(View itemView) {
super(itemView);
view = itemView;
linearLayoutback = (LinearLayout)
itemView.findViewById(R.id.linearlayoutback);
mdatabase=FirebaseDatabase.getInstance().getReference().child("msg");
lay = (LinearLayout) itemView.findViewById(R.id.idcardview);
cardViewreal = (CardView) itemView.findViewById(R.id.idrealcard);
}
void setDirection(final String name){
if(!name.equalsIgnoreCase(nameOFUser)) {
lay.setGravity(Gravity.LEFT);
cardViewreal.setCardBackgroundColor(Color.parseColor("#ffffff"));
} else {
lay.setGravity(Gravity.LEFT);
cardViewreal.setCardBackgroundColor(Color.parseColor("#f5f4f5"));
}
}
void setSender(String sender){
TextView tvsender = (TextView) view.findViewById(R.id.senderidtv);
tvsender.setText(sender);
}
void SetMessage(String message){
TextView tvmessage = (TextView) view.findViewById(R.id.messageidtv);
tvmessage.setText(message);
}
}
Check your set direction method. It always sets direction to the left
void setDirection(final String name){
if(!name.equalsIgnoreCase(nameOFUser)) {
lay.setGravity(Gravity.LEFT);
cardViewreal.setCardBackgroundColor(Color.parseColor("#ffffff"));
//linearLayoutback.setBackgroundResource(R.drawable.white);
}
else{
lay.setGravity(Gravity.LEFT);
cardViewreal.setCardBackgroundColor(Color.parseColor("#f5f4f5"));
// linearLayoutback.setBackgroundResource(R.drawable.white);
}
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);
}
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.