runtime exception when i click the button to load data - android

I want to retrieve the data from a SQLite database and show it in the RecyclerView but it shows an error
((Adapter class))
public class myAdapert extends RecyclerView.Adapter<myAdapert.myViewHolder> {
ArrayList<Model_class> modelClassArrayList;
public myAdapert(ArrayList<Model_class> modelClassArrayList) {
this.modelClassArrayList = modelClassArrayList;
}
#NonNull
#Override
public myViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View customView = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.custom_row,viewGroup,false);
myViewHolder myviewHolder = new myViewHolder(customView);
return myviewHolder;
}
#Override
public void onBindViewHolder(#NonNull myViewHolder myViewHolder, int i) {
Model_class model_class = modelClassArrayList.get(i);
myViewHolder.author.setText(model_class.getAuthor());
myViewHolder.title.setText(model_class.getTitle());
myViewHolder.cost.setText(model_class.getCost());
myViewHolder.quantity.setText(model_class.getQuantity());
}
#Override
public int getItemCount() {
return modelClassArrayList.size();
}
public class myViewHolder extends RecyclerView.ViewHolder {
TextView author,title;
TextView cost,quantity;
public myViewHolder(#NonNull View itemView) {
super(itemView);
author = itemView.findViewById(R.id.authortxt);
title = itemView.findViewById(R.id.titleTxt);
cost = itemView.findViewById(R.id.costTxt);
quantity = itemView.findViewById(R.id.quantityTxt);
}
}

As cost and quantity are numbers, I suppose, you should convert it to String explicitly, to prevent resourse manager from loading drawables:
myViewHolder.cost.setText(String.valueOf(model_class.getCost()));
myViewHolder.quantity.setText(String.valueOf(model_class.getQuantity()));

Related

How to get shared prefs in Adapter class, Firebase

I Get data from firebase into a recycler view, In my Adapter class I want to get a Data which is stored in my Shared Prefs, How can I get the Data which is in my Shared Prefs into my Firebase Adapter Activity !
MY ADAPTER CLASS
public class Chat_Adapter extends
FirebaseRecyclerAdapter<Chat_Model,Chat_Adapter.myViewHolder> {
public Chat_Adapter(#NonNull FirebaseRecyclerOptions<Chat_Model> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull myViewHolder holder, int position, #NonNull Chat_Model model) {
holder.date.setText(model.getDate());
holder.uname.setText(model.getUname());
holder.msg.setText(model.getMsg());
}
#NonNull
#Override
public myViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_item, parent, false);
return new myViewHolder(view);
}
class myViewHolder extends RecyclerView.ViewHolder {
TextView uname, date,msg;
public myViewHolder(#NonNull View itemView) {
super(itemView);
uname = itemView.findViewById(R.id.named);
date = itemView.findViewById(R.id.date);
msg = itemView.findViewById(R.id.message);
}
}
}
I have a data coming from my Database which (model.getUname), What i want to do is to my original name from my Shared Prefs and check if (model.getUname == myname).
Get Context inside onCreateViewHolder() method and save it in your field variable and then use it inside onBindViewHolder() and get shared prefs there
public class Chat_Adapter extends FirebaseRecyclerAdapter<Chat_Model, Chat_Adapter.myViewHolder> {
private Context context;
public Chat_Adapter(#NonNull FirebaseRecyclerOptions<Chat_Model> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull myViewHolder holder, int position, #NonNull Chat_Model model) {
//here is your shared pref
SharedPreferences your_shared_pref_name = context.getSharedPreferences("YOUR_SHARED_PREF_NAME", Context.MODE_PRIVATE);
holder.date.setText(model.getDate());
holder.uname.setText(model.getUname());
holder.msg.setText(model.getMsg());
}
#NonNull
#Override
public myViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.chat_item, parent, false);
context = parent.getContext();
return new myViewHolder(view);
}
class myViewHolder extends RecyclerView.ViewHolder {
TextView uname, date, msg;
public myViewHolder(#NonNull View itemView) {
super(itemView);
uname = itemView.findViewById(R.id.named);
date = itemView.findViewById(R.id.date);
msg = itemView.findViewById(R.id.message);
}
}
}

How to show all items of list in recyclerview?

I have an adapter that is showing limited Items on Fragment.
#Override
public int getItemCount() {
int limit = 7;
return Math.min(latestProductModelList.size(),limit);
}
and I want to show all the items of list in recyclerview when I click on ViewAll button using the same adapter on another acitivity.
this is my adapter.
`
public class LatestProductAdapter extends RecyclerView.Adapter<LatestProductAdapter.ViewHolder> {
List<LatestProductModel> latestProductModelList = new ArrayList<>();
Context context;
LatestProductClickInterface latestProductClickInterface;
public LatestProductAdapter(Context context, LatestProductClickInterface latestProductClickInterface) {
this.context = context;
this.latestProductClickInterface = latestProductClickInterface;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_layout,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
LatestProductModel latestProductModel = latestProductModelList.get(position);
Glide.with(context).load(latestProductModel.getImage()).into(holder.itemImage);
holder.itemTitle.setText(latestProductModel.getTitle());
holder.itemView.setOnClickListener(v -> {
latestProductClickInterface.OnLatestProductClicked(latestProductModelList.get(position));
});
}
#Override
public int getItemCount() {
int limit = 7;
return Math.min(latestProductModelList.size(),limit);
}
#SuppressLint("NotifyDataSetChanged")
public void updateList(List<LatestProductModel> latestProductModels){
latestProductModelList.clear();
latestProductModelList.addAll(latestProductModels);
Collections.reverse(latestProductModelList);
notifyDataSetChanged();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
ImageView itemImage;
TextView itemTitle;
public ViewHolder(#NonNull View itemView) {
super(itemView);
itemImage = itemView.findViewById(R.id.item_img);
itemTitle = itemView.findViewById(R.id.item_title);
}
}
}
`
How to achieve this?
Or is there another way to achieve this?
kindly help me.
You can use bellow codes for your adapter:
public class LatestProductAdapter extends RecyclerView.Adapter<LatestProductAdapter.ViewHolder> {
List<LatestProductModel> latestProductModelList = new ArrayList<>();
Context context;
LatestProductClickInterface latestProductClickInterface;
private boolean shouldShowAllItems;
public LatestProductAdapter(Context context, LatestProductClickInterface latestProductClickInterface , boolean shouldShowAllItems) {
this.context = context;
this.latestProductClickInterface = latestProductClickInterface;
this.shouldShowAllItems = shouldShowAllItems;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.item_layout,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
LatestProductModel latestProductModel = latestProductModelList.get(position);
Glide.with(context).load(latestProductModel.getImage()).into(holder.itemImage);
holder.itemTitle.setText(latestProductModel.getTitle());
holder.itemView.setOnClickListener(v -> {
latestProductClickInterface.OnLatestProductClicked(latestProductModelList.get(position));
});
}
#Override
public int getItemCount() {
if (shouldShowAllItems){
return latestProductModelList.size();
}else {
int limit = 7;
return Math.min(latestProductModelList.size(), limit);
}
}
#SuppressLint("NotifyDataSetChanged")
public void updateList(List<LatestProductModel> latestProductModels){
latestProductModelList.clear();
latestProductModelList.addAll(latestProductModels);
Collections.reverse(latestProductModelList);
notifyDataSetChanged();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
ImageView itemImage;
TextView itemTitle;
public ViewHolder(#NonNull View itemView) {
super(itemView);
itemImage = itemView.findViewById(R.id.item_img);
itemTitle = itemView.findViewById(R.id.item_title);
}
}
}
and create adapter object accourding your need:
LatestProductAdapter latestProductAdapter = LatestProductAdapter(context , this ,//true or false);

how to add item OnClickListener in FirebaseRecyclerAdapter?

I am trying to set an onClickListener on my recyclerview. I retrieve data from Firebase and am able to load the data in the recyclerview. When I set an onClickListener nothing happens. How can I solve this?
myadapter.class
public class my adapter extends FirebaseRecyclerAdapter<model,myadapter.myviewholder>{
public myadapter(#NonNull FirebaseRecyclerOptions<model> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull myadapter.myviewholder holder, int position, #NonNull model model)
{
holder.name.setText(model.getName());
Glide.with(holder.img.getContext()).load(model.getImgurl()).into(holder.img);
}
#NonNull
#Override
public myviewholder onCreateViewHolder(#NonNull ViewGroup parent, int viewType)
{
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.singlerow,parent,false);
return new myviewholder(view);
}
class myviewholder extends RecyclerView.ViewHolder
{
ImageView img;
TextView name;
Uri tvurl;
public myviewholder(#NonNull View itemView)
{
super(itemView);
img = (ImageView)itemView.findViewById(R.id.img1);
name =(TextView) itemView.findViewById(R.id.nametext);
}
}
}

Android Nested Recylcerview with different items counts

I have a nested recyclerview, and I just want it to display different item numbers for each item of the parent recyclerview.
It's supposed that each item implements number of view according to each model object list that's been passed to adapter
It may look like as the layout shown in this image
Here is my parent adapter code
public class ConstructionAdapter extends RecyclerView.Adapter<ConstructionAdapter.ConstructionViewHolder> {
private RecyclerView.RecycledViewPool viewPool = new RecyclerView.RecycledViewPool();
private final List<ConstructionModel> constructionList;
public ConstructionAdapter(List<ConstructionModel> constructionList) {
this.constructionList = constructionList;
}
#NonNull
#Override
public ConstructionViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.construction_cranes_view_item, parent, false);
return new ConstructionViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull ConstructionViewHolder holder, int position) {
holder.setContent(constructionList.get(position));
}
#Override
public int getItemCount() {
return constructionList.size();
}
public class ConstructionViewHolder extends RecyclerView.ViewHolder {
private TextView categoeyTitleTV;
private RecyclerView constructionRecyclerView;
private ConstructionViewHolder(#NonNull View itemView) {
super(itemView);
categoeyTitleTV = itemView.findViewById(R.id.category_title_tv);
constructionRecyclerView = itemView.findViewById(R.id.construction_item_recycler);
}
private void setContent(ConstructionModel model) {
LinearLayoutManager layoutManager = new LinearLayoutManager(itemView.getContext());
constructionRecyclerView.setLayoutManager(layoutManager);
categoeyTitleTV.setText(model.getCraneCategory());
layoutManager.setInitialPrefetchItemCount(model.getSubCategoryList().size());
IndustrialCranesAdapter subCatAdapter = new IndustrialCranesAdapter(model.getSubCategoryList());
constructionRecyclerView.setAdapter(subCatAdapter);
constructionRecyclerView.setRecycledViewPool(viewPool);
}
}
}
Here is my Inner recyclerview adapter
public class IndustrialCranesAdapter extends RecyclerView.Adapter<IndustrialCranesAdapter.IndustrialCranesViewHolder> {
private final List<Crane> industrialCranesList;
public IndustrialCranesAdapter(List<Crane> industrialCranesList) {
this.industrialCranesList = industrialCranesList;
}
#NonNull
#Override
public IndustrialCranesViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.crane_item, parent, false);
return new IndustrialCranesViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull IndustrialCranesViewHolder holder, int position) {
holder.setCranesItems(industrialCranesList.get(position));
}
#Override
public int getItemCount() {
return industrialCranesList.size();
}
public class IndustrialCranesViewHolder extends RecyclerView.ViewHolder {
private TextView craneTitle;
private ImageView craneImage;
private IndustrialCranesViewHolder(#NonNull View itemView) {
super(itemView);
craneTitle = itemView.findViewById(R.id.industrial_crane_title);
craneImage = itemView.findViewById(R.id.industrial_crane_image_view);
itemView.getRootView().setOnClickListener(v->{
Toast.makeText(itemView.getContext(), "Clicked", Toast.LENGTH_SHORT).show();
});
}
private void setCranesItems(Crane crane){
craneTitle.setText(crane.getCraneTitle());
craneImage.setImageResource(crane.getCraneImage());
}
}
}

What is the practical way write recyclerview

I saw many methods to write Recyclerview. But I do not know what is the best practice way in terms of performance.
I have two methods to write a Recyclerview, are they the same or is there in difference?
First method is to write it in separate Adapter class
Adapter.java
public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
return null;
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder myViewHolder, int i) {
}
#Override
public int getItemCount() {
return 0;
}
class MyViewHolder extends RecyclerView.ViewHolder{
public MyViewHolder(#NonNull View itemView) {
super(itemView);
}
}
}
Second method is to write it like this inside the MainActivity or Fragment
recyclerView.setAdapter(new RecyclerView.Adapter() {
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
return null;
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder viewHolder, int i) {
}
#Override
public int getItemCount() {
return 0;
}
});
}
public class MyViewHolder extends RecyclerView.ViewHolder{
public MyViewHolder(#NonNull View itemView) {
super(itemView);
}
}
The adapter should be implemented as a separated class, because it makes re-using it easier:
public class YourAdapter extends RecyclerView.Adapter<Adapter.MyViewHolder> {
private ArrayList<YourModel> list = new ArrayList();
public YourAdapter(ArrayList<YourModel> list){
this.list = list;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
//return the viewholder
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder myViewHolder, int i) {
//deal with data
}
#Override
public int getItemCount() {
//return the list number
}
class MyViewHolder extends RecyclerView.ViewHolder{
public MyViewHolder(#NonNull View itemView) {
super(itemView);
//bind the views
}
}
}
Than in your Activity/Fragment you can use it like this :
//after you have initialized recyclerview and added the layoutmanager
//prepare the list for the adapter
recyclerView.setAdapter(new YourAdapter(yourList));
This way you can reuse it in more than one Activity/Fragment. This pattern of reusable code should be preferred because you don't have to create the classes multiple times as anonymous classes - if needed.
the best way is to set adapter class outside main class as your explain just use your first class

Categories

Resources