I have a JSON node wich is displayed in logcat and look like this :
jsonData[{"numarProduse":"4","pretTotal":"1138","data":"11-27-2017","numeVanzator":"Clau","numarClient":0,"detaliiCos":{"metodaPlata":"CASH"}}]
But I want from this JSON response to display in another activity just 3 of them "numarProduse":"4", "pretTotal":"1138", "numarClient":0.
I tried something else, to upload data to Firebase and then retrive data from Firebase.
Here is my RecyclerView Adapter:
public class CardArrayAdapter extends RecyclerView.Adapter<CardArrayAdapter.ViewHolder> {
private List<Card> listdata;
public CardArrayAdapter(List<Card> listdata) {
this.listdata = listdata;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.list_item_card, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Card vanzatorProduse = listdata.get(position);
holder.txtViewNumarCumparator.setText(vanzatorProduse.getProdusId());
holder.listaProduse.setText(vanzatorProduse.getQty());
holder.sumaProduse.setText(vanzatorProduse.getSubTotal());
}
#Override
public int getItemCount() {
return listdata.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView txtViewNumarCumparator;
public TextView listaProduse;
public TextView sumaProduse;
public ViewHolder(View itemView) {
super(itemView);
txtViewNumarCumparator = (TextView) itemView.findViewById(R.id.txtViewNumarCumparator);
listaProduse = (TextView) itemView.findViewById(R.id.listaProduse);
sumaProduse = (TextView) itemView.findViewById(R.id.sumaProduse);
}
}
}
I made my layout for CardView, and object class with getters and setters.
public class Card implements Serializable {
public String produsId;
public String subTotal;
public String qty;
public Card(){}
public Card(String produsId, String subTotal, String qty) {
this.produsId = produsId;
this.subTotal = subTotal;
this.qty = qty;
}
public String getProdusId() {
return produsId;
}
public void setProdusId(String produsId) {
this.produsId = produsId;
}
public String getSubTotal() {
return subTotal;
}
public void setSubTotal(String subTotal) {
this.subTotal = subTotal;
}
public String getQty() {
return qty;
}
public void setQty(String qty) {
this.qty = qty;
}
}
And here is where I tried to retrive data from Firebase:
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
list = new ArrayList<>();
for (DataSnapshot snapshot : dataSnapshot.getChildren()){
VanzatorProduse produse = snapshot.getValue(VanzatorProduse.class);
Card listdata = new Card();
String id = produse.getProdusId();
String qty = String.valueOf(produse.getQty());
String suma = String.valueOf(produse.getSubTotal());
listdata.setProdusId(id);
listdata.setQty(qty);
listdata.setSubTotal(suma);
list.add(listdata);
}
CardArrayAdapter recycler = new CardArrayAdapter(list);
RecyclerView.LayoutManager layoutmanager = new LinearLayoutManager(VanzatorActivity.this);
recyclerview.setLayoutManager(layoutmanager);
recyclerview.setItemAnimator( new DefaultItemAnimator());
recyclerview.setAdapter(recycler);
}
But is not working, I get two columns where is 0 displayed. I have no idea what to do next, here I'm blocked.
You can create a bean class with setter getter for your List Items and the bean class should implement parcelable so that you can send the bean object to other activity. For recycler View you use adapter of same bean type and set that adapter once data is loaded from server.
Related
I am using an MVVM structure in my project. In one activity I have two recyclerviews and one adapter. They are both using the same tasks_layout. I set the data this way:
final UnsortedAdapter adapter = new UnsortedAdapter();
eatFrog.setHasFixedSize(true);
eatFrog.setLayoutManager(new LinearLayoutManager(this));
eatFrog.setAdapter(adapter);
biologicalRhythms.setAdapter(adapter);
biologicalRhythms.setLayoutManager(new LinearLayoutManager(this));
biologicalRhythms.setHasFixedSize(true);
viewModel.getUnsortedWhereDateIs(currentDate).observe(this, new Observer<List<Unsorted>>() {
#Override
public void onChanged(List<Unsorted> unsorted) {
adapter.setData(EatAFrog(unsorted));
eatAFrogList = EatAFrog(unsorted);
adapter.setData(BiologicalRhythms(unsorted));
biologicalList = BiologicalRhythms(unsorted);
}
});
The point is that each recyclerview gets data of the same class "Unsorted", but data went through two different algorithms. If I do it this way, then two recyclerviews will have the same data, that went through second algorithm.
I tried to do different things. Firstly, I tried to use another instance of my adapter to the second recyclerview, but in that case there was no data displayed in the first recyclerview:
final UnsortedAdapter adapter = new UnsortedAdapter();
eatFrog.setHasFixedSize(true);
eatFrog.setLayoutManager(new LinearLayoutManager(this));
eatFrog.setAdapter(adapter);
final UnsortedAdapter adapter2 = new UnsortedAdapter();
biologicalRhythms.setAdapter(adapter2);
biologicalRhythms.setLayoutManager(new LinearLayoutManager(this));
biologicalRhythms.setHasFixedSize(true);
Seconly, I tried to create another adapter class, the same as third. I also created another layout - tasks2_layout, where I only changed the ids of the elements. Then in second new adapter I also changed them in a viewholder class.
My very first adapter looks like:
List<Unsorted> list = new ArrayList<>();
#NonNull
#Override
public UnsortedViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.tasks_layout , parent, false);
return new UnsortedAdapter.UnsortedViewHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull UnsortedViewHolder holder, int position) {
Unsorted data = list.get(position);
holder.title.setText(data.getName());
holder.date.setText(data.getDate());
holder.category.setText(String.valueOf(data.getCategory()));
holder.attach.setText(String.valueOf(data.isAttach()));
holder.to.setText(String.valueOf(toTime(data.getDuration() + data.getTimeBegin())));
holder.from.setText(String.valueOf(toTime(data.getTimeBegin())));
}
public void setData(List<Unsorted> unsortedList){
this.list = unsortedList;
notifyDataSetChanged();
}
#Override
public int getItemCount() {
return list.size();
}
class UnsortedViewHolder extends RecyclerView.ViewHolder{
private TextView title;
private TextView date;
private TextView from;
private TextView to;
private TextView category;
private TextView attach;
public UnsortedViewHolder(#NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.tv_title);
date = itemView.findViewById(R.id.tv_date);
from = itemView.findViewById(R.id.tv_from2);
to = itemView.findViewById(R.id.tv_to2);
category = itemView.findViewById(R.id.tv_category);
attach = itemView.findViewById(R.id.tv_attach);
}
}
And the second, new one:
List<Unsorted> list = new ArrayList<>();
#NonNull
#Override
public UnsortedViewHolder2 onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.tasks2_layout , parent, false);
return new UnsortedAdapter2.UnsortedViewHolder2(itemView);
}
#Override
public void onBindViewHolder(#NonNull UnsortedViewHolder2 holder, int position) {
Unsorted data = list.get(position);
holder.title.setText(data.getName());
holder.date.setText(data.getDate());
holder.category.setText(String.valueOf(data.getCategory()));
holder.attach.setText(String.valueOf(data.isAttach()));
holder.to.setText(String.valueOf(toTime(data.getDuration() + data.getTimeBegin())));
holder.from.setText(String.valueOf(toTime(data.getTimeBegin())));
}
public void setData1(List<Unsorted> unsortedList){
this.list = unsortedList;
notifyDataSetChanged();
}
#Override
public int getItemCount() {
return list.size();
}
class UnsortedViewHolder2 extends RecyclerView.ViewHolder{
private TextView title;
private TextView date;
private TextView from;
private TextView to;
private TextView category;
private TextView attach;
public UnsortedViewHolder2(#NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.tv_title1);
date = itemView.findViewById(R.id.tv_date1);
from = itemView.findViewById(R.id.tv_from3);
to = itemView.findViewById(R.id.tv_to3);
category = itemView.findViewById(R.id.tv_category1);
attach = itemView.findViewById(R.id.tv_attach1);
}
}
When I have set a different adapter to each of the recyclerview, nothing changed. As in previous case, only second recyclerview was showing some data.
final UnsortedAdapter adapter = new UnsortedAdapter();
eatFrog.setHasFixedSize(true);
eatFrog.setLayoutManager(new LinearLayoutManager(this));
eatFrog.setAdapter(adapter);
final UnsortedAdapter2 adapter2 = new UnsortedAdapter();
biologicalRhythms.setAdapter(adapter2);
biologicalRhythms.setLayoutManager(new LinearLayoutManager(this));
biologicalRhythms.setHasFixedSize(true);
And in onChange:
adapter.setData(EatAFrog(unsorted));
eatAFrogList = EatAFrog(unsorted);
adapter2.setData1(BiologicalRhythms(unsorted));
biologicalList = BiologicalRhythms(unsorted);
Could you please suggest how I can solve this problem? Thanks for any help.
MyViewModel class:
public class UnsortedViewModel extends AndroidViewModel {
private DatesRepository repository2;
private UnsortedRepository repository;
private SortedRepository repository3;
private LiveData<List<Unsorted>> allUnsorted;
public UnsortedViewModel(#NonNull Application application) {
super(application);
repository3 = new SortedRepository(application);
repository2 = new DatesRepository(application);
repository = new UnsortedRepository(application);
allUnsorted = repository.getAllUnsorted();
}
public LiveData<List<Unsorted>> getUnsortedWhereDateIsAlgo1(String currentDate) throws ExecutionException, InterruptedException{
return repository.getUnsortedWhereDateIs(currentDate);
}
public LiveData<List<Unsorted>> getUnsortedWhereDateIsAlgo2(String currentDate) throws ExecutionException, InterruptedException{
return repository.getUnsortedWhereDateIs(currentDate);
}
MutableLiveData<List<Unsorted>> unsortedWhereDateIsAlgo1 = new MutableLiveData<>();
MutableLiveData<List<Unsorted>> unsortedWhereDateIsAlgo2 = new MutableLiveData<>();
public void insertDate(Dates dates) {
repository2.insertDate(dates);
}
public LiveData<List<Unsorted>> getAllUnsorted() {
return allUnsorted;
}
public void insert(Unsorted data){
repository.insert(data);
}
public void deleteAllUnsorted(){
repository.deleteAllUnsorted();
}
public void deleteWhereDateIs(String currentDate){
repository.deleteWhereDateIs(currentDate);
}
public void insertSorted(Sorted data){
repository3.insertSorted(data);
}
}
Repository:
public class UnsortedRepository {
private UnsortedDao unsortedDao;
private LiveData<List<Unsorted>> allUnsorted;
public UnsortedRepository(Application application){
UnsortedDatabase database = UnsortedDatabase.getInstance(application);
unsortedDao = database.unsortedDao();
allUnsorted = unsortedDao.getAllUnsorted();
}
public LiveData<List<Unsorted>> getUnsortedWhereDateIs(String currentDate)
throws ExecutionException, InterruptedException{
return new
GetUnsortedWhereDateIsAsyncTask(unsortedDao).execute(currentDate).get();
}
private static class GetUnsortedWhereDateIsAsyncTask extends
AsyncTask<String, Void, LiveData<List<Unsorted>>> {
private UnsortedDao unsortedDao;
private GetUnsortedWhereDateIsAsyncTask(UnsortedDao unsortedDao){
this.unsortedDao = unsortedDao;
}
#Override
protected LiveData<List<Unsorted>> doInBackground(String ... strings) {
LiveData<List<Unsorted>> list =
unsortedDao.getUnsortedWhereDateIs(strings[0]);
return list;
}
}
}
Dao:
#Query ("SELECT * FROM Unsorted WHERE date = :currentDate")
LiveData<List<Unsorted>> getUnsortedWhereDateIs (String currentDate);
Your problem is, that you are observing single data source, so you will always have same data in both adapters. You need to add another LiveData inside your viewmodel. Use something like this:
unsortedWhereDateIsAlgo1 : MutableLiveData<List<Unsorted>>()
unsortedWhereDateIsAlgo2 : MutableLiveData<List<Unsorted>>()
and post result of your algorithms into correct LiveData:
fun algo1() {
//some code/operations
unsortedWhereDateIsAlgo1.postValue(result)
}
fun algo2() {
//some different code/operations
unsortedWhereDateIsAlgo2.postValue(result)
}
Next, in your fragment/activity:
viewModel.getUnsortedWhereDateIsAlgo1(currentDate).observe(this, new Observer<List<Unsorted>>() {
#Override
public void onChanged(List<Unsorted> unsorted) {
final UnsortedAdapter adapter = new UnsortedAdapter();
adapter.setData(EatAFrog(unsorted));
eatFrog.setHasFixedSize(true);
eatFrog.setLayoutManager(new LinearLayoutManager(this));
eatFrog.setAdapter(adapter);
}
});
and
viewModel.getUnsortedWhereDateIsAlgo2(currentDate).observe(this, new Observer<List<Unsorted>>() {
#Override
public void onChanged(List<Unsorted> unsorted) {
final UnsortedAdapter adapter2 = new UnsortedAdapter();
adapter2.setData(BiologicalRhythms(unsorted));
biologicalRhythms.setAdapter(adapter2);
biologicalRhythms.setLayoutManager(new LinearLayoutManager(this));
biologicalRhythms.setHasFixedSize(true);
}
});
I need to design the bus seat view in android application and I have seat positions in matrix format. For example
a1.row=4;
a1.column=5;
I only need to set those seats in gridview based on their matrix position. Other spaces will be blank or empty in matrix. After showing these seat view if I choose specific seat then that seat related object will be added in a list of object and if I deselect it will be removed from the list of object. Can anyone help me to find out a solution for this ? Is it possible to implement this using gridview but I will set the item in grid view using its matrix position
Try this code
Activityclass
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.rv_list);
showSeatList();
}
private void showSeatList()
{
List<SeatModel> seatModels = new ArrayList<>();
seatModels.add(new SeatModel("1","A",false));
seatModels.add(new SeatModel("2","B",false));
seatModels.add(new SeatModel("3","C",false));
seatModels.add(new SeatModel("4","D",false));
seatModels.add(new SeatModel("5","E",false));
seatModels.add(new SeatModel("6","F",false));
seatModels.add(new SeatModel("7","G",false));
seatModels.add(new SeatModel("8","H",false));
seatModels.add(new SeatModel("9","I",false));
seatModels.add(new SeatModel("10","J",false));
SeatSelectionAdapter adapter = new SeatSelectionAdapter(this,seatModels);
recyclerView.setLayoutManager(new GridLayoutManager(this, 5));
recyclerView.setAdapter(adapter);
}
}
Adapter Class
public class SeatSelectionAdapter extends RecyclerView.Adapter<SeatSelectionAdapter.ViewHolder>
{
private List<SeatModel> seatModelList;
private LayoutInflater inflater;
private Context context;
public SeatSelectionAdapter(Context context, List<SeatModel>models)
{
this.seatModelList = models;
inflater = LayoutInflater.from(context);
this.context = context;
}
#NonNull
#Override
public SeatSelectionAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.model_seat,parent,false);
return new SeatSelectionAdapter.ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull SeatSelectionAdapter.ViewHolder holder, int position) {
SeatModel model = seatModelList.get(position);
holder.setData(model);
}
#Override
public int getItemCount() {
return seatModelList.size();
}
class ViewHolder extends RecyclerView.ViewHolder
{
private CardView cardView;
private LinearLayout lnLayout;
private TextView tvSeat;
private ViewHolder(View view) {
super(view);
tvSeat = view.findViewById(R.id.tv_seat);
cardView = view.findViewById(R.id.cv_card);
lnLayout = view.findViewById(R.id.ln_layout);
}
#SuppressLint({"SetTextI18n", "ClickableViewAccessibility"})
private void setData(final SeatModel model)
{
tvSeat.setText(model.getSeat());
if(model.isSelect())
lnLayout.setBackgroundColor(context.getResources().getColor(R.color.colorAccent));
else lnLayout.setBackgroundColor(context.getResources().getColor(R.color.white));
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(model.isSelect())
{
model.setSelect(false);
Toast.makeText(context,"Seat not selected",Toast.LENGTH_LONG).show();
}
else{
model.setSelect(true);
Toast.makeText(context,"Seat selected",Toast.LENGTH_LONG).show();
}
notifyDataSetChanged();
}
});
}
}
}
Model Class
public class SeatModel
{
private String id,seat;
private boolean isSelect;
public SeatModel(String id, String seat, boolean isSelect) {
this.id = id;
this.seat = seat;
this.isSelect = isSelect;
}
public boolean isSelect() {
return isSelect;
}
public void setSelect(boolean select) {
isSelect = select;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getSeat() {
return seat;
}
public void setSeat(String seat) {
this.seat = seat;
}
}
I am trying to make a list of Recycler View by loading data from FireBase
For that I made MainList.java
public class MainList {
private String imageResourceId;
private String name;
public MainList() {}
public MainList(String imageResourceId, String name){
this.imageResourceId = imageResourceId;
this.name = name;
}
public String getImageResourceId() { return imageResourceId; }
public String getName(){ return name;}
}
and MainListAdapter
public class MainListAdapter extends RecyclerView.Adapter<MainListAdapter.MainListViewHolder>{
ImageView item_image;
List<MainList> mMainList;
LayoutInflater inflater;
class MainListViewHolder extends RecyclerView.ViewHolder {
TextView item_name;
ImageView item_image;
MainListAdapter mAdapter;
public MainListViewHolder(View itemView, MainListAdapter adapter) {
super(itemView);
item_name = itemView.findViewById(R.id.item_name);
item_image = itemView.findViewById(R.id.item_image);
this.mAdapter = adapter;
}
}
public MainListAdapter(Context context, List<MainList> main_list){
inflater = LayoutInflater.from(context);
this.mMainList = main_list;
}
#NonNull
#Override
public MainListViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View mItemView = inflater.inflate(R.layout.list_item, parent, false);
return new MainListViewHolder(mItemView, this);
}
#Override
public void onBindViewHolder(#NonNull MainListViewHolder holder, int position) {
MainList mainList = mMainList.get(position);
holder.item_name.setText(mainList.getName());
Picasso.get().load(mainList.getImageResourceId()).into(item_image);
//holder.item_image.setImageResource(mainList.getImageResourceId());
}
#Override
public int getItemCount() {
return mMainList.size();
}
}
And I try to use use Picasso inside of OnBindViewHolder for my image
and in MainActivity I try to get my data from FireBase to fill my List
main_comics_List = new ArrayList<>();
mRef = FirebaseDatabase.getInstance().getReference().child("ComicsData").child("AllComicses");
mRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()){
MainList item_of = dataSnapshot1.getValue(MainList.class);
main_comics_List.add(item_of);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
But when I start my app nothing shows at the place of my Recycler and I get the message
What am I doing wrong? And how do I solve it?
I would appreciate you answering me.
The instance variables inside the class MainList should be the same as the attributes inside the database. Therefore inside the MainList add the following:
private String imageLink;
private String title;
And the setters and getters:
public String getImageLink() { return imageLink; }
public String getTitle(){ return title;}
public void setImageLink(String imageLink) { this.imageLink = imageLink; }
public void setTitle(String title){ this.title = title;}
Also in your database it doesnt seem that you are using name and imageResourceId therefore you can remove them from the class MainList.
My RecyclerView not show any data from Firebase. I don't see any errors at logcat so I don't know why. I followed guide from youtube video. But still nothing. I'm using android 8.0 and API 27. Hope Somebody can help.
My Main Activity Code:
public class ViewProduct extends AppCompatActivity {
DatabaseReference ref;
ArrayList<Model> list;
private RecyclerView recyclerView;
private RecyclerView.Adapter viewHolder;
private RecyclerView.LayoutManager layoutManager;
SearchView searchView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_product) ;
ref = FirebaseDatabase.getInstance().getReference().child("buddymealplanneruser").
child("Products");
recyclerView = findViewById(R.id.stallproductRecyclerView);
//recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
//SEARCH VIEW
searchView = findViewById(R.id.searchProductStall);
//ADAPTER
list = new ArrayList<>();
viewHolder = new ViewHolder(list);
recyclerView.setAdapter(viewHolder);
}
#Override
protected void onRestart() {
super.onRestart();
if (ref!=null)
{
ref.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists())
{
//list = new ArrayList<>();
list.clear();
for(DataSnapshot ds : dataSnapshot.getChildren())
{
list.add(ds.getValue(Model.class));
}
//ViewHolder viewHolder = new ViewHolder(list);
//recyclerView.setAdapter(viewHolder);
viewHolder.notifyDataSetChanged();
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(ViewProduct.this, databaseError.getMessage(),
Toast.LENGTH_SHORT).show();
}
});
}
if (searchView !=null)
{
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String s)
{
return false;
}
#Override
public boolean onQueryTextChange(String s) {
search(s);
return true;
}
});
}
}
private void search(String str) {
ArrayList<Model> myList = new ArrayList<>();
for(Model object : list)
{
if(object.getProductName().toLowerCase().contains(str.toLowerCase()))
{
myList.add(object);
}
}
ViewHolder viewHolder = new ViewHolder(myList);
recyclerView.setAdapter(viewHolder);
}
}
My Adapter:
public class ViewHolder extends RecyclerView.Adapter<ViewHolder.MyViewHolder> {
ArrayList<Model> list;
public ViewHolder (ArrayList<Model> list)
{
this.list=list;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_product, viewGroup,false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder myViewHolder, int i) {
myViewHolder.productName.setText(list.get(i).getProductName());
myViewHolder.productCalorie.setText(list.get(i).getProductCalorie());
myViewHolder.StallId.setText(list.get(i).getStallId());
myViewHolder.productType.setText(list.get(i).getProductType());
myViewHolder.productServing.setText(list.get(i).getProductServing());
myViewHolder.statusProduct.setText(list.get(i).getStatusProduct());
}
#Override
public int getItemCount()
{
return list.size();
}
class MyViewHolder extends RecyclerView.ViewHolder
{
TextView productName, productCalorie, StallId;
TextView productType, productServing, statusProduct;
ImageView productImageView;
public MyViewHolder (#NonNull View itemView){
super((itemView));
productName = itemView.findViewById(R.id.productTitle);
productCalorie = itemView.findViewById(R.id.productDescriptionCalorie);
StallId = itemView.findViewById(R.id.productDescriptionStallId);
productType = itemView.findViewById(R.id.productDescriptionType);
productServing=itemView.findViewById(R.id.productDescriptionServing);
statusProduct=itemView.findViewById(R.id.productDescriptionAvailibility);
//productImageView = itemView.findViewById(R.id.productImageView);
}
}
}
My Model :
package com.example.buddymealplannerstall.Child;
import android.view.Display;
public class Model {
//String productName, productImage, productCalorie, StallId, productType, productServing, statusProduct;
private String StallId;
private String productCalorie;
private String productImage;
private String productName;
private String productServing;
private String productType;
private String statusProduct;
public Model (String StallId,
String productCalorie,
String productImage,
String productName,
String productServing,
String productType,
String statusProduct){
this.StallId = StallId;
this.productCalorie = productCalorie;
this.productImage = productImage;
this.productName = productName;
this.productServing = productServing;
this.productType = productType;
this.statusProduct = statusProduct;
}
public Model(){
}
public String getStallId() {
return StallId;
}
public void setStallId(String stallId) {
StallId = stallId;
}
public String getProductCalorie() {
return productCalorie;
}
public void setProductCalorie(String productCalorie) {
this.productCalorie = productCalorie;
}
public String getProductImage() {
return productImage;
}
public void setProductImage(String productImage) {
this.productImage = productImage;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getProductServing() {
return productServing;
}
public void setProductServing(String productServing) {
this.productServing = productServing;
}
public String getProductType() {
return productType;
}
public void setProductType(String productType) {
this.productType = productType;
}
public String getStatusProduct() {
return statusProduct;
}
public void setStatusProduct(String statusProduct) {
this.statusProduct = statusProduct;
}
}
Screen Shot of my firebase and my apps.
firebase
my app
According to your comment:
yes, buddymealplanner is my project's name
Please note that there is no need to add the name of your project as a child in the reference. So please change the following line of code:
ref = FirebaseDatabase.getInstance().getReference().child("buddymealplanneruser").
child("Products");
Simply to:
ref = FirebaseDatabase.getInstance().getReference().child("Products");
I dont think you are populating your list, you are sending it empty. From what I can see, you only add items to the list when you are restarting the activity. Try either populating it before sendinig the list, or changing the onRestart() for an onStart() or an onResume().
This is my adapter for recycle view
CategoryAdapter Class
public class CategoryRAdapter extends RecyclerView.Adapter<CategoryRAdapter.MyViewHolder> {
private Context mcontext;
private List<Food> mData;
RequestOptions option;
public CategoryRAdapter(Context mcontext, List<Food> mData) {
this.mcontext = mcontext;
this.mData = mData;
option = new RequestOptions().centerCrop().placeholder(R.drawable.loading_shape).error(R.drawable.loading_shape);
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.food_row, parent, false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Food current = mData.get(position);
holder.tv_Name.setText(current.getName());
holder.tv_Rating.setText(current.getRating());
holder.tv_Descip.setText(current.getDescrip());
holder.tv_Category.setText(current.getCateg());
Glide.with(mcontext).load(mData.get(position).getImages()).apply(option).into(holder.img_Thumbnail);
}
#Override
public int getItemCount() {
return mData.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView tv_Name;
TextView tv_Rating;
TextView tv_Descip;
TextView tv_Category;
ImageView img_Thumbnail;
public MyViewHolder(View itemView) {
super(itemView);
tv_Name = itemView.findViewById(R.id.food_name);
tv_Rating = itemView.findViewById(R.id.rating);
tv_Descip = itemView.findViewById(R.id.desc);
tv_Category = itemView.findViewById(R.id.category);
img_Thumbnail = itemView.findViewById(R.id.thumbnail);
}
}
}
This is my model
Food Class
public class Food {
String Name;
String Images;
String Descrip;
String Rating;
String Categ;
public Food() {
}
public Food(String name, String images, String descrip, String rating, String categ) {
this.Name = name;
this.Images = images;
this.Descrip = descrip;
this.Rating = rating;
this.Categ = categ;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public String getImages() {
return Images;
}
public void setImages(String images) {
Images = images;
}
public String getDescrip() {
return Descrip;
}
public void setDescrip(String descrip) {
Descrip = descrip;
}
public String getRating() {
return Rating;
}
public void setRating(String rating) {
Rating = rating;
}
public String getCateg() {
return Categ;
}
public void setCateg(String categ) {
Categ = categ;
}
}
My mian activity
CategoryActivity Class
public class CategoriaActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private List<Food> dbObjects;
private RecyclerView.Adapter adapter;
private AlertDialog.Builder dialogBuilder;
private AlertDialog dialog;
TextView l_nombre,l_precio;
Button l_finalizar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_categoria);
recyclerView = findViewById(R.id.recycle_id);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
dbObjects = new ArrayList<>();
ParseQuery<ParseObject> query = ParseQuery.getQuery("Category");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objList, ParseException e) {
if (e == null) {
for (ParseObject obj : objList) {
Food food = new Food();
food.setName(obj.getString("Name"));
food.setDescrip(obj.getString("Descrip"));
food.setRating(obj.getString("Rating"));
food.setCateg(obj.getString("Categ"));
food.setImages(obj.getString("Images"));
dbObjects.add(food);
}
} else {
FancyToast.makeText(CategoriaActivity.this, "Error: " + e.getMessage(), FancyToast.LENGTH_SHORT, FancyToast.ERROR, true).show();
}
}
});
adapter = new CategoryRAdapter(this, dbObjects);
recyclerView.setAdapter(adapter);
FloatingActionButton fab = findViewById(R.id.shoppingFlot);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
carritopop();
}
});
}
private void carritopop() {
dialogBuilder = new AlertDialog.Builder(this);
View view = getLayoutInflater().inflate(R.layout.pop_cart,null);
l_finalizar = view.findViewById(R.id.l_finalizar);
l_finalizar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
pago();
}
});
dialogBuilder.setView(view);
dialog = dialogBuilder.create();
dialog.show();
}
private void pago() {
Intent intent = new Intent(CategoriaActivity.this, PagoActivity.class);
startActivity(intent);
}
}
I dont know whats wrong with the code, the recycle view aint working, it aint showing any items, i would really appreciate if someone could help me out and notice something wrong in my code
Call adapter.notifyDataSetChanged() in your query done callback, this will notify the adapter that the list has changed. This is happening because when you set the adapter, the list was empty as the query was running in background. Once it completes, you have to tell the adapter that new data has been added to list:
if (e == null) {
for (ParseObject obj : objList) {
...
}
adapter.notifyDataSetChanged();
}
Also, create your adapter before you send the query otherwise it may result in NullPointerException in case data is loaded before the next statement executes (highly unlikely but never hurts to be safe).
dbObjects = new ArrayList<>();
adapter = new CategoryRAdapter(this, dbObjects);
ParseQuery<ParseObject> query = ParseQuery.getQuery("Category");
I recommend you to use RxJava2 to user asynchronous methods, it's awesome... And about the problem, maybe your data is not ready when you're already creating the adapter with the ArrayList.
Try to move the recyclerView.setAdapter() to inside the FindCallback after create the Food Objects.