I created my own class called RecyclerviewAdapter.java that extends RecyclerView.Adapter<>, and I created a method in that class
public void setItemCount(int i){
size=i;
}
My RecyclerView works, and I want user to be able to set how many items to be displayed in RecyclerView. In MainActivity, when I call adapter.setItemCount(2), it says Cannot resolve method 'setItemCount' in 'Adapter', also Method 'setItemCount(int)' is never used. I don't know how to solve it.Here are my files.MainActivity.java
RecyclerView recycler;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager manager;
ArrayList<RecyclerviewItem> list=new ArrayList<>();
list.add(new RecyclerviewItem(R.drawable.hoversfw,"Title","Description"));
manager=new LinearLayoutManager(this);
adapter=new RecyclerviewAdapter(list);
recycler=findViewById(R.id.recycler);
recycler.setHasFixedSize(true);
recycler.setLayoutManager(manager);
recycler.setAdapter(adapter);
if(!count.getText().toString().equals("")) {
int c= Integer.parseInt(count.getText().toString());
adapter.setItemCount(c);
}
RecyclerviewAdapter.java
public class RecyclerviewAdapter extends RecyclerView.Adapter<RecyclerviewAdapter.RecyclerviewHolder> {
private ArrayList<RecyclerviewItem> list;
private int size;
#NonNull
#Override
public RecyclerviewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.recyclerview_item,parent,false);
RecyclerviewHolder holder=new RecyclerviewHolder(v);
return holder;
}
public RecyclerviewAdapter(ArrayList<RecyclerviewItem>mlist){
list=mlist;
size=1;
}
public void setItemCount(int i){
size=i;
}
#Override
public void onBindViewHolder(#NonNull RecyclerviewHolder holder, int position) {
RecyclerviewItem item=list.get(position);
holder.image.setImageResource(item.getImageResource());
holder.title.setText(item.getTitle());
holder.description.setText(item.getDescription());
}
#Override
public int getItemCount() {
return size;
}
public static class RecyclerviewHolder extends RecyclerView.ViewHolder{
public ImageView image;
public TextView title;
public TextView description;
public RecyclerviewHolder(#NonNull View itemView) {
super(itemView);
image=itemView.findViewById(R.id.img);
title=itemView.findViewById(R.id.title);
description=itemView.findViewById(R.id.description);
}
}
}
RecyclerviewItem.java
public class RecyclerviewItem {
private int imageResource;
private String title;
private String description;
public RecyclerviewItem(int mimageResource, String mtitle, String mdescription){
imageResource=mimageResource;
title=mtitle;
description=mdescription;
}
public int getImageResource(){
return imageResource;
}
public String getTitle(){
return title;
}
public String getDescription(){
return description;
}
}
You are declaring RecyclerView Adapter with RecyclerView.Adapter although it should be RecyclerviewAdapter
RecyclerView.Adapter is an SDK class, but RecyclerviewAdapter is yours.
Related
MainActivity:
public class MainActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private FirebaseAdapter mAdapter;
private static final String TAG = "MainActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = (RecyclerView)findViewById(R.id.recyclerview);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
FirebaseRecyclerOptions<DataClass> options =
new FirebaseRecyclerOptions.Builder<DataClass>()
.setQuery(FirebaseDatabase.getInstance().getReference().child("Users"), DataClass.class)
.build();
mAdapter = new FirebaseAdapter(options);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
}
#Override
protected void onStart() {
super.onStart();
mAdapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
mAdapter.stopListening();
}
}
FirebaseAdapter
public class FirebaseAdapter extends FirebaseRecyclerAdapter<DataClass,FirebaseAdapter.MyviewHolder> {
private static final String TAG = "FirebaseAdapter";
public FirebaseAdapter(#NonNull FirebaseRecyclerOptions<DataClass> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull MyviewHolder holder, int position, #NonNull DataClass model) {
holder.name.setText(model.getName());
holder.age.setText(model.getAge());
Log.d(TAG, "onBindViewHolder: Now Bind method executing");
}
#NonNull
#Override
public MyviewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View v= layoutInflater.inflate(R.layout.row_represent,parent,false);
MyviewHolder v1 = new MyviewHolder(v);
Log.d(TAG, "onCreateViewHolder: Now on create method executing");
return v1;
}
public static class MyviewHolder extends RecyclerView.ViewHolder{
TextView name;
TextView age;
public MyviewHolder(#NonNull View itemView) {
super(itemView);
name = (TextView)itemView.findViewById(R.id.names);
age = (TextView) itemView.findViewById(R.id.age);
}
}
}
DataClass
public class DataClass {
private String name;
private String age;
public DataClass(){
}
public DataClass(String name, String age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public String getAge() {
return age;
}
}
doubt in this parts
public DataClass(String name, String age) {
this.name = name;
this.age = age;
}
protected void onBindViewHolder(#NonNull MyviewHolder holder, int position, #NonNull DataClass model) {
holder.name.setText(model.getName());
holder.age.setText(model.getAge());
Log.d(TAG, "onBindViewHolder: Now Bind method executing");
}
because showing me that constructor never used
I'm not getting any errors but still my screen shows empty, Actually it is displaying null data
how to find it and guys tell me how to solve this issue
I know I made small mistake somewhere in this code help me to rectify it
Thank you.............................................
Replace this code
From
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
To
RecyclerView.LayoutManager layoutManager;
layoutManager = new LinearLayoutManager(this, RecyclerView.VERTICAL, false);
mRecyclerView.setLayoutManager(layoutManager);
Hope this work.
do u have any messages in firebase database or its empty?
you can add some messages through firebase so you can check it
I created tab layout with recyclerview (with fragments). Now I am trying to implement checkboxes, but I stuck with. I tried some solution online, but without any result. I added checkbox part into xml. Can you help me ?
RecyclerViewAdapter.java
public class RecyclerViewAdapter extends RecyclerView.Adapter <RecyclerViewAdapter.MyViewHolder>{
Context mContext;
List<RecTab1> mData;
public RecyclerViewAdapter(Context mContext, List<RecTab1> mData) {
this.mContext = mContext;
this.mData = mData;
}
#NonNull
#Override
public MyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v ;
v = LayoutInflater.from(mContext).inflate(R.layout.item_tab1,parent,false);
MyViewHolder vHolder = new MyViewHolder(v);
return vHolder;
}
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
holder.tv_name.setText(mData.get(position).getName());
holder.tv_subName.setText(mData.get(position).getSubName());
}
#Override
public int getItemCount() {
return mData.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
private TextView tv_name;
private TextView tv_subName;
public MyViewHolder (View itemView){
super (itemView);
tv_name = (TextView) itemView.findViewById(R.id.Item1Name);
tv_subName = (TextView) itemView.findViewById(R.id.Item1SubName);
}
}
}
item java file:
public class RecTab1 {
private String Name;
private String SubName;
public RecTab1(){
}
public RecTab1 (String name, String subName){
Name = name;
SubName = subName;
}
public String getName() {
return Name;
}
public String getSubName() {
return SubName;
}
public void setName(String name) {
Name = name;
}
public void setSubName(String subName) {
SubName = subName;
}
}
In your R.layout.item_tab1 add the checkbox:
<CheckBox android:id="#+id/cb_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check box text" />
Then define it in you viewholder:
public static class MyViewHolder extends RecyclerView.ViewHolder {
private TextView tv_name;
private TextView tv_subName;
private Checkbox cb_checkbox
public MyViewHolder (View itemView){
super (itemView);
tv_name = (TextView) itemView.findViewById(R.id.Item1Name);
tv_subName = (TextView) itemView.findViewById(R.id.Item1SubName);
cb_checkbox = (CheckBox) itemView.findViewById(R.id.cb_checkbox)
}
}
Then in you onBindViewHolder add the setOnClickListener to the checkbox:
#Override
public void onBindViewHolder(#NonNull MyViewHolder holder, int position) {
holder.tv_name.setText(mData.get(position).getName());
holder.tv_subName.setText(mData.get(position).getSubName());
holder.cb_checkbox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
//Your code goes here
}
}
Hope it helps. Here a source for more info.
I want to make a custom Adapter for my RecyclerView because I need to use a custom method inside who will init my List later when my presenter will be ready:
public void setList(List<Object> data){
this.data = data;
}
This is my not custom interface for my Adapter without implementation.
final class AdapterReviews extends RecyclerView.Adapter<AdapterReviews.ReviewViewHolder> {}
The question is how should be the interface for my custom Adapter?
**public class GetHolidaylistAdapter extends RecyclerView.Adapter<GetHolidaylistAdapter.ViewHolder> {
ArrayList<HashMap<String, String>> arrayList;
public GetHolidaylistAdapter(ArrayList<HashMap<String, String>> arrayList) {
this.arrayList = arrayList;
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView holiyDay_Date_Tv, holidayName_tv, holidayType_tv, day_tv;
public ViewHolder(View itemView) {
super(itemView);
holiyDay_Date_Tv = (TextView) itemView.findViewById(R.id.holiyDay_Date_Tv);
holidayName_tv = (TextView) itemView.findViewById(R.id.holidayName_tv);
holidayType_tv = (TextView) itemView.findViewById(R.id.holidayType_tv);
}
}
#Override
public int getItemCount() {
return arrayList.size();
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
GetHolidaylistAdapter.ViewHolder viewHolder = null;
Log.d(TAG, "Rv_Child_Active.." + viewType);
viewHolder = new GetHolidaylistAdapter.ViewHolder(LayoutInflater.from(parent.getContext()).
inflate(R.layout.holiday_child_rv, parent, false));
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
String date = arrayList.get(position).get(TAG_HOLIDAYDATE);
holder.holidayName_tv.setText(arrayList.get(position).get(TAG_HOLIDAYName));
holder.holiyDay_Date_Tv.setText(arrayList.get(position).get(TAG_HOLIDAYDATE));
holder.holidayType_tv.setText(arrayList.get(position).get(TAG_HOLIDAYtype));
***}
}
recycle_view=(RecyclerView)findViewById(R.id.recycle_view);
linearLayoutManager=new LinearLayoutManager(this);
recycle_view.setLayoutManager(linearLayoutManager);
adapter=new CustomAdapter(modelRecyclerArrayList);
recycle_view.setAdapter(adapter);
RecyclerView recycle_view;
LinearLayoutManager linearLayoutManager;
CustomAdapter adapter;
ArrayList<ModelRecycler> modelRecyclerArrayList = new ArrayList<>();*
A basic RecyclerView CustomAdapter looks like this
public class CustomAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private final Context context;
ArrayList<String> list = new ArrayList<>();
public CustomAdapter(Context context, ArrayList<String> list) { // you can pass other parameters in constructor
this.context = context;
this.list = list;
}
private class CustomAdapterItemView extends RecyclerView.ViewHolder {
final TextView textView;
CustomAdapterItemView(final View itemView) {
super(itemView);
textView = (TextView) itemView;
}
void bind(int position) {
textView.setText("");
}
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new CustomAdapterItemView(LayoutInflater.from(context).inflate(R.layout.item_color, parent, false));
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
((CustomAdapterItemView) holder).bind(position);
}
#Override
public int getItemCount() {
return list.size();
}
// Add your other methods here
}
You can check this. Here you have complete example of RecyclerView. It is all made by Google.
For some reason the on bindviewholder is not accepting the variables created by Viewholder(holder.name and holder.dexription). I have tried everything to fix it but nothing has worked Here is the code if you could help I would be grateful.
public class CityAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private ArrayList<City> cities;
public CityAdapter(ArrayList<City> cities) {
this.cities =cities;
}
#Override
public int getItemCount() {
if (cities != null) {
return cities.size();
} else {
return 0;
}
}
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent,int viewType){
View v =(View) LayoutInflater.from(parent.getContext()).inflate(R.layout.items,parent,false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder holder, int position) {
City city = cities.get(position);
holder.name.setText(city.getName());
holder.description.setText(city.getDescription());
}
static class ViewHolder extends RecyclerView.ViewHolder {
public View view;
public TextView name;
public TextView description;
public ImageView image;
public ViewHolder(#NonNull View itemView) {
super(itemView);
this.name=(TextView) itemView.findViewById(R.id.name);
this.description=(TextView) itemView.findViewById(R.id.description);
this.image=(ImageView) itemView.findViewById(R.id.image);
}
}
}
You are extending from wrong class
change CityAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
to
CityAdapter extends RecyclerView.Adapter<CityAdapter.ViewHolder>
And change your "onBindViewHolder" method's first argument from
RecyclerView.ViewHolder
to
ViewHolder
In my android project, I created Generic RecyclerView's Adapter class & Viewholder class like below,
Adapter class,
public class BaseRecyclerViewAdapter extends RecyclerView.Adapter<BaseViewHolder> {
private ItemClickListener itemClickListener;
private List<? extends Object> objectArrayList;
private int layout;
private BaseViewHolder baseViewHolder;
public BaseRecyclerViewAdapter(int layout, ItemClickListener itemClickListener, List<? extends Object> objectArrayList) {
this.layout = layout;
this.itemClickListener = itemClickListener;
this.objectArrayList = objectArrayList;
}
public BaseRecyclerViewAdapter(BaseViewHolder baseViewHolder, int layout,
ItemClickListener itemClickListener, List<? extends Object> objectArrayList) {
this.baseViewHolder = baseViewHolder;
this.layout = layout;
this.itemClickListener = itemClickListener;
this.objectArrayList = objectArrayList;
}
#NonNull
#Override
public BaseViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(layout, parent, false);
return new BaseViewHolder(itemView, itemClickListener);
}
#Override
public void onBindViewHolder(#NonNull BaseViewHolder holder, int position) {
}
#Override
public int getItemCount() {
return objectArrayList.size();
}
}
ViewHolder class
public class BaseViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener {
private CardView cardView;
private AppCompatImageView imgEdit;
private AppCompatImageView imgDelete;
private ItemClickListener itemClickListener;
public BaseViewHolder(View itemView, ItemClickListener itemClickListener) {
super(itemView);
cardView = itemView.findViewById(R.id.cardView);
imgEdit = itemView.findViewById(R.id.imgEdit);
imgDelete = itemView.findViewById(R.id.imgDelete);
imgDelete.setOnClickListener(this);
cardView.setOnClickListener(this);
cardView.setOnLongClickListener(this);
this.itemClickListener = itemClickListener;
}
#Override
public void onClick(View view) {
if (view.getId() == cardView.getId()) {
itemClickListener.onClick(view, getLayoutPosition(), ConstantCodes.ACTION_CLICK);
} else if (view.getId() == imgDelete.getId()) {
itemClickListener.onClick(view, getLayoutPosition(), ConstantCodes.ACTION_DELETE);
} else if (view.getId() == imgEdit.getId()) {
itemClickListener.onClick(view, getLayoutPosition(), ConstantCodes.ACTION_EDIT);
}
}
#Override
public boolean onLongClick(View view) {
itemClickListener.onLongClick(view, getLayoutPosition());
return false;
}
}
Here is the way I implement the above adapter & viewholder in my activity class
private class DispatchViewHolder extends BaseViewHolder {
AppCompatTextView txtInvoiceNo;
AppCompatTextView txtVehicleNo;
AppCompatTextView txtPartyName;
AppCompatTextView txtNoOfBags;
AppCompatTextView txtMaterialType;
AppCompatTextView txtWeight;
DispatchViewHolder(View itemView, ItemClickListener itemClickListener) {
super(itemView, itemClickListener);
}
}
private class DispatchMaterialAdapter extends BaseRecyclerViewAdapter {
ItemClickListener itemClickListener;
List<? extends Object> objectArrayList;
DispatchMaterialAdapter(ItemClickListener itemClickListener, List<? extends Object> objectArrayList) {
super(R.layout.dispatch_material_row, itemClickListener, objectArrayList);
this.itemClickListener = itemClickListener;
this.objectArrayList = objectArrayList;
}
#NonNull
#Override
public DispatchViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.dispatch_material_row, parent, false);
return new DispatchViewHolder(itemView, itemClickListener);
}
#Override
public void onBindViewHolder(#NonNull BaseViewHolder holder, int position) {
try {
DispatchViewHolder dispatchViewHolder = (DispatchViewHolder) holder;
DispatchMaterialMd dispatchMaterialMd = (DispatchMaterialMd) objectArrayList.get(position);
dispatchViewHolder.txtInvoiceNo.setText(dispatchMaterialMd.getInvoiceNo());
dispatchViewHolder.txtVehicleNo.setText(dispatchMaterialMd.getVehicleNo());
dispatchViewHolder.txtPartyName.setText(dispatchMaterialMd.getPartyName());
dispatchViewHolder.txtNoOfBags.setText(dispatchMaterialMd.getNoOfBags());
dispatchViewHolder.txtMaterialType.setText(dispatchMaterialMd.getMaterialType());
dispatchViewHolder.txtWeight.setText(dispatchMaterialMd.getWeight());
} catch (Exception e) {
e.printStackTrace();
}
}
#Override
public int getItemCount() {
return dispatchMaterialMds.size();
}
}
I have data in my objectArrayList list. I can print it in console, but nothing is coming in recyclerview. In recyclerview, it is only displaying empty Textview.
Following way, implement recyclerview,
layoutManager = new LinearLayoutManager(this);
dispatchMaterialAdapter =
new DispatchMaterialAdapter(itemClickListener, dispatchMaterialMds);
recycleListDetail.setLayoutManager(layoutManager);
recycleListDetail.setAdapter(dispatchMaterialAdapter);
dispatchMaterialMds is an arraylist of model class which is filled from the database's data.
Can you please help me, why my data is not getting displayed ?
You made BaseViewHolder that did the inflation then redone it in the DispatchViewHolder not sure if this will solve the problem but there was repeated code.
private class DispatchViewHolder extends BaseViewHolder {
AppCompatTextView txtInvoiceNo;
AppCompatTextView txtVehicleNo;
AppCompatTextView txtPartyName;
AppCompatTextView txtNoOfBags;
AppCompatTextView txtMaterialType;
AppCompatTextView txtWeight;
DispatchViewHolder(View itemView, ItemClickListener itemClickListener) {
super(itemView, itemClickListener);
}
}
private class DispatchMaterialAdapter extends BaseRecyclerViewAdapter {
ItemClickListener itemClickListener;
List<? extends Object> objectArrayList;
DispatchMaterialAdapter(ItemClickListener itemClickListener, List<? extends Object> objectArrayList) {
super(R.layout.dispatch_material_row, itemClickListener, objectArrayList);
this.itemClickListener = itemClickListener;
this.objectArrayList = objectArrayList;
}
#Override
public DispatchViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.dispatch_material_row, parent, false);
return new DispatchViewHolder(itemView, itemClickListener);
}
#Override
public void onBindViewHolder(#NonNull BaseViewHolder holder, int position) {
try {
DispatchViewHolder dispatchViewHolder = (DispatchViewHolder) holder;
DispatchMaterialMd dispatchMaterialMd = (DispatchMaterialMd) objectArrayList.get(position);
dispatchViewHolder.txtInvoiceNo.setText(dispatchMaterialMd.getInvoiceNo());
dispatchViewHolder.txtVehicleNo.setText(dispatchMaterialMd.getVehicleNo());
dispatchViewHolder.txtPartyName.setText(dispatchMaterialMd.getPartyName());
dispatchViewHolder.txtNoOfBags.setText(dispatchMaterialMd.getNoOfBags());
dispatchViewHolder.txtMaterialType.setText(dispatchMaterialMd.getMaterialType());
dispatchViewHolder.txtWeight.setText(dispatchMaterialMd.getWeight());
} catch (Exception e) {
e.printStackTrace();
}
}
}
In the DispatchViewHolder:
AppCompatTextView txtInvoiceNo;
AppCompatTextView txtVehicleNo;
AppCompatTextView txtPartyName;
AppCompatTextView txtNoOfBags;
AppCompatTextView txtMaterialType;
AppCompatTextView txtWeight;
DispatchViewHolder(View itemView, ItemClickListener itemClickListener) {
super(itemView, itemClickListener);
//Initiliazation of new view?
}
make change in set adapter code like below.
recycleListDetail.setLayoutManager(new LinearLayoutManager(this));
dispatchMaterialAdapter =
new DispatchMaterialAdapter(itemClickListener, dispatchMaterialMds);
recycleListDetail.setLayoutManager(layoutManager);
recycleListDetail.setAdapter(dispatchMaterialAdapter);
dispatchMaterialAdapter.notifyDataSetChanged();
and also check your adapter in bind view get the arraylist value and check arraylist type in getting and passing to adapter that same or not.
above discussion i am make demo for recycler view and is wroking using simple string array..
Adapter class.
public class RecyclerViewAdpater extends RecyclerView.Adapter {
List mStringList=new ArrayList<>();// hear you can pass any pojo class object.
Context mContext;
public RecyclerViewAdpater(List<String> mStringList, Context mContext) {
this.mStringList = mStringList;
this.mContext = mContext;
}
#Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_main,parent,false);
return new ItemViewHolder(view);
}
#Override
public void onBindViewHolder(ItemViewHolder holder, int position) {
String data=mStringList.get(position); // if you pass object of class then create that class object.
holder.textView.setText(data);
}
#Override
public int getItemCount() {
return mStringList.size();
}
public class ItemViewHolder extends RecyclerView.ViewHolder {
TextView textView;
public ItemViewHolder(View itemView) {
super(itemView);
textView=itemView.findViewById(R.id.timeData);
}
}
}
then implement activity like below code put in onCreate view method ...
recyclerViewAdpater=new RecyclerViewAdpater(mStingList,this);
mRvData.setLayoutManager(new LinearLayoutManager(this));
mRvData.setAdapter(recyclerViewAdpater);
chatAdapter.notifyDataSetChanged();
Please try this
#Override
public int getItemCount() {
return objectArrayList.size();
}