Android Studio : E/RecyclerView: No adapter attached; skipping layout - android-recyclerview

I've found only a few articles related to this issue on StackOverflow tried all of their solutions in various combinations but can't get the RecyclerView to display my RecyclerView.Adapter that is populated with data which a I can verify and see the list in debug mode but it simply won't show the information.
My code (I struggled to paste the XML Layout files):
public class RecipeDetailActivity extends AppCompatActivity {
private RecipeModel mRecipe;
private ArrayList<IngredientModel> mIngredientList = new ArrayList<>();
private IngredientListAdapter ingredientListAdapter;
private RecyclerView rvIngredientList;
private boolean mTwoPane;
private ListView listView;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipe_detail);
rvIngredientList = (RecyclerView)findViewById(R.id.rv_ingredient_list);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
rvIngredientList.setLayoutManager(layoutManager);
SampleRecycler sampleRecycler = new SampleRecycler(); // empty RecyclerView.Adapter
rvIngredientList.setAdapter(sampleRecycler);
mRecipe = getIntent().getParcelableExtra("com.wernerraubenheimer.bakingapp.data.RecipeModel");
mIngredientList = mRecipe.getIngredients();
ingredientListAdapter = new IngredientListAdapter(mIngredientList);
ingredientListAdapter.notifyDataSetChanged();
// Up until this point the RecyclerVie.Adapter is populated with data - debug mode I can see the list
rvIngredientList.swapAdapter(ingredientListAdapter, false);
}
}
public class IngredientListAdapter extends RecyclerView.Adapter<IngredientListAdapter.IngredientViewHolder> {
private ArrayList<IngredientModel> mIngredientList;
private Context mContext;
private boolean mTwoPane;
public IngredientListAdapter(ArrayList<IngredientModel> ingredientList) {
mIngredientList = ingredientList;
}
#Override
public IngredientViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
mContext = parent.getContext();
int layoutIdForListItem = R.layout.ingredient_list_item;
LayoutInflater inflater = LayoutInflater.from(mContext);
boolean attachImmediatelyToParent = false;
View view = inflater.inflate(layoutIdForListItem, parent, attachImmediatelyToParent);
IngredientViewHolder viewHolder = new IngredientViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(IngredientViewHolder holder, int position) {
IngredientModel ingredient = mIngredientList.get(position);
holder.bind(mContext, ingredient);
}
#Override
public int getItemCount() {
if (mIngredientList != null) {
return 0;
} else {
return mIngredientList.size();
}
}
static class IngredientViewHolder extends RecyclerView.ViewHolder {
private CardView cardviewContainer;
private TextView tvIngredientName;
private TextView tvIngredientQuantity;
private TextView tvIngredientMeasure;
private Context mContext;
private IngredientModel mIngredient;
IngredientViewHolder(View itemView) {
super(itemView);
//cardviewContainer = (CardView)itemView.findViewById(R.id.card_view_ingredient);
tvIngredientName = (TextView)itemView.findViewById(R.id.text_view_ingredient_name);
tvIngredientQuantity =(TextView)itemView.findViewById(R.id.text_view_quantity);
tvIngredientMeasure = (TextView)itemView.findViewById(R.id.text_view_measure);
}
void bind(Context context, IngredientModel ingredientModel) {
mContext = context;
mIngredient = ingredientModel;
tvIngredientName.setText(mIngredient.getIngredient());
tvIngredientQuantity.setText(mIngredient.getQuantity());
tvIngredientMeasure.setText(mIngredient.getMeasure());
}
}
}

Although, it is difficult to decipher from the code you have pasted,
it looks like the problem is with the following function:
#Override
public int getItemCount() {
if (mIngredientList != null) {
return 0;
} else {
return mIngredientList.size();
}
}
Changing condition to if (mIngredientList == null) { should solve the problem.
Also, in the RecipeDetailActivity.onCreate() method, only following code should be enough to display the list:
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipe_detail);
rvIngredientList = (RecyclerView) findViewById(R.id.rv_ingredient_list);
rvIngredientList.setLayoutManager(new LinearLayoutManager(this));
mRecipe = getIntent().getParcelableExtra("com.wernerraubenheimer.bakingapp.data.RecipeModel");
mIngredientList = mRecipe.getIngredients();
ingredientListAdapter = new IngredientListAdapter(mIngredientList);
rvIngredientList.setAdapter(ingredientListAdapter);
}

Related

How to implement an Action on Item Click in Order to show the result in CardView of RecyclerView

I am very new to Android programming. I can't find a solution for my current problem which I've been trying to solve for days.
I want to click on item of populated Array List and get the position i.e. the searched word result in Cardview (neither ItemClickListener nor OnSuggestionListener did work here. Here is my code of MainActivity and I would be very thankful if somebody could help me out:
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
SearchAdapter adapter;
MaterialSearchBar materialSearchBar;
List<String> suggestList = new ArrayList<>();
Database database;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//init View
recyclerView = (RecyclerView) findViewById(R.id.recycler_search);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
materialSearchBar = (MaterialSearchBar) findViewById(R.id.search_bar);
database = new Database(this);
materialSearchBar.setHint("Search");
materialSearchBar.setCardViewElevation(10);
loadSuggestList();
materialSearchBar.addTextChangeListener (new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
List<String> suggest = new ArrayList<>();
for (String search : suggestList) {
if (search.toLowerCase().contains(materialSearchBar.getText().toLowerCase()))
suggest.add(search);
}
materialSearchBar.setLastSuggestions(suggest);
}
#Override
public void afterTextChanged(Editable s) {
}
});
materialSearchBar.setOnSearchActionListener (new MaterialSearchBar.OnSearchActionListener() {
#Override
public void onSearchStateChanged(boolean enabled) {
if (!enabled)
adapter = new SearchAdapter(getBaseContext(), database.getLughats());
recyclerView.setAdapter(adapter);
}
#Override
public void onSearchConfirmed(CharSequence text) {
startSearch(text.toString());
}
#Override
public void onButtonClicked(int buttonCode) {
}
});
adapter = new SearchAdapter(this, database.getLughats());
recyclerView.setAdapter(adapter);
}
private void startSearch(String text) {
adapter = new SearchAdapter(this, database.getLughatByWort(text));
recyclerView.setAdapter(adapter);
}
private void loadSuggestList() {
suggestList = database.getWorts();
materialSearchBar.setLastSuggestions(suggestList);
}
}```
Let me know if I should post here also my AdapterCode for the ViewHolder.
Thank you for your help in advance!
Here ist my AdapterCode:
class SearchViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
RecyclerItemClick itemClickListener;
public TextView wort, praeteritum, partizip2, artikelGrammatik, uebersetzung1, uebersetzung2;
public ImageButton button1, button2;
public SearchViewHolder(#NonNull View itemView) {
super(itemView);
wort = (TextView)itemView.findViewById(R.id.wort);
praeteritum = (TextView)itemView.findViewById(R.id.praeteritum);
partizip2 = (TextView)itemView.findViewById(R.id.partizip2);
artikelGrammatik = (TextView)itemView.findViewById(R.id.artikelGrammatik);
uebersetzung1 = (TextView)itemView.findViewById(R.id.uebersetzung1);
uebersetzung2 = (TextView)itemView.findViewById(R.id.uebersetzung2);
button1 = (ImageButton)itemView.findViewById(R.id.button_id_1);
button2 = (ImageButton) itemView.findViewById(R.id.button_id_2);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
this.itemClickListener.onItemClickListener(v, getLayoutPosition());
}
public void setItemClickListener(RecyclerItemClick ic) {
this.itemClickListener = ic;
}
}
public class SearchAdapter extends RecyclerView.Adapter<SearchViewHolder> {
private Context context;
private List<Lughat> lughats;
public SearchAdapter(Context context, List<Lughat> lughats) {
this.context = context;
this.lughats = lughats;
}
#NonNull
#Override
public SearchViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
final View itemView = inflater.inflate(R.layout.layout_item, parent, false);
return new SearchViewHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull final SearchViewHolder holder, final int position) {
holder.wort.setText(lughats.get(position).getWort());
holder.praeteritum.setText(lughats.get(position).getPraeteritum());
holder.partizip2.setText(lughats.get(position).getPartizip2());
holder.artikelGrammatik.setText(lughats.get(position).getArtikelGrammatik());
holder.uebersetzung1.setText(lughats.get(position).getUebersetzung1());
holder.uebersetzung2.setText(lughats.get(position).getUebersetzung2());
CharSequence praet;
praet = holder.praeteritum.getText();
if (praet.length() == 0) {
holder.praeteritum.setVisibility(View.GONE);
holder.button1.setVisibility(View.GONE);
} else {
holder.praeteritum.setVisibility(View.VISIBLE);
holder.button1.setVisibility(View.VISIBLE);
}
CharSequence part2;
part2 = holder.partizip2.getText();
if (part2.length() == 0) {
holder.partizip2.setVisibility(View.GONE);
} else {
holder.partizip2.setVisibility(View.VISIBLE);
}
CharSequence artGr;
artGr = holder.artikelGrammatik.getText();
if (artGr.length() == 0) {
holder.artikelGrammatik.setVisibility(View.GONE);
holder.button2.setVisibility(View.GONE);
} else {
holder.artikelGrammatik.setVisibility(View.VISIBLE);
holder.button2.setVisibility(View.VISIBLE);
}
CharSequence ueb2;
ueb2 = holder.uebersetzung2.getText();
if (ueb2.length() == 0) {
holder.uebersetzung2.setVisibility(View.GONE);
} else {
holder.uebersetzung2.setVisibility(View.VISIBLE);
}
holder.button1.setOnClickListener(new ToastMaker(context.getApplicationContext()));
holder.button2.setOnClickListener(new ToastMaker(context.getApplicationContext()));
holder.setItemClickListener(new RecyclerItemClick() {
#Override
public void onItemClickListener(View v, int position) {
Toast.makeText(context, "Begriff", Toast.LENGTH_LONG).show();
}
});
}
#Override
public int getItemCount() {
return lughats.size();
}
}
If I do it also with OnClickListener it does not work too, it does not call the method startSearch.
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CharSequence text = materialSearchBar.getText();
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_LONG).show();
startSearch(text.toString());
}

how fix NullPointerException? [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
im new in android programing and i want to add JSON to my recyclerview i was getting the adapter not attetched skiping layout but my CustomAdapter is giving me NullPointerException too.
my CustomAdapter
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.CustomViewHolder> {
private List<RetroProduct> dataList;
private Context context;
CustomAdapter(Context context, List< RetroProduct > dataList){
this.context = context;
this.dataList = dataList;
}
class CustomViewHolder extends RecyclerView.ViewHolder {
final View mView;
TextView goodsId,categoryTitle,number,title1,title2;
CustomViewHolder(View itemView) {
super(itemView);
mView = itemView;
goodsId = mView.findViewById(R.id.tv_goodsId);
categoryTitle = mView.findViewById(R.id.tv_categoryTitle);
number = mView.findViewById(R.id.tv_number);
title1 = mView.findViewById(R.id.tv_title1);
title2 = mView.findViewById(R.id.tv_title2);
}
}
#NonNull
#Override
public CustomViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
View view = layoutInflater.inflate(R.layout.item_row, parent, false);
return new CustomViewHolder(view);
}
#Override
public void onBindViewHolder(CustomViewHolder holder, int position) {
holder.goodsId.setText(dataList.get(position).getGoodsId());
holder.categoryTitle.setText(dataList.get(position).getCategoryTitle());
holder.title1.setText(dataList.get(position).getTitle1());
holder.title2.setText(dataList.get(position).getTitle2());
holder.number.setText(dataList.get(position).getNumber());
}
#Override
public int getItemCount() {
return dataList.size();
}
}
and im getting NullPointerException on
return dataList.size();
what should i do now? give it value or i dont know.
MainActivity
public class MainActivity extends AppCompatActivity {
private CustomAdapter adapter;
private RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GetDataService service =
RetrofitClientInstance.getRetrofitInstance()
.create(GetDataService.class);
Call< List<RetroProduct> > call = service.getAllProducts();
call.enqueue(new Callback<List<RetroProduct>>() {
#Override
public void onResponse(Call<List<RetroProduct>> call,
Response<List<RetroProduct>> response) {
generateDataList(response.body());
LinearLayoutManager manager = new
LinearLayoutManager(MainActivity.this);
recyclerView.setLayoutManager(manager);
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
#Override
public void onFailure(Call<List<RetroProduct>> call, Throwable t)
{
Toast.makeText(MainActivity.this, "null",
Toast.LENGTH_SHORT).show();
}
});
}
private void generateDataList(List<RetroProduct> productList) {
recyclerView = findViewById(R.id.recycler_view);
adapter = new CustomAdapter(this,productList);
}
}
#Override
public int getItemCount() {
if(dataList!=null)
return dataList.size();
else
return 0;
}
Add a null check before calling size() method of a Arraylist as above

Unable to Pass Data from RecyclerView Adapter to Viewpager Adapter

I am creating simple image gallery app in which images are shown on MainActivity inside RecyclerView and on click of Any Image it will take us to SlideActivity in which I want to show images on ViewPager and then we swipe all images should be shown on ViewPager. For retrieving images i am using Glide. Images Load on RecyclerView Perfectly Fine but when i click on any Image i get this Exception "java.lang.ArrayIndexOutOfBoundsException: length=15; index=-1".
I know there are many same questions like this I saw all codes but did not understood so finally decided to ask question I am pasting code whatever i tried till now. Any Suggestion or advice will be helpful bcz I work alone I have no Mentor and I am Learning. Thank you!
Error :
java.lang.ArrayIndexOutOfBoundsException: length=15; index=-1
at java.util.ArrayList.get(ArrayList.java:439)
at com.starmoonsolutions.imagegallery.RecycleViewAdapter$ViewHolder$1.onClick(RecycleViewAdapter.java:63)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24697)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
ImageUrl.java
public class ImageUrl {
String imageUrl;
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
}
MainActivity:
public class MainActivity extends AppCompatActivity {
private ImageView imageView;
RecyclerView recyclerView;
GridLayoutManager gridLayoutManager;
ArrayList<ImageUrl> imageUrlList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
recyclerView = findViewById(R.id.recyclerView);
gridLayoutManager = new GridLayoutManager(getApplicationContext(), 2);
recyclerView.setLayoutManager(gridLayoutManager);
imageUrlList = prepareData();
RecycleViewAdapter galleryAdapter = new RecycleViewAdapter(getApplicationContext(), imageUrlList);
recyclerView.setAdapter(galleryAdapter);
}
private ArrayList prepareData(){
// here you should give your image URLs and that can be a link from the Internet
String imageUrls[] = {
"https://images.pexels.com/photos/358457/pexels-photo-358457.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=750&w=1260",
"https://images.pexels.com/photos/934964/pexels-photo-934964.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
"https://images.pexels.com/photos/1202887/pexels-photo-1202887.jpeg?auto=compress&cs=tinysrgb&h=650&w=940",
"https://images.pexels.com/photos/337909/pexels-photo-337909.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
"https://images.pexels.com/photos/894443/pexels-photo-894443.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
"https://images.pexels.com/photos/951325/pexels-photo-951325.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
"https://images.pexels.com/photos/1534411/pexels-photo-1534411.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
"https://images.pexels.com/photos/266436/pexels-photo-266436.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
"https://images.pexels.com/photos/1894350/pexels-photo-1894350.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
"https://images.pexels.com/photos/87009/earth-soil-creep-moon-lunar-surface-87009.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
"https://images.pexels.com/photos/1820563/pexels-photo-1820563.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940",
"https://images.pexels.com/photos/1274260/pexels-photo-1274260.jpeg?auto=compress&cs=tinysrgb&dpr=2&h=650&w=940"
};
imageUrlList = new ArrayList<>();
for (int i = 0; i < imageUrls.length; i++) {
ImageUrl imageUrl = new ImageUrl();
imageUrl.setImageUrl(imageUrls[i]);
imageUrlList.add(imageUrl);
}
Log.d("MainActivity", "List count: " + imageUrlList.size());
return imageUrlList;
}
}
RecyclerViewAdapter:
public class RecycleViewAdapter extends RecyclerView.Adapter<RecycleViewAdapter.ViewHolder>{
private ArrayList<ImageUrl> imageUrls;
private Context context;
public RecycleViewAdapter(Context context, ArrayList<ImageUrl> imageUrls) {
this.imageUrls = imageUrls;
this.context = context;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.image_layout, viewGroup, false);
return new ViewHolder(view);
}
/**
* gets the image url from adapter and passes to Glide API to load the image
*
* #param viewHolder
* #param i
*/
#Override
public void onBindViewHolder(#NonNull ViewHolder viewHolder, int i) {
Glide.with(context).load(imageUrls.get(i).getImageUrl()).into(viewHolder.img);
}
#Override
public int getItemCount() {
return imageUrls.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
RecyclerView recyclerView;
ImageView img;
public ViewHolder(#NonNull final View itemView) {
super(itemView);
img = itemView.findViewById(R.id.imageView);
recyclerView = itemView.findViewById(R.id.recyclerView);
final int pos = getAdapterPosition();
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, SlideActivity.class);
error point here-> intent.putExtra("image", String.valueOf(imageUrls.get(pos).getImageUrl())); <- error point here
intent.putExtra("position", pos);
intent.putExtra("list", imageUrls.get(pos).imageUrl);
context.startActivity(intent);
}
});
}
}
}
SlideActivity(ViewPager):
public class SlideActivity extends AppCompatActivity {
private ViewPager viewPager;
private Context context = SlideActivity.this;
private ViewPagerAdapter adapter;
private ArrayList<String> list;
String image;
private int position;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.slide_activity);
viewPager = findViewById(R.id.viewPager);
getIntentInfo();
adapter = new ViewPagerAdapter(list,context);
viewPager.setAdapter(adapter);
viewPager.setCurrentItem((position));
}
private void getIntentInfo() {
image = getIntent().getStringExtra("image");
list = getIntent().getStringArrayListExtra("list");
position = getIntent().getIntExtra("position",0);
}
}
ViewPagerAdapter:
public class ViewPagerAdapter extends PagerAdapter {
private ArrayList<String> imageUrls;
private android.content.Context context;
private LayoutInflater inflater;
private ImageView wallpaper;
public ViewPagerAdapter() {
}
public ViewPagerAdapter(ArrayList<String> imageUrls, android.content.Context context) {
this.imageUrls = imageUrls;
this.context = context;
}
#Override
public int getCount() {
return imageUrls.size();
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object o) {
return view.equals(o);
}
#NonNull
#Override
public Object instantiateItem(#NonNull final ViewGroup container, final int position) {
inflater = LayoutInflater.from(container.getContext());
View view = inflater.inflate(R.layout.pager_item, container, false);
wallpaper = view.findViewById(R.id.walpaperImage);
Glide.with(context).asBitmap().load(imageUrls.get(position)).into(wallpaper);
container.addView(view, 0);
return view;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((View) object);
}
}
The problem is actually in getting the adapter position.
public ViewHolder(#NonNull final View itemView) {
super(itemView);
img = itemView.findViewById(R.id.imageView);
recyclerView = itemView.findViewById(R.id.recyclerView);
// remove the below line
//final int pos = getAdapterPosition();
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, SlideActivity.class);
// you need to freshly get the adapter position here.
int pos = getAdapterPosition();
error point here-> intent.putExtra("image", String.valueOf(imageUrls.get(pos).getImageUrl())); <- error point here
intent.putExtra("position", pos);
intent.putExtra("list", imageUrls.get(pos).imageUrl);
context.startActivity(intent);
}
});
}
Since ViewHolder gets recycled, adapter position can change therefore you need to get the latest adapter position inside your click listener.
And regarding the Bundle data, pass the list of URLs of 'String' type instead of the custom object,
public void onClick(View v) {
Intent intent = new Intent(context, SlideActivity.class);
error point here-> intent.putExtra("image", String.valueOf(imageUrls.get(pos).getImageUrl())); <- error point here
intent.putExtra("position", pos);
// imageUrls is of type ArrayList<String>
intent.putStringArrayListExtra("list", imageUrls);
context.startActivity(intent); }
or else use the Parcelable library to serialize the custom object.

how to get value from one adapter to another adapter using recyclerview in android?

How to get value from one adapter to another adapter when clicking on the favorite button then it shows how many we clicked on favorite image button and also shows all data in another activity. Below is my Adapter Class & ViewHolder class (i.e AdapterFav.java & ContactViewHolder.java)
AdapterFav.java
public class AdapterFav extends
RecyclerView.Adapter<AdapterFav.ContactViewHolder>{
private ArrayList<Contactfav> contactfavList;
private static Context mContext;
public AdapterFav(ArrayList<Contactfav> contactfavList, Context applicationContext) {
this.contactfavList = contactfavList;
this.mContext = applicationContext;
}
#Override
public ContactViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.contact_row, null);
ContactViewHolder contactViewHolder = new ContactViewHolder(view);
return contactViewHolder;
}
#Override
public void onBindViewHolder(ContactViewHolder holder, int position) {
Contactfav contactfav = (Contactfav) contactfavList.get(position);
holder.tvContactName.setText(contactfav.getContactName());
holder.tvPhoneNumber.setText(contactfav.getContactNumber());
//Log.e("Id is", contactfav.getContactNumber());
holder.ivContactImage.setImageBitmap(contactfav.getContactImage());
holder.id=contactfav.getCid();
}
#Override
public int getItemCount() {
return contactfavList.size();
}
}
ContactViewHolder.java
public static class ContactViewHolder extends RecyclerView.ViewHolder {
ImageView ivContactImage;
ImageButton ibFavBtn;
TextView tvContactName;
TextView tvPhoneNumber;
Contactfav contactfav=new Contactfav();
String id;
public ContactViewHolder(View itemView) {
super(itemView);
ivContactImage = (ImageView) itemView.findViewById(R.id.ivContactImage);
tvContactName = (TextView) itemView.findViewById(R.id.tvContactName);
tvPhoneNumber = (TextView) itemView.findViewById(R.id.tvPhoneNumber);
ibFavBtn = (ImageButton) itemView.findViewById(R.id.fav);
ibFavBtn.setOnClickListener(new View.OnClickListener() {
Boolean b = true;
#Override
public void onClick(View view) {
if (b == true) {
ibFavBtn.setImageResource(R.drawable.star2);
Log.e("From get", id);
b = false;
} else {
ibFavBtn.setImageResource(R.drawable.star);
b = true;
}
}
});
}
}

how to set on click listener of recyclerview where list items are heterogenous

I have a recyclerview with 2 types of viewholders for showing 2 types of items
i want to implement on click listener on sub itmes in the items
existing solutions shows viewholder class should be implemented as static inside the adapter ,but i have declared them in sepearte file(a tutorial suggested),
So i cant implement onlick listener on the viewholders
my viewholder1 class is
public class ViewHolder1 extends RecyclerView.ViewHolder {
private TextView label1, label2;
public ImageView postImage;
public ViewHolder1(View v) {
super(v);
label1 = (TextView) v.findViewById(R.id.text11);
label2 = (TextView) v.findViewById(R.id.text22);
postImage = (ImageView) v.findViewById(R.id.dummyImage);
}
public TextView getLabel1() {
return label1;
}
public void setLabel1(TextView label1) {
this.label1 = label1;
}
public TextView getLabel2() {
return label2;
}
public void setLabel2(TextView label2) {
this.label2 = label2;
}
}
viewholder2 is
public class ViewHolder2 extends RecyclerView.ViewHolder {
private ImageView ivExample;
public ViewHolder2(View v) {
super(v);
ivExample = (ImageView) v.findViewById(R.id.ivExample);
}
public ImageView getImageView() {
return ivExample;
}
public void setImageView(ImageView ivExample) {
this.ivExample = ivExample;
}
}
adapter is
public class ComplexRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
// The items to display in your RecyclerView
private List<Object> items;
private final int USER = 0, IMAGE = 1;
// Provide a suitable constructor (depends on the kind of dataset)
public ComplexRecyclerViewAdapter(List<Object> items) {
this.items = items;
}
// Return the size of your dataset (invoked by the layout manager)
#Override
public int getItemCount() {
return this.items.size();
}
#Override
public int getItemViewType(int position) {
if (items.get(position) instanceof User) {
return USER;
} else if (items.get(position) instanceof String) {
return IMAGE;
}
return -1;
}
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
RecyclerView.ViewHolder viewHolder;
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
switch (viewType) {
case USER:
View v1 = inflater.inflate(R.layout.layout_viewholder1, viewGroup, false);
viewHolder = new ViewHolder1(v1);
break;
case IMAGE:
View v2 = inflater.inflate(R.layout.layout_viewholder2, viewGroup, false);
viewHolder = new ViewHolder2(v2);
break;
default:
View v3 = inflater.inflate(R.layout.layout_viewholder2, viewGroup, false);
viewHolder = new ViewHolder2(v3);
break;
}
return viewHolder;
}
#Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position) {
switch (viewHolder.getItemViewType()) {
case USER:
ViewHolder1 vh1 = (ViewHolder1) viewHolder;
configureViewHolder1(vh1, position);
break;
case IMAGE:
ViewHolder2 vh2 = (ViewHolder2) viewHolder;
configureViewHolder2(vh2);
break;
default:
ViewHolder2 vh3 = (ViewHolder2) viewHolder;
configureViewHolder2(vh3);
break;
}
}
// private void configureDefaultViewHolder(RecyclerViewSimpleTextViewHolder vh, int position) {
// vh.getLabel().setText((CharSequence) items.get(position));
// }
private void configureViewHolder1(ViewHolder1 vh1, int position) {
User user = (User) items.get(position);
if (user != null) {
vh1.getLabel1().setText(user.getMFName());
vh1.getLabel2().setText( user.getMName());
//vh1.postImage.setImageResource(R.drawable.universe);
Picasso.with(vh1.postImage.getContext())
.load(R.drawable.universe)
.resize(400,200) // resizes the image to these dimensions (in pixel)
.centerCrop()
.placeholder(R.drawable.progress_animation)
.error(R.drawable.error_image)
.into(vh1.postImage);
}
}
private void configureViewHolder2(ViewHolder2 vh2) {
// vh2.getImageView().setImageResource(R.drawable.sample_golden_gate);
Picasso.with(vh2.getImageView().getContext())
.load(R.drawable.sample_golden_gate)
.resize(400,200) // resizes the image to these dimensions (in pixel)
.centerCrop()
.placeholder(R.drawable.progress_animation)
.error(R.drawable.error_image)
.into(vh2.getImageView());
}
}
fragment that uses all
public class OneFragment extends Fragment {
private static final int VERTICAL_ITEM_SPACE = 48;
Button button;
RecyclerView rvMixed;
public OneFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_one, container, false);
// Lookup the recyclerview in activity layout
rvMixed = (RecyclerView) rootView.findViewById(R.id.rvContacts);
rvMixed.setAdapter(new ComplexRecyclerViewAdapter(getSampleArrayList()));
LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
// Control orientation of the items
// also supports LinearLayoutManager.HORIZONTAL
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
// Optionally customize the position you want to default scroll to
layoutManager.scrollToPosition(0);
// Attach layout manager to the RecyclerView
rvMixed.setLayoutManager(layoutManager);
//adding spacing between recycler view items
// rvMixed.addItemDecoration(new VerticalSpaceItemDecoration(VERTICAL_ITEM_SPACE));
// That's all!
return rootView;
}
private void bindDataToAdapter() {
// Bind adapter to recycler view object
rvMixed = (RecyclerView) getView().findViewById(R.id.rvContacts);
ComplexRecyclerViewAdapter mAdapter = new ComplexRecyclerViewAdapter(getSampleArrayList());
rvMixed.setAdapter(mAdapter);
}
private ArrayList<Object> getSampleArrayList() {
ArrayList<Object> items = new ArrayList<>();
items.add(new User("Dany Targaryen", "Valyria"));
items.add(new User("Rob Stark", "Winterfell"));
items.add("image");
items.add(new User("Jon Snow", "Castle Black"));
items.add("image");
items.add(new User("Tyrion Lanister", "King's Landing"));
return items;
}
}
you can simply pass the click Listener on ComplexRecyclerViewAdapter through its constructor at the time of its initialization. And then you can reuse the same click listener reference for each of the ViewHolder.