I have this horizontal recyclerview inside a tab layout.
Things I am sure of:
1. The list is not empty and is passed correctly.
2. The adapter constructor method is called.
3. The fragment is called.
The adapter code:
public class ProjectAdapter extends RecyclerView.Adapter<ProjectAdapter.ProjectViewHolder> {
ArrayList<ProjectModel> project;
public ProjectAdapter(ArrayList<ProjectModel> project) {
this.project = project;
}
public static class ProjectViewHolder extends RecyclerView.ViewHolder {
TextView title, desc;
public ProjectViewHolder(View itemView) {
super(itemView);
title = (TextView) itemView.findViewById(R.id.proj_element_title);
desc = (TextView) itemView.findViewById(R.id.proj_element_desc);
}
}
#Override
public ProjectViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.projects_element, parent, false);
return new ProjectViewHolder(view);
}
#Override
public void onBindViewHolder(ProjectViewHolder holder, int position) {
//holder.imageView.setImageResource(projectModel.getImg_res());
holder.title.setText(project.get(position).getTitle());
holder.desc.setText(project.get(position).getDesc());
}
#Override
public int getItemCount() {
return project.size();
}
}
I added debugging points at
holder.title.setText(project.get(position).getTitle());
holder.desc.setText(project.get(position).getDesc());
return project.size();
but they were never called.
What could be the problem?
Related
I have two adapters that adapt to a recycler view depending on different conditions. I want to merge them.
The issue is that I created first in the same file as the activity where it is being used and the other is in a new file.
Take it as follows:
DefaultAdapter is in the file where it is being used.
AutoCompleteAdapter is also used in the same file but is declared in other file.
I want to get rid of Default Adapter and get its funtions into AutoCompleteAdapter.
There are some issues to it: I can no more use the ParentFiles' variables.
Below is the adapter that I want to get rid of.
public class DefaultAdapter extends RecyclerView.Adapter<DefaultAdapter.ViewHolder> {
private Context context;
private AdapterView.OnItemClickListener onItemClickListener;
List<String> searchList;
#NonNull #Override
public DefaultAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = (View) LayoutInflater.from(parent.getContext())
.inflate(android.R.layout.simple_list_item_1, null);
ViewHolder holder = new ViewHolder(view);
return holder;
}
#Override public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.txt.setText(
Html.fromHtml(getItem(position)).toString());///need to get rid of this error
}
#Override public int getItemCount() {
return searchList.size();
}
#NonNull public String getItem(int position) {
return searchList.get(position);
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView txt;
public ViewHolder(#NonNull View itemView) {
super(itemView);
txt = itemView.findViewById(android.R.id.text1);
itemView.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
String title = txt.getText().toString();
searchPresenter.saveSearch(title);
}
});
itemView.setOnLongClickListener(new View.OnLongClickListener() {
#Override public boolean onLongClick(View v) {
String searched = txt.getText().toString();
deleteSpecificSearchDialog(searched);
return true;
}
});
}
}
}
Please take note of the SearchPresenter. This is a variable in the parent file, that is causing major issues. Autocomplete adapter doesn't have to use it, but default have to.
This is the adapter I want it to merge into
public class AutoCompleteAdapter extends RecyclerView.Adapter<AutoCompleteAdapter.ViewHolder>
implements Filterable {
#Inject JNIKiwix currentJNIReader;
#Inject SharedPreferenceUtil sharedPreferenceUtil;
#Inject ZimReaderContainer zimReaderContainer;
private List<String> data;
private KiwixFilter kiwifilter;
private Context context;
public AutoCompleteAdapter(Context context) {
this.context = context;
data = new ArrayList<>();
kiwifilter = new KiwixFilter();
setupDagger();
}
private void setupDagger() {
CoreApp.getCoreComponent().inject(this);
}
#NonNull #Override public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = (View) LayoutInflater.from(parent.getContext())
.inflate(android.R.layout.simple_list_item_1, null);
ViewHolder holder = new ViewHolder(view);
return holder;
}
#Override public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.title.setText(Html.fromHtml(getItem(position)).toString());
}
#Override
public String getItem(int index) {
String a = data.get(index);
if (a.endsWith(".html")) {
String trim = a.substring(2);
trim = trim.substring(0, trim.length() - 5);
return trim.replace("_", " ");
} else {
return a;
}
}
#Override public int getItemCount() {
return data.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView title;
public ViewHolder(#NonNull View itemView) {
super(itemView);
title = itemView.findViewById(android.R.id.text1);
}
}
}
...
Note the similarity between the two, they are almost similar, autocompleteadapter is like superset of defaultadapter.
I just want to get rid of the default adapter.
Note: I have chopped off some other things from autocompeleteadapter, I think they are not required for the problem.
Noob here...
Thanks
Why am I getting random result on item click? My error is inside the btn onClick Listener but what shows in my TextView (reference_number) is fine. when button clicked - it will open another activity to show more details about it but it shows random result, after I scroll my recyclerView it will change the result again. ## im confused already, below is all the codes inside my Adapter
What's the cause of this error and where did I get this wrong?
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>{
int i;
#NonNull
#Override
public RecyclerViewAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_history_pending, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull RecyclerViewAdapter.ViewHolder holder, final int position) {
i = holder.getAdapterPosition();
holder.reference_number.setText("#"+mainActivity.myBookings.getCargoBooking().get(i).getReferenceNumber());
holder.btn_details.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String fee;
fee = isNull(mainActivity.myBookings.getCargoBooking().get(i).getCargoItem().get(0).getCargoFee().toString());
Intent i = new Intent(mainActivity.context, BookingDetailsActivity.class);
i.putExtra("fee", fee);
startActivity(i);
}
});
}
private String isNull(String s){
if (s != null){
return s;
} else return "";
}
#Override
public int getItemCount() {
return mainActivity.myBookings.getCargoBooking().size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView reference_number, driver_schedule;
Button btn_details;
public ViewHolder(View itemView) {
super(itemView);
reference_number = itemView.findViewById(R.id.tv_pending_trackNum);
driver_schedule = itemView.findViewById(R.id.tv_view_driver_schedule);
btn_details = itemView.findViewById(R.id.btn_pending_view);
}
}
}
Your problem is the "i" variable in your adapter scope, remove it and declare it as local (inside your onBindViewHolder method) and it should work.
That's because your instance variable i keeps changing whenever onBindViewHolder is called, say when the RecyclerView is being scrolled for example. Instead, change the variable i to local:
#Override
public void onBindViewHolder(#NonNull RecyclerViewAdapter.ViewHolder holder, final int position) {
final int i = holder.getAdapterPosition();
Is it possible to call same adapter in two activity for different different work
Here is my RecyclerAdapter:
Only difference when calling from two activity is in this line: From Activity1.java it is:
holder.Name.setText(arrayList.get(position).getName());
And from Activity2.java
holder.Name.setText(arrayList.get(position).getVehicle());
All other code is same how can I use same adapter for different different work.
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private ArrayList<Contact> arrayList= new ArrayList<>();
public RecyclerAdapter(ArrayList<Contact> arrayList){
this.arrayList= arrayList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view,parent,false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder (MyViewHolder holder, int position) {
holder.Name.setText(arrayList.get(position).getName());
int sync_status = arrayList.get(position).getSync_status();
if(sync_status== DbContact.SYNC_STATUS_OK){
holder.Sync_Status.setImageResource(R.drawable.success);
}
else {
holder.Sync_Status.setImageResource(R.drawable.stopwatch);
}
}
#Override
public int getItemCount() {
return arrayList.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
ImageView Sync_Status;
TextView Name;
public MyViewHolder(View itemView) {
super(itemView);
Sync_Status=(ImageView)itemView.findViewById(R.id.imgSync);
Name=(TextView)itemView.findViewById(R.id.txtName);
}
}
}
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private ArrayList<Contact> arrayList= new ArrayList<>();
private int whichActivity;
public RecyclerAdapter(ArrayList<Contact> arrayList, int activity){
whichActivity = activity;
this.arrayList= arrayList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view,parent,false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder (MyViewHolder holder, int position) {
if(whichActivity == 0) {
holder.Name.setText(arrayList.get(position).getName());
}
else {
holder.Name.setText(arrayList.get(position).getVehicle());
}
int sync_status = arrayList.get(position).getSync_status();
if(sync_status== DbContact.SYNC_STATUS_OK){
holder.Sync_Status.setImageResource(R.drawable.success);
}
else {
holder.Sync_Status.setImageResource(R.drawable.stopwatch);
}
}
#Override
public int getItemCount() {
return arrayList.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
ImageView Sync_Status;
TextView Name;
public MyViewHolder(View itemView) {
super(itemView);
Sync_Status=(ImageView)itemView.findViewById(R.id.imgSync);
Name=(TextView)itemView.findViewById(R.id.txtName);
}
}
}
Try this code. Now when you create your RecyclerAdapter in Activity1 call new RecyclerAdapter(arrayList, 0) and when you create your RecyclerAdapter in Activity2 call new RecyclerAdapter(arrayList, 1). You are just passing a variable into the constructor so your adapter knows which activity it is in and can run through different logic depending on the activity.
I have to two data sets with different adapters inside a single activity and I want to use notifyDataSetChanged() however its not working for the other adapter. So inside my adapterFirst class I have LongCLickListener which is using notifyDataSetChanged() and I want to call the adapterSecond to notify too if adapterFirst is updated. The AdapterSecond data is not being updated when pressing LongClick
public class AdapterFirst extends RecyclerView.Adapter <AdapterFirst .ViewHolder> {
private List<Object> objectList;
private Context mContext;
AdapterSecond adapterSecond;
public AdapteFirst(List<Object> list) { objectList= list; }
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_list, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
//query method
notifyDataSetChanged(); //working
//here it gets the nullpointerexception error
adapterSecond = new AdapteSecond();
adapterSecond.notifyDataSetChanged();
}
});
}
#Override
public int getItemCount() {
return (objectList != null? objectList.size():0);
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView data;
public ViewHolder(View itemView) {
super(itemView);
data= (TextView) itemView.findViewById(R.id.text);
}
}
}
AdapterSecond
public class AdapterSecond extends
RecyclerView.Adapter <AdapterSecond.ViewHolder> {
private List<Object> objectList;
private Context mContext;
public AdapterSecond () {}
public AdapterSecond (List<Object> list) { objectList= list; }
#Override
public AdapterSecond.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_list_2, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final AdapterSecond.ViewHolder holder, int position) {
final Object object = objectList.get(holder.getAdapterPosition());
holder.data2.setText(object.getData());
}
#Override
public int getItemCount() {
return (objectList != null? objectList.size():0);
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView data2;
public ViewHolder(View itemView) {
super(itemView);
data2= (TextView) itemView.findViewById(R.id.text2);
}
}
}
Seems like the order of initialization of the adapters have some issues.
Just make sure that you have initialize the secondAdapter before the firstAdapter
secondAdapter= new SecondAdapter();
firstAdapter = new FirstAdapter();
Just check the order in which they initialize!
I have a recyclerview . I click on a item. It is highlighted ( sets background - color) . And then, I click another item , It doesn't remove old highlighted view. My class is very leak . Also could you give me an advice to make more effective ?
public class DrawerMenuShelfListAdapter extends RecyclerView.Adapter<DrawerMenuShelfListAdapter.ViewHolder> implements View.OnClickListener {
private int selectedRow = 0;
public Fragment fragment;
public ViewHolder holder;
public RecyclerView recyclerView;
private ArrayList<ShelfModel> shelfList;
public DrawerMenuShelfListAdapter(Fragment fragment, ArrayList<ShelfModel> shelfList, RecyclerView recyclerView) {
this.shelfList = shelfList;
this.fragment = fragment;
this.recyclerView = recyclerView;
}
public void setItemSelected(int position){
recyclerView.getChildAt(selectedRow).setBackgroundColor(fragment.getActivity().getResources().getColor(android.R.color.transparent));
notifyItemChanged(selectedRow);
selectedRow = position;
recyclerView.getChildAt(selectedRow).setBackgroundColor(fragment.getActivity().getResources().getColor(android.R.color.black));
notifyItemChanged(selectedRow);
}
public void updateList(ArrayList<ShelfModel> list){
shelfList.clear();
shelfList.addAll(list);
notifyDataSetChanged();
notifyItemChanged(selectedRow);
}
#Override
public DrawerMenuShelfListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext())
.inflate(R.layout.navigation_drawer_list_row, parent, false);
holder = new ViewHolder(v);
holder.text = (TextView) v.findViewById(R.id.definitionText);
holder.total = (TextView) v.findViewById(R.id.countText);
holder.editButton = (ImageButton)v.findViewById(R.id.editButton);
holder.text.setTypeface(App.MUSEO_100);
holder.total.setTypeface(App.MUSEO_300);
return holder;
}
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
final ShelfModel shelfModel = shelfList.get(position);
holder.text.setText(shelfModel.getName());
holder.total.setText(String.valueOf(shelfModel.getTotal()));
holder.shelfId = shelfModel.getShelfId();
holder.itemView.setOnClickListener(this);
}
#Override
public int getItemCount() {
return shelfList.size();
}
#Override
public void onClick(View v) {
int position = recyclerView.getChildLayoutPosition(v);
((DrawerMenuShelfListListener)fragment).onShelfItemClick(shelfList.get(position));
setItemSelected(position);
}
public interface DrawerMenuShelfListListener extends BaseRecyclerViewListener {
void onShelfItemClick(ShelfModel shelfModel);
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView text;
public TextView total;
public ImageView editButton;
public int shelfId;
public ViewHolder(View view) {
super(view);
}
}
}
Ok. I found the solution.If I don't call notifyItemChanged , it works