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.
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 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.
I am bumbing my head on trying to implement a ClickListener in an adapter for a DialogFragment.
I am getting the error on the codeline:
holder.bind(tzmodel.get(position), listener);
Error says (bind() in ViewHolder cannot be applied). Seems it does not match the ViewHolder code.
Could somebody please assist?
The code for my timeZoneAdapter:
public class timeZoneAdapter extends RecyclerView.Adapter<timeZoneAdapter.ViewHolder> {
public interface OnItemClickListener {
void onItemClick(TimezoneModel item);
}
private Context c;
private List<TimezoneModel> tzmodel;
private final OnItemClickListener listener;
public timeZoneAdapter(Context c, List<TimezoneModel> tzModel, OnItemClickListener listener) {
this.c = c;
this.tzmodel = tzModel;
this.listener = listener;
}
//INITIALIE TZ
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.timezonemodel, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.bind(tzmodel.get(position), listener); <-- error
}
#Override
public int getItemCount() {
return tzmodel.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView timeTxt, currenttimeTxt, regionTxt;
public ViewHolder(View itemView) {
super(itemView);
timeTxt= (TextView) itemView.findViewById(R.id.timeTxt);
currenttimeTxt= (TextView) itemView.findViewById(R.id.currentTimeTxt);
regionTxt= (TextView) itemView.findViewById(R.id.regionTxt);
}
public void bind(ViewHolder viewHolder, int i, final OnItemClickListener listener) {
viewHolder.timeTxt.setText(tzmodel.get(i).getTime());
viewHolder.currenttimeTxt.setText(tzmodel.get(i).getCurrentTime());
viewHolder.regionTxt.setText(tzmodel.get(i).getRegion());
itemView.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
listener.onItemClick((TimezoneModel) tzmodel);
}
});
}
}
}
The problem is the parameters that you put in the bind method that you define.
You define the public method bind in the ViewHolder class in this way:
public void bind(ViewHolder viewHolder, int i, final OnItemClickListener listener){
//...
}
You can fix your compile error change the call to :
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.bind(holder,position, listener);
}
I also share with you a couple of correction because if you create a public method in the ViewHolder class, you don't need to recipe the viewHolder instance because you can access with this operator:
public class TimeZoneAdapter extends RecyclerView.Adapter<TimeZoneAdapter.ViewHolder> {
public interface OnItemClickListener {
void onItemClick(TimezoneModel item);
}
private Context c;
private List<TimezoneModel> tzmodel;
private final OnItemClickListener listener;
public TimeZoneAdapter(Context c, List<TimezoneModel> tzModel, OnItemClickListener listener) {
this.c = c;
this.tzmodel = tzModel;
this.listener = listener;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.bind(tzmodel.get(position), listener);
}
#Override
public int getItemCount() {
return tzmodel.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView timeTxt, currenttimeTxt, regionTxt;
public ViewHolder(View itemView) {
super(itemView);
timeTxt= itemView.findViewById(R.id.timeTxt);
currenttimeTxt= itemView.findViewById(R.id.currentTimeTxt);
regionTxt= itemView.findViewById(R.id.regionTxt);
}
public void bind(TimezoneModel item , final OnItemClickListener listener) {
this.timeTxt.setText(item.getTime());
this.currenttimeTxt.setText(item.getCurrentTime());
this.regionTxt.setText(item.getRegion());
itemView.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
listener.onItemClick((TimezoneModel) tzmodel);
}
});
}
}
}
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
there are two recyclerview and two adapters. Both recyclerview contain text fields and buttons. I have below pressing in one recyclerview item jumped to another recyclerview and vice versa. How to do it?
First adapter:
public class RVAdapterStart extends RecyclerView.Adapter<RVAdapterStart.TaskViewHolder> {
public static class TaskViewHolder extends RecyclerView.ViewHolder {
RelativeLayout relativeLayout;
private TextView title;
private TextView status;
private Button button;
TaskViewHolder(View view) {
super(view);
relativeLayout = (RelativeLayout) view.findViewById(R.id.frameLayout);
title = (TextView) view.findViewById(R.id.title);
status = (TextView) view.findViewById(R.id.status);
button = (Button) view.findViewById(R.id.tasks_finish);
}
}
private List<Task> tasksStart;
RVAdapterStart() {
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public void onBindViewHolder(TaskViewHolder holder, final int position) {
holder.title.setText(tasksStart.get(position).getTitle());
holder.status.setText(tasksStart.get(position).getStatus());
holder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
#Override
public int getItemCount() {
return tasksStart.size();
}
#Override
public TaskViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_tasks_start, viewGroup, false);
TaskViewHolder tvh = new TaskViewHolder(view);
return tvh;
}
Second adapter:
public class RVAdapterProcces extends RecyclerView.Adapter<RVAdapterProcces.TasksViewHolder> {
public static class TasksViewHolder extends RecyclerView.ViewHolder {
RelativeLayout relativeLayout;
private TextView title;
private TextView status;
private Button TaskFinishBtn;
private Button TaskCancelBtn;
TasksViewHolder(View view) {
super(view);
relativeLayout = (RelativeLayout) view.findViewById(R.id.relative_tasks_procces);
title = (TextView) view.findViewById(R.id.title_procces);
status = (TextView) view.findViewById(R.id.status_procces);
TaskFinishBtn = (Button) view.findViewById(R.id.tasks_finish);
TaskCancelBtn = (Button) view.findViewById(R.id.tasks_procces_cancel);
}
}
private List<Task> tasksProcces = new ArrayList<Task>();
RVAdapterProcces() {
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public void onBindViewHolder(TasksViewHolder holder, final int position) {
holder.title.setText(tasksProcces.get(position).getTitle());
holder.status.setText(tasksProcces.get(position).getStatus());
}
#Override
public int getItemCount() {
return tasksProcces.size();
}
#Override
public TasksViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_tasks_procces, viewGroup, false);
TasksViewHolder tvh = new TasksViewHolder(view);
return tvh;
}