I am trying to show my integers saved in the firebase database in my Recyclerview. I have two Strings I can easily show in my App, but somehow it doesn't work with integers. Even more strange is that when I save a String where before there was an integer I got an error.
Here my database structure
Here I populate my Viewholder
#Override
public void onStart() {
super.onStart();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<ListItem, ListViewHolder>(
ListItem.class,
R.layout.card_item,
ListViewHolder.class,
mDatabase.orderByChild("minusAccandShare").endAt(0)
) {
#Override
protected void populateViewHolder(ListViewHolder viewHolder, ListItem model, final int position) {
viewHolder.setImage(getActivity().getApplicationContext(), model.getImage());
viewHolder.setHeading(model.getHeading());
viewHolder.setStoreName(model.getStoreName());
viewHolder.setAcceptCount(model.getAcceptCount());
viewHolder.mView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent i = new Intent(getActivity(), DetailActivity.class);
Bundle extras = new Bundle();
extras.putString(EXTRA_QUOTE, firebaseRecyclerAdapter.getRef(position).getKey());
i.putExtra(BUNDLE_EXTRAS, extras);
startActivity(i);
}
});
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
My ViewHolder
public static class ListViewHolder extends RecyclerView.ViewHolder {
View mView;
public ListViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setImage(Context ctx, String image){
ImageView postImage = (ImageView) mView.findViewById(R.id.im_item_icon);
Picasso.with(ctx).load(image).resize(200,200).into(postImage);
}
public void setHeading(String heading){
TextView card_heading = (TextView) mView.findViewById(R.id.lbl_item_sub_title);
card_heading.setText(heading);
}
public void setStoreName(String storeName){
TextView card_storeName = (TextView) mView.findViewById(R.id.lbl_item_text);
card_storeName.setText(storeName);
}
public void setAcceptCount(String accepts){
TextView accepts_count = (TextView) mView.findViewById(R.id.lbl_accepts_count);
accepts_count.setText(accepts);
}
}
And finally my model
public class ListItem {
private String heading, storeName, image;
private Long accepts;
public ListItem() {
}
public ListItem(String heading,String storeName, Long accepts, String image){
this.heading = heading;
this.storeName = storeName;
this.image = image;
this.accepts = accepts;
}
public String getHeading() {
return heading;
}
public String getStoreName() {
return storeName;
}
public String getImage() {return image;}
public String getAcceptCount() {
return String.valueOf(accepts);
}
In this way I see null where there should stand 200 instead. I tried everything. I even saved an String instead of an integer and tried to show it the same way as the heading String ( which works perfectly) but then I see a blank place... Please help. Thanks
you should create a getter and setter for accepts in ListItem.
public Long getAccepts() {
return accepts;
}
public void setAccepts(Long accepts) {
this.accepts = accepts;
}
Related
So below are the database of my cloud firestore. That DatePosted shows 2nd December, 2019 at 8.19 pm UTC +8. According to the code I used and I run my app, it shows an unreadable number. Is it correct on how I retrieve the data from cloud firebase? If its wrong, how do I retrieve that timestamp and make it readable?
ForumAdapter.java
public class ForumAdapter extends FirestoreRecyclerAdapter<Forum,ForumAdapter.ForumHolder> {
private OnItemClickListener listener;
public ForumAdapter(FirestoreRecyclerOptions<Forum> options) {
super(options);
}
#Override
public void onBindViewHolder(ForumHolder forumHolder, int i, Forum forum) {
forumHolder.textViewTitle.setText(forum.getTitle());
forumHolder.textViewDescription.setText(forum.getDescription());
forumHolder.timeStamp.setText(forum.getDatePosted().toString());
}
#NonNull
#Override
public ForumHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
android.view.View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardviewforumtitle,parent,false);
return new ForumHolder(v);
}
class ForumHolder extends RecyclerView.ViewHolder{
TextView textViewTitle;
TextView textViewDescription;
TextView timeStamp;
public ForumHolder(View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.tvTitle);
textViewDescription = itemView.findViewById(R.id.tvDescription);
timeStamp = itemView.findViewById(R.id.tvTimestamp);
textViewTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = getAdapterPosition();
// NO_POSITION to prevent app crash when click -1 index
if(position != RecyclerView.NO_POSITION && listener !=null ){
listener.onItemClick(getSnapshots().getSnapshot(position),position);
}
}
});
}
}
public interface OnItemClickListener{
void onItemClick(DocumentSnapshot documentSnapshot, int position);
}
public void setOnItemClickListener(OnItemClickListener listener){
this.listener = listener;
}
#Override
public int getItemCount() {
return super.getItemCount();
}
}
Forum.java
public class Forum {
private String Title;
private String Description;
private String User;
private com.google.firebase.Timestamp DatePosted;
public Forum() {
}
public Forum(String title, String description, Timestamp datePosted,String user) {
Title = title;
Description = description;
DatePosted = datePosted;
User = user;
}
public String getUser() {
return User;
}
public void setUser(String user) {
User = user;
}
public String getTitle() {
return Title;
}
public void setTitle(String title) {
Title = title;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
public Timestamp getDatePosted() {
return DatePosted;
}
public void setDatePosted(Timestamp datePosted) {
DatePosted = datePosted;
}
}
When you query a document with a timestamp field, you will get a Timestamp object back. If you'd rather have a Date object, you can use its toDate method.
You will need to write some code to format this for display. This is a very common task for mobile and web apps.
I have an online quiz app in Firebase.
If my phone is in English, the app works fine, but when it change into Turkish, Piccaso does not load image.Please help me I can not find a solution for over a week
In this case my phone is in English
In this cas my phone is in Turkish
private void loadCategories() {
adapter = new FirebaseRecyclerAdapter<Category, CategoryViewHolder>(Category.class,
R.layout.category_layout,
CategoryViewHolder.class,
categories) {
#Override
protected void populateViewHolder(CategoryViewHolder viewHolder, final Category model, int position) {
viewHolder.category_name.setText(model.getName());
Picasso.get().load(model.getImage()).into(viewHolder.category_image);
viewHolder.setItemClickListener(new ItemClickListener() {
#Override
public void onClick(View view, final int position, boolean isLongClick) {
{
Intent play = new Intent(getActivity(), StartActivity.class);
Common.categoryId = adapter.getRef(position).getKey();
Common.categoryName = model.getName();
startActivity(play);
}
}
});
}
};
adapter.notifyDataSetChanged();
listCategory.setAdapter(adapter);
}
View Holder
public class CategoryViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
View mV;
public TextView category_name;
public ImageView category_image,gradient2;
public Button btnPlay;
private ItemClickListener itemClickListener;
public CategoryViewHolder(View itemView) {
super(itemView);
mV = itemView;
category_image = (ImageView)itemView.findViewById(R.id.category_image);
category_name = (TextView)itemView.findViewById(R.id.category_name);
gradient2 = (ImageView)itemView.findViewById(R.id.gradient2);
btnPlay = (Button)itemView.findViewById(R.id.btn_play);
btnPlay.setTag(R.id.btn_play,itemView);
btnPlay.setOnClickListener(this);
itemView.setOnClickListener(this);
}
public void setItemClickListener(ItemClickListener itemClickListener) {
this.itemClickListener = itemClickListener;
}
#Override
public void onClick(View v) {
itemClickListener.onClick(v,getAdapterPosition(),false);
}
}
Catgeory Model.java
public class Category {
private String Name;
private String Image;
private String Button;
public Category() {
}
public Category(String name, String image, String button) {
this.Name = name;
this.Image = image;
this.Button = button;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getImage() {
return Image;
}
public void setImage(String image) {
Image = image;
}
public String getButton() {
return Button;
}
public void setButton(String button) {
Button = button;
}
}
I find solution.I changed catgegory model and firabse json file.Maybe in category model Name And İmage translated into Turkish when changed phone language.
you model should be the same firebase json file. I changed model and jason file then app works fine.
Json File
"Category" : {
"01" : {
"ad" : "Azərbaycan",
"sekil" : "https://avatanplus.com/files/resources/mid/5969ff2ae80a415d460cbfc6.jpg"
},
"02" : {
"ad" : "Türkiyə",
"sekil" : "https://img00.deviantart.net/6f00/i/2012/238/9/0/turkey_flag_grunge_hd_2_0_by_syndikata_np-d5che5q.jpg"
},
"03" : {
"ad" : "Viner",
"sekil" : "https://img.milli.az/2017/12/08/606172.jpg"
}
Category Model
public class Category {
private String ad;
private String sekil;
private String Button;
public Category() {
}
public Category(String ad, String sekil, String button) {
this.ad = ad;
this.sekil = sekil;
Button = button;
}
public String getAd() {
return ad;
}
public void setAd(String ad) {
this.ad = ad;
}
public String getSekil() {
return sekil;
}
public void setSekil(String sekil) {
this.sekil = sekil;
}
public String getButton() {
return Button;
}
public void setButton(String button) {
Button = button;
}
}
I find solution.I changed catgegory model and firabse json file.Maybe in category model Name And İmage translated into Turkish when changed phone language.
I can get images and titles, texts in my post_row layout from my Firebase Server.
I want to click each items in the Recyclerview and I searched on them on google. but I couldn't find suitable codes implementing the click event in FirebaseRecyclerView.
and This is my FirebaseRecycler Adapter code!
thank you ~~!!
mDatabase = FirebaseDatabase.getInstance().getReference().child("post");
#Override
protected void onStart() {
super.onStart();
final Query DBquery = FirebaseDatabase.getInstance().getReference().child("post").orderByChild("count");
FirebaseRecyclerAdapter<Post, PostViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Post, PostViewHolder>(
Post.class,
R.layout.post_row,
PostViewHolder.class,
DBquery
mDatabase
) {
#Override
protected void populateViewHolder(PostViewHolder viewHolder, Post model, int position) {
viewHolder.setTitle(model.getTitle());
viewHolder.setDesc(model.getDesc());
viewHolder.setImage(getApplicationContext(), model.getImage());
viewHolder.setDate(model.getDate());
}
};
mPostList.setAdapter(firebaseRecyclerAdapter);
}
public static class PostViewHolder extends RecyclerView.ViewHolder {
View mView;
public PostViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setTitle(String title){
TextView post_title = mView.findViewById(R.id.post_title);
post_title.setText(title);
}
public void setDesc(String desc){
TextView post_desc = mView.findViewById(R.id.post_desc);
post_desc.setText(desc);
}
public void setImage(Context ctx, String image){
ImageView post_image = mView.findViewById(R.id.post_image);
Picasso.get().load(image).into(post_image);
}
public void setDate(String date){
TextView post_date = mView.findViewById(R.id.post_date);
post_date.setText(date);
}
}
`
Use a listview to show your data, I use it like this and it's working really good.
Just inside populateViewHolder
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
mDatabase = mAdapter.getRef(position);
Intent intent = new Intent(MainActivity.this, UserEdit.class);
intent.putExtra("uid",mDatabase.getKey());
intent.putExtra("name", mAdapter.getItem(position).getNombre());
intent.putExtra("Email", mAdapter.getItem(position).getEmail());
intent.putExtra("paid", mAdapter.getItem(position).getPago());
intent.putExtra("connection", mAdapter.getItem(position).getUConexion());
intent.putExtra("connection2", mAdapter.getItem(position).getPConexion());
startActivity(intent);
}
});
mListView.setAdapter(mAdapter);
This is just an example from my code, it just clicks the list, get the item and pass it to another activity to work with that data.
With this you are setting the adapter data into a listview, and then clicking each item gives you the data of that position and you can manage that data in another activity if you need it.
The getters I'm using at the putExtras are being passed by the POJO class to the FirebaseList.
FirebaseListOptions<Usuarios> options = new FirebaseListOptions.Builder<Usuarios>()
.setQuery(query, Usuarios.class)
.setLayout(R.layout.item_row)
.build();
Here Usuarios is this:
private String Nombre;
private String Dispositivo;
private String sexo;
private String lenguaje;
private String email;
public Usuarios(){}
public Usuarios(String nombre, String dispositivo, String sexo, String lenguaje, String email, String PConexion, String UConexion, String URL_frases, String URL_grupos, String URL_pictos, String edad, String pago) {
Nombre = nombre;
Dispositivo = dispositivo;
this.sexo = sexo;
this.lenguaje = lenguaje;
this.email = email;
....
}
public String getNombre() {
return Nombre;
}
public void setNombre(String nombre) {
Nombre = nombre;
}
public String getDispositivo() {
return Dispositivo;
}
....
Remember, here the variables nombre, email and so one need to be declared exactly as the Firebase database ones, otherwise you will see blank and results won't display, and remember in your onStart to place mAdapter.startListening();.
Add a function in your PostViewHolder say
public void init(int position){
itemView.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View view) {
//Handle your click here
}
});
}
Now from populateViewHolder call
viewHolder.init(position);
To solve this, in your PostViewHolder class attach a click listener on your view like this:
mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Do what you need to do
}
});
If you want to add a click listener on each view, then just find the views inside the mView object and attach the listener on each one of them. That's it!
Make ImageView Object outside the function as you did for mView .
And then in the populateViewHolder just add
viewHolder.imageView.setOnClickListener()
It'll work fine Guaranteed
everyone.
Hello. I’ve created android application. I am retrieving data from real-time database and also using recycle view + view holder. But when I go to another activity and then go back - my list oh view holder items was reset. How I can save state view holder and restore them?
I looked at various topics on the site, but not one answer did not help me.
My Pojo Class
public class Post {
private String post_id;
private String post_title;
private long post_date;
private long post_desc;
public Post(String post_id, String post_title, long post_date, String post_desc) {
this.post_id = post_id;
this.post_title = post_title;
this.comments_count = comments_count;
this.post_date = post_date;
this.post_desc = post_desc;
}
public void setPost_id(String post_id) {
this.post_id = post_id;
}
public String getPost_title() {
return post_title;
}
public void setPost_title(String post_title) {
this.post_title = post_title;
}
public String getPost_date() {
String month = new java.text.SimpleDateFormat("MM").
format(new java.util.Date(post_date * 1000));
int monthnumber = Integer.parseInt(month);
String value = new java.text.SimpleDateFormat("dd ").
format(new java.util.Date(post_date * 1000));
value +=MonthName[monthnumber-1];
value+= new java.text.SimpleDateFormat(" HH:mm").
format(new java.util.Date(post_date * 1000));
return value;
}
public void setPost_date(long post_date) {
this.post_date = post_date;
}
public String getPost_desc() {
return post_desc;
}
public void setPost_desc(String post_desc) {
this.post_desc = post_desc;
}}
ViewHolderClass
public static class PostViewHolder extends RecyclerView.ViewHolder {
public PostViewHolder(View itemView) {
super(itemView); }
public void setTitle(String title) {
titleViewPost.setText(title);
}
public void setDate(String date) {
dateViewPost.setText(date);
}
public void setDesc(String desc) {
descViewPost.setText(desc);
}}}
FirebaseRecyleAdapter
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Post, PostViewHolder >(
Post.class,
R.layout.post_row,
PostViewHolder.class,
mDatabase
) {#Override
protected void populateViewHolder(final PostViewHolder viewHolder,
Post model, final int position) {
viewHolder.mCommentButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent commentActivity = new Intent(MainActivity.this, CommentActivity.class);
commentActivity.putExtra("post_id", post_key);
if(mUserId != null) {
commentActivity.putExtra("user_id", mUserId);
}
startActivity(commentActivity);
}
});
As I understand, the whole problem is that my adapter is reused and I can not return to that position in the tape - where I was before.
Its my app.https://play.google.com/store/apps/details?id=dev.arounda.chesnock
I want fix thiw trouble for this application
Try to call firebaseRecyclerAdapter.startListening(); in onCreate() instead of onStart and firebaseRecyclerAdapter.stopListening(); in onDestroy() instead of onStop()
In my App I developed a News feature with RecaylerView widget. Now I want to pass CoverImage and Date from this recyclerView to detail page of news. I can pass date of the news successfully. But the cover image is not shown in the detail page. I have found 0 image in my logcat in android monitor window. I cannot not identify what would be the problem here.
My Model class of news is-
public class NewsModel extends RealmObject {
#PrimaryKey
private int image;
private String title;
private String date;
private String detail ;
public NewsModel() {
}
public NewsModel(int image, String title, String date) {
this.image = image;
this.title = title;
this.date = date;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
......
}
My adaper class for news page is
public class NewsAdapter extends
RecyclerView.Adapter<NewsAdapter.NewsHolder> {
private List<NewsModel> newsObject;
private Context context;
public NewsAdapter(Context context, List<NewsModel> newsObject) {
this.context = context;
this.newsObject=newsObject;
}
#Override
public NewsHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.news_row_layout,parent,false);
return new NewsHolder(view);
}
#Override
public void onBindViewHolder(NewsAdapter.NewsHolder holder, int position) {
final NewsModel newsModel=newsObject.get(position);
holder.newsImage.setImageResource(newsModel.getImage());
holder.newsHeadline.setText(newsModel.getTitle());
holder.newsDate.setText(newsModel.getDate());
holder.cardView.setTag(position);
holder.cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int pos = (int)view.getTag();
openDetailNews(Integer.toString(newsModel.getImage()),newsModel.getDate());
}
});
}
//open Detail News Page
private void openDetailNews(String...details) {
Intent i=new Intent(context,DetailNews.class);
i.putExtra("image",details[0]);
i.putExtra("date",details[1]);
context.startActivity(i);
}
#Override
public int getItemCount() {
return newsObject.size();
}
public class NewsHolder extends RecyclerView.ViewHolder {
public CardView cardView;
public ImageView newsImage;
public TextView newsHeadline;
public TextView newsDate;
public NewsHolder(View itemView) {
super(itemView);
newsImage=(ImageView)itemView.findViewById(R.id.news_picture);
newsHeadline=(TextView)itemView.findViewById(R.id.news_headline);
newsDate=(TextView) itemView.findViewById(R.id.news_date);
cardView=(CardView)itemView.findViewById(R.id.cardview_news);
}
}
}
The detail news page code where I want to get the cover image and date from news page is-
public class DetailNews extends AppCompatActivity {
private RecyclerView recyclerView;
private RecyclerView.Adapter adapter;
private List<NewsImageModel> newsObject;
ImageView _coverImage;
TextView _description;
TextView _newsDate;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.news_detail);
// Showing and Enabling clicks on the Home/Up button
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
newsObject=getAllImageList();
// setting up views and stuff
setUpUIViews();
Intent intent = getIntent();
//RECEIVE DATA
Log.e("_coverImage",""+_coverImage);
Integer coverImage=intent.getExtras().getInt("image");
Log.e("coverImage",""+coverImage);
String newsDate=intent.getExtras().getString("date");
//BIND DATA
_coverImage.setImageResource(coverImage);
Log.e("coverImage",""+coverImage);
_newsDate.setText(newsDate);
}
private void setUpUIViews() {
_coverImage=(ImageView)findViewById(R.id.news_cover);
_description=(TextView)findViewById(R.id.news_description);
_newsDate=(TextView)findViewById(R.id.news_date);
recyclerView = (RecyclerView)findViewById(R.id.image_list);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(DetailNews.this);
recyclerView.setLayoutManager(layoutManager);
adapter = new NewsDetailAdapter(this,newsObject );
recyclerView.setAdapter(adapter);
}
private List<NewsImageModel> getAllImageList() {
List<NewsImageModel> images = new ArrayList<NewsImageModel>();
......
return images;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
...
}
}
Use this :
public void onClick(View view) {
int pos = (int)view.getTag();
Intent i=new Intent(context,DetailNews.class);
i.putExtra("image",newsModel.getImage());
i.putExtra("date",newsModel.getDate());
context.startActivity(i);
}
and then in your activity:
Intent intent = getIntent();
int imageId = intent.getIntExtra("image",0);
then use this resource id to set your image
_coverImage.setImageResource(imageId);
Try it!
String coverImage=intent.getExtras().getString("image");
int convertCoverImage = Integer.valueOf("coverImage");
_coverImage.setImageResource(convertCoverImage);
You can't directly show a string url or any integer type into a imageview . You need to add Image lib's like Picaso or glide.
follow below steps:
1.Add dependencies:
compile 'com.squareup.picasso:picasso:2.5.2'
2.In your adapter insted of your old code replace with
Picasso.with(context).load(newsModel.getImage()).into(holder.newsImage);
Your problem will be solved.
private int image;
What integer data you are setting for the image? Is it a resource image?