FirebaseRecyclerAdapter not showing data in Fragment - android

I've a Fragment where I'm trying to show the ListView using FirebaseRecyclerAdapter which comes in compile 'com.firebaseui:firebase-ui:0.4.2'.
My Fragment code goes like this :
public class HomeFragment extends Fragment {
private RecyclerView services_list;
private DatabaseReference mDatabase;
public HomeFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
// Inflate the layout for this fragment
return view;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mDatabase = FirebaseDatabase.getInstance().getReference().child("AllServices");
mDatabase.keepSynced(true);
services_list = (RecyclerView) view.findViewById(R.id.services_list);
services_list.setHasFixedSize(true);
services_list.setLayoutManager(new LinearLayoutManager(getContext()));
}
#Override
public void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Services, ServiceViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Services, ServiceViewHolder>(
Services.class,
R.layout.service_row,
ServiceViewHolder.class,
mDatabase
) {
#Override
protected void populateViewHolder(ServiceViewHolder viewHolder, Services model, final int position) {
viewHolder.setServiceName(model.getName());
viewHolder.setServicePrice(model.getPrice());
viewHolder.setImage(getContext(), model.getImageUrl());
viewHolder.service_name_textview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getContext(), "Clicked on Service Name "+(position+1), Toast.LENGTH_SHORT).show();
}
});
viewHolder.service_price_text_view.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getContext(), "Clicked on Service Price "+(position+1), Toast.LENGTH_SHORT).show();
}
});
viewHolder.service_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getContext(), "Clicked on Service Image "+(position+1), Toast.LENGTH_SHORT).show();
}
});
}
};
services_list.setAdapter(firebaseRecyclerAdapter);
}
public static class ServiceViewHolder extends RecyclerView.ViewHolder
{
View mView;
TextView service_name_textview;
TextView service_price_text_view;
ImageView service_image;
public ServiceViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setServiceName(String title)
{
service_name_textview = (TextView) mView.findViewById(R.id.service_name_textview);
service_name_textview.setText(title);
}
public void setServicePrice(String desc)
{
service_price_text_view = (TextView) mView.findViewById(R.id.service_price_text_view);
service_price_text_view.setText(desc);
}
public void setImage(final Context ctx, final String image)
{
service_image = (ImageView) mView.findViewById(R.id.service_image);
//Picasso.with(ctx).load(image).into(post_image);
Picasso.with(ctx).load(image).networkPolicy(NetworkPolicy.OFFLINE).into(service_image, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
Picasso.with(ctx).load(image).into(service_image);
}
});
}
}
}
My Services.java code goes like this :
public class Services {
private String Name;
private String ImageUrl;
private String Price;
public Services() {
}
public Services(String name, String imageUrl, String price) {
Name = name;
ImageUrl = imageUrl;
Price = price;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getImageUrl() {
return ImageUrl;
}
public void setImageUrl(String imageUrl) {
ImageUrl = imageUrl;
}
public String getPrice() {
return Price;
}
public void setPrice(String price) {
Price = price;
}
}
There are 21 rows to be displayed into the RecyclerView.
NOTE : once when I installed app for the first time then the data get loaded into the recyclerview (and in the logcat there was images link from where the images were getting downloaded) but after that the data never loaded into the app.
I'm really trying hard to get it sorted but nothing seems to help.

Same problem occurred me. Simply remove the service_list.setHasFixedSize(true).

Sometimes there is an issue in sizing of Recycleview
I also had the same problem that I fixed it with setting
android:layout_width="match_parent"
android:layout_height="match_parent"
just set height and width to match_parent

Related

Unable to Get Images to Display in Recycler View- Text is Sucessfully Displayed

Please see the following issue:
All User Profile Images are not successfully displaying in the RecyclerView.
Text view is successfully displaying in the RecyclerView.
I looked up other similar issues online, but have not been able to find a solution.
I have updated my google play services, and my Firebase storage dependencies.
I am able to successfully pull the current user profile image in another activity.
I am getting the following 404 error message below:
**Adapter**
public class FindFriendsAdapter extends RecyclerView.Adapter<FindFriendsAdapter.FindFriendsViewHolder>
{
private Context context;
private List<FindFriendModel> findFriendModelList;
////This is a constructor and is a must have for recyclerviews/////
public FindFriendsAdapter(Context context, List<FindFriendModel> findFriendModelList) {
this.context = context;
this.findFriendModelList = findFriendModelList;
}
////This is a constructor and is a must have for recyclerviews/////
#NonNull
#Override
public FindFriendsAdapter.FindFriendsViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.find_friends_layout, parent, false);
return new FindFriendsViewHolder(view); ///// layout converts our xml layout file to a programmable file some kind of way.
}
#Override
public void onBindViewHolder(#NonNull FindFriendsAdapter.FindFriendsViewHolder holder, int position) {
final FindFriendModel friendModel = findFriendModelList.get(position);
holder.text_view_full_name.setText(friendModel.getFull_name());
StorageReference fileref = FirebaseStorage.getInstance().getReference().child(Constants.IMAGES_FOLDER +"/" + friendModel.getProfileimage());
fileref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
Glide.with(context)
.load(uri)
.placeholder(R.drawable.small_grey_circle)
.error(R.drawable.small_grey_circle)
.into(holder.imageView_profile);
}
});
}
#Override
public int getItemCount() {
return findFriendModelList.size();
}
public class FindFriendsViewHolder extends RecyclerView.ViewHolder{
private CircleImageView imageView_profile;
private TextView text_view_full_name;
private ImageButton button_request, button_cancel_request;
public FindFriendsViewHolder(#NonNull View itemView) {
super(itemView);
imageView_profile = itemView.findViewById(R.id.find_friends_profile_picture);
text_view_full_name = itemView.findViewById(R.id.find_friends_user_full_name);
button_request = itemView.findViewById(R.id.button_send_requests);
button_cancel_request = itemView.findViewById(R.id.button_cancel_requests);
}
}
}
**Model Class**
public class FindFriendModel
{
private String full_name;
private String profileimage;
private String userID;
private boolean requestSent;
public FindFriendModel(String full_name, String profileimage, String userID, boolean requestSent) {
this.full_name= full_name;
this.profileimage = profileimage;
this.userID = userID;
this.requestSent = requestSent;
}
public FindFriendModel() {}
public String getFull_name() {
return full_name;
}
public void setFull_name(String full_name) {
this.full_name = full_name;
}
public String getProfileimage() {
return profileimage;
}
public void setProfileimage(String profileimage) {
this.profileimage = profileimage;
}
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
public boolean isRequestSent() {
return requestSent;
}
public void setRequestSent(boolean requestSent) {
this.requestSent = requestSent;
}
}
**Fragment Java Class**
public class FindFriendsFragment extends Fragment {
private RecyclerView recycler_view_find_friends;
private FindFriendsAdapter findFriendsAdapter;
private List<FindFriendModel> findFriendModelList;
private TextView text_view_empty_Friends_List;
private DatabaseReference databaseReference;
private FirebaseUser currentUser;
public FindFriendsFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_find_friends, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
recycler_view_find_friends = view.findViewById(R.id.recycler_view_find_friends);
text_view_empty_Friends_List = view.findViewById(R.id.text_view_empty_find_friends);
recycler_view_find_friends.setLayoutManager(new LinearLayoutManager(getActivity()));
findFriendModelList = new ArrayList<>();
findFriendsAdapter = new FindFriendsAdapter(getActivity(), findFriendModelList);
recycler_view_find_friends.setAdapter(findFriendsAdapter);
databaseReference = FirebaseDatabase.getInstance().getReference().child("Users");
currentUser = FirebaseAuth.getInstance().getCurrentUser();
text_view_empty_Friends_List.setVisibility(View.VISIBLE);
Query query = databaseReference.orderByChild("full_name");
query.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
findFriendModelList.clear();
for (final DataSnapshot ds : dataSnapshot.getChildren())
{
final String userID = ds.getKey();
if (userID.equals(currentUser.getUid()))
return;
if (ds.child("full_name").getValue()!=null)
{
String fullName = ds.child("full_name").getValue().toString();
String profileImage = ds.child("profileimage").getValue().toString();
findFriendModelList.add(new FindFriendModel(fullName, profileImage, userID, false));
findFriendsAdapter.notifyDataSetChanged();
text_view_empty_Friends_List.setVisibility(View.GONE);
}
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(getContext(),"Failed to fetch friends", Toast.LENGTH_SHORT).show();
}
});
}
}
````
ds.child("full_name").getValue()!=null should be replaced by
Objects.equals(ds.child("full_name"), null)
It worked for me when I had the same error, maybe it will help you as well !
Figured out the issue:
The issue was that I was already saving my images images to Firebase Storage, and I was trying to get the download url string again.
I fixed the following part of my adapter:
#Override
public void onBindViewHolder(#NonNull FindFriendsAdapter.FindFriendsViewHolder holder, int position) {
final FindFriendModel friendModel = findFriendModelList.get(position);
holder.text_view_full_name.setText(friendModel.getFull_name());
Glide.with(context)
.load(friendModel.getProfileimage())
.placeholder(R.drawable.small_grey_circle)
.error(R.drawable.small_grey_circle)
.into(holder.imageView_profile);
}

how to remove item from Recyclerview

friends...
In my project, I am creating a list of images from the server and showing in Recyclerview.
now I want to remove a particular item from the list when I click on the verify button.
I tried to hide holder.itemview but its see again when I scroll.
I just want to remove or hide that item which is verified once.
here is my code :
1) Main activity
public class MainActivity extends AppCompatActivity {
private static final String url = "www.mysite.com/api/api-id-list.php?action=imgs";
private ProgressDialog mDetailProgress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_main_activity);
LoadRecyclerView();
}
private void LoadRecyclerView() {
mDetailProgress = new ProgressDialog(this);
mDetailProgress.setTitle("Loading images");
mDetailProgress.setMessage("Please Wait...");
mDetailProgress.setCanceledOnTouchOutside(false);
mDetailProgress.show();
final RecyclerView imgList = (RecyclerView) findViewById(R.id.imgList);
imgList.setLayoutManager(new LinearLayoutManager(this));
StringRequest request = new StringRequest(url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
Image[] images = gson.fromJson(response, Image[].class);
imgList.setAdapter(new ImageAdapter(MainActivity.this, images));
mDetailProgress.dismiss();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getBaseContext(), "Something went wrong..!", Toast.LENGTH_LONG);
}
});
RequestQueue queue = Volley.newRequestQueue(this);
queue.add(request);
}}
2) ImageAdapter
public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ImageViewHolder> {
private ProgressDialog mDetailProgress;
private Context context;
private Image[] data;
Button btn_ban, btn_verify;
private View view;
public ImageAdapter (Context context, Image[] data) {
this.context = context;
this.data = data;
}
#NonNull
#Override
public ImageViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int i) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
view = inflater.inflate(R.layout.single_item, parent, false);
return new ImageViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull final ImageViewHolder holder, final int position) {
final Image imagelist = data[position];
holder.userCode.setText(imagelist.getCode());
Glide.with(holder.userImage.getContext()).load(imagelist.getIdPhoto()).into(holder.userImage);
btn_verify.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
holder.itemView.setVisibility(View.GONE);
}
});
}
#Override
public int getItemCount() { return data.length; }
public class ImageViewHolder extends RecyclerView.ViewHolder {
TextView userCode;
ImageView userImage;
public ImageViewHolder(#NonNull View itemView) {
super(itemView);
userImage = (ImageView) itemView.findViewById(R.id.card_iv_img);
userCode = (TextView) itemView.findViewById(R.id.card_tv_code);
btn_ban = (Button) itemView.findViewById(R.id.btn_ban);
btn_verify = (Button) itemView.findViewById(R.id.btn_verify);
}
}}
3) Image.java
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
public class Image {
#SerializedName("per_ID")
#Expose
private String perID;
#SerializedName("code")
#Expose
private String code;
#SerializedName("first_nm")
#Expose
private String firstNm;
#SerializedName("last_nm")
#Expose
private String lastNm;
#SerializedName("photo_ID")
#Expose
private String photoID;
#SerializedName("id_photo")
#Expose
private String idPhoto;
public String getPerID() {
return perID;
}
public void setPerID(String perID) {
this.perID = perID;
}
public Image withPerID(String perID) {
this.perID = perID;
return this;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Image withCode(String code) {
this.code = code;
return this;
}
public String getFirstNm() {
return firstNm;
}
public void setFirstNm(String firstNm) {
this.firstNm = firstNm;
}
public Image withFirstNm(String firstNm) {
this.firstNm = firstNm;
return this;
}
public String getLastNm() {
return lastNm;
}
public void setLastNm(String lastNm) {
this.lastNm = lastNm;
}
public Image withLastNm(String lastNm) {
this.lastNm = lastNm;
return this;
}
public String getPhotoID() {
return photoID;
}
public void setPhotoID(String photoID) {
this.photoID = photoID;
}
public Image withPhotoID(String photoID) {
this.photoID = photoID;
return this;
}
public String getIdPhoto() {
return idPhoto;
}
public void setIdPhoto(String idPhoto) {
this.idPhoto = idPhoto;
}
public Image withIdPhoto(String idPhoto) {
this.idPhoto = idPhoto;
return this;
}}
First make data not array, but List, so it's easy to remove item;
Secondly set listener for verify button inside ViewHolder and inside OnClick remove item and notify adapter.
btn_verify.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = getAdapterPosition();
data.remove(position);
notifyItemRemoved(position);
}
});
Note: Setting OnClickListener inside ViewHolder makes sure that only correct item is removed by using getAdapterPosition() to get correct position.
Position provided by onBindViewHolder might be invalid after new items are inserted.

Firebase data to RecyclerView shows only one item

I'm retrieving data from firebase and trying to display that in recycler view, every thing is fine but it only shows one item in the list (there are total 14 items)
comment if you do not understand the question
This is my main activity where i get datasnapshots from firebase
public class cakes_activity extends AppCompatActivity {
RecyclerView recyclerView;
ArrayList<Data> listdata;
private DatabaseReference mref;
private NorthAdaptor north;
private final Context context = this;
public int counter = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cakes);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
EditText editText = (EditText) findViewById(R.id.editText) ;
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//FIREBASE
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mref= FirebaseDatabase.getInstance().getReference("Cakes");
mref.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Data data = dataSnapshot.getValue(Data.class);
listdata = new ArrayList<>();
listdata.add(data);
north = new NorthAdaptor(context,listdata);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.hasFixedSize();
recyclerView.setAdapter(north);
}
#Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
#Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
and the following code is my adaptor class:
public class NorthAdaptor extends RecyclerView.Adapter<NorthAdaptor.NorthHolder> {
private LayoutInflater inflater;
ArrayList<Data> northy;
public NorthAdaptor(Context context,ArrayList<Data> northy)
{
inflater=LayoutInflater.from(context);
this.northy=northy;
}
public NorthHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.each_item,parent,false);
NorthHolder holder = new NorthHolder(view);
return holder;
}
#Override
public void onBindViewHolder(NorthHolder holder, int position) {
holder.text2.setText(northy.get(position).getName());
holder.text3.setText("₹."+northy.get(position).getRS());
}
#Override
public int getItemCount() {
return northy.size();
}
class NorthHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
TextView text,text2,text3;
ImageView img;
private final Context context;
public NorthHolder(View itemview)
{
super(itemview);
context = itemView.getContext();
CardView card = (CardView) itemView.findViewById(R.id.card);
text3=(TextView)itemView.findViewById(R.id.Rs);
text2 = (TextView) itemView.findViewById(R.id.itemtext2);
img = (ImageView) itemView.findViewById(R.id.itemimage);
card.setOnClickListener(this);
}
#Override
public void onClick(View view){
Toast.makeText(
cakes_activity.this,
"Your message here",
Toast.LENGTH_SHORT
).show();
}
}
}
this is my java class to store the data received from firebase
public class Data {
private String ID,Name,image,RS;
public Data(){
}
public Data(String ID, String Name, String image, String RS) {
this.ID = ID;
this.Name = Name;
this.image= image;
this.RS = RS;
}
public String getId() {
return ID;
}
public String getRS() {
return RS;
}
public void setId(String id) {
this.ID = id;
}
public String getName() {
return Name;
}
public void setName(String Name) {
this.Name = Name;
}
public String getimage() {
return image;
}
public void setImg(String image) {
this.image = image;
}
}
listdata = new ArrayList<>();
north = new NorthAdaptor(context,listdata);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.hasFixedSize();
recyclerView.setAdapter(north);
you need to write this code in onCreate instead of onchildadded

FirebaseRecyclerAdapter with gridlayoutmanager

I have recyclerview recieving data from firebase and i want to make last item uploaded to be first item in the list.I'm using GridLayoutManager and want to display a pic with a text, all of this work fine but i want to make them in order like instagram, does any one know something like that ?
Here is my code
public class ItemsUser extends Fragment {
private View mMainView;
private RecyclerView mUsersList;
private String user_id;
private DatabaseReference mUserDatabase;
private FirebaseRecyclerAdapter<ItemRecycleview,UserRecycleView> firebaseRecyclerAdapter;
private DatabaseReference mDatabaseReference;
private FirebaseListAdapter<ItemRecycleview> firebaseListAdapter;
public ItemsUser() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
mMainView = inflater.inflate(R.layout.fragment_items_user, container, false);
mUsersList = (RecyclerView) mMainView.findViewById(R.id.recyclerView_profile);
mUsersList.setHasFixedSize(true);
mUsersList.setLayoutManager(new GridLayoutManager(getActivity(),3));
ProfileUser activity = (ProfileUser) getActivity();
user_id = activity.getMyData();
mDatabaseReference = FirebaseDatabase.getInstance().getReference();
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users_photos").child(user_id);
mUserDatabase.keepSynced(true);
return mMainView;
}
#Override
public void onStart() {
super.onStart();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<ItemRecycleview, UserRecycleView>(
ItemRecycleview.class,
R.layout.recycleview_item,
UserRecycleView.class,
mUserDatabase
) {
#Override
protected void populateViewHolder(UserRecycleView viewHolder, ItemRecycleview model, int position) {
viewHolder.setImageName(model.getImageName());
viewHolder.setImageURL(model.getImageURL(),getContext());
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String key = firebaseRecyclerAdapter.getRef(mUsersList.getChildLayoutPosition(v)).getKey();
Intent imageIntent = new Intent(getActivity(), ImageActivity.class);
imageIntent.putExtra("imageKey",key);
imageIntent.putExtra("user_id",user_id);
startActivity(imageIntent);
}
});
}
};
mUsersList.setAdapter(firebaseRecyclerAdapter);
}
public static class UserRecycleView extends RecyclerView.ViewHolder {
View mView;
public UserRecycleView(View itemView) {
super(itemView);
mView = itemView;
}
public void setImageName(String imageName){
TextView userNameView = (TextView) mView.findViewById(R.id.ImageNameTextView);
userNameView.setText(imageName);
}
public void setImageURL(final String imageURL,final Context ctx){
final ImageView userImageView = (ImageView) mView.findViewById(R.id.imageView);
Picasso.with(ctx).load(imageURL).networkPolicy(NetworkPolicy.OFFLINE).into(userImageView, new Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
Picasso.with(ctx).load(imageURL).into(userImageView);
}
});
}
}
}
and this is ItemRecyclerview:
public class ItemRecycleview {
public String imageName;
public String imageURL;
public ItemRecycleview(){
}
public ItemRecycleview(String imageName, String imageURL) {
this.imageName = imageName;
this.imageURL = imageURL;
}
public String getImageName() {
return imageName;
}
public void setImageName(String imageName) {
this.imageName = imageName;
}
public String getImageURL() {
return imageURL;
}
public void setImageURL(String imageURL) {
this.imageURL = imageURL;
}
}
hey guys i just found the answer :D
all you need to do is to add this method in firebaseRecyclerAdapter
here it's:
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<ItemRecycleview, UserRecycleView>(
ItemRecycleview.class,
R.layout.recycleview_item,
UserRecycleView.class,
mUserDatabase
) {
#Override
public ItemRecycleview getItem(int position) {
return super.getItem(getItemCount() - 1 - position);
}
#Override
protected void populateViewHolder(UserRecycleView viewHolder, ItemRecycleview model, int position) {
viewHolder.setImageName(model.getImageName());
viewHolder.setImageURL(model.getImageURL(),getContext());
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String key = firebaseRecyclerAdapter.getRef(mUsersList.getChildLayoutPosition(v)).getKey();
Intent imageIntent = new Intent(getActivity(), ImageActivity.class);
imageIntent.putExtra("imageKey",key);
imageIntent.putExtra("user_id",user_id);
startActivity(imageIntent);
}
});
}
};
and that will make it done
Have you tried using setReverseLayout() method to make first element last in the list
GridLayoutManager mLayoutManager = new GridLayoutManager(getActivity(),3);
mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);
mUsersList.setLayoutManager(mLayoutManager);

Firebase display integers inside a textfield in an android app

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

Categories

Resources