Reputation: 423
I am trying to put ads in a RecyclerView which is currently parsing a JSON file, the problem is that the ArrayList is throwing a casting error when I try to fill it with ads or the data from JSON.
Errors occur at two places, one in the RecyclerViewAdapter()
method body and other one in the BindViewHolder()
.
Here is my code from RecyclerViewAdapter.java (Error commented in 5th line)
private ArrayList<Object> arrayList = new ArrayList<>();
public RecyclerViewAdapter(Context context, ArrayList<CardSetterGetter> arrayList, ArrayList<String> favouriteItemList, int totalCount) {
this.context = context;
this.arrayList = arrayList; //Incompatible types. Required: ArrayList<java.lang.object>; found: ArrayList<com.comp.app.modal.CardSetterGetter>
this.favouriteItemList = favouriteItemList;
this.totalCount = totalCount;
}
onBindViewHolder()
(Error commented in 27th line)
@Override
public void onBindViewHolder(final RecyclerViewHolder holder, final int position) {
int viewType = getItemViewType(position);
switch (viewType){
case AD_VIEW_TYPE:
NativeExpressAdViewHolder nativeExpressHolder = (NativeExpressAdViewHolder)holder;
NativeExpressAdView adView = (NativeExpressAdView)arrayList.get(position);
ViewGroup adCardView = (ViewGroup)nativeExpressHolder.itemView;
adCardView.removeAllViews();
if(adView.getParent() != null){
((ViewGroup)adView.getParent()).removeView(adView);
}
adCardView.addView(adView);
break;
case MENU_ITEM_VIEW_TYPE:
default:
String card_image;
CardSetterGetter cardSetterGetter;
cardSetterGetter = arrayList.get(position); //Incompatible types. Required: ArrayList<com.comp.app.modal.CardSetterGetter>; found: ArrayList<java.lang.object>;
card_image = constants.SERVERIP.concat(cardSetterGetter.getImageurl());
holder.cardSetterGetter = cardSetterGetter;
holder.title.setText(cardSetterGetter.getImagetitle()); //set title
PointF focusPoint = new PointF(0.5f, 0f);
// your app populates the focus point
holder.image // set image
.getHierarchy()
.setActualImageFocusPoint(focusPoint);
holder.image.setImageURI(Uri.parse(card_image));
}
}
Here is my CardSetterGetter.java
public class CardSetterGetter implements Serializable {
private String imagetitle ;
private String imageurl;
private String description;
private String tag;
private boolean isFavorite;
public String getImageId() {
return imageId;
}
public void setImageId(String imageId) {
this.imageId = imageId;
}
public String getFavourite() {
return favourite;
}
public void setFavourite(String favourite) {
this.favourite = favourite;
}
private String imageId ;
private String favourite ;
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String category;
public CardSetterGetter(){
}
public String getImagetitle() {
return imagetitle;
}
public void setImagetitle(String imagetitle) {
this.imagetitle = imagetitle;
}
public String getImageurl() {
return imageurl;
}
public void setImageurl(String imageurl) {
this.imageurl = imageurl;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getTag() {
return tag;
}
public void setTag(String tag) {
this.tag = tag;
}
public CardSetterGetter(String imageurl , String imagetitle , String description , String category , String tag,String imageId ,String favourite){
this.imageurl = imageurl ;
this.imagetitle = imagetitle ;
this.description = description ;
this.tag = tag ;
this.category = category ;
this.isFavorite =false ;
this.imageId = imageId ;
this.favourite = favourite ;
}
public boolean isFavorite() {
return isFavorite;
}
public void setFavorite(String favorite) {
favourite = favorite;
}
}
I tried some of the questions on StackOverFlow like this one (Question), but I am unable to understand the basic concept of the solution, I already changed the datatype to Object (from CardSetterGetter).
Also tell me what happens if this issue of casting is solved and the arrayList returns adView where CardSetterGetter is expected or vice-versa, Do I need to put all such statements in try catch blocks?
Upvotes: 2
Views: 2089
Reputation: 37404
Issue : your List is containing two kind of items CardSetterGetter
and NativeExpressAdView
hence you need to use List of Objects
instead of any specific type because both types has no relation plus apply the casting at appropriate places
use this
public RecyclerViewAdapter(Context context, ArrayList<Object> arrayList, ArrayList<String> favouriteItemList, int totalCount) {
and use casting
cardSetterGetter =(CardSetterGetter ) arrayList.get(position);
instead of
public RecyclerViewAdapter(Context context, ArrayList<CardSetterGetter> arrayList, ArrayList<String> favouriteItemList, int totalCount) {
Also tell me what happens if this issue of casting is solved and the arrayList returns adView where CardSetterGetter is expected or vice-versa, Do I need to put all such statements in try catch blocks?
getItemViewType(position)
is working as per position
// ITEMS_PER_AD = 8
return (position % MainActivity.ITEMS_PER_AD == 0) ? NATIVE_EXPRESS_AD_VIEW_TYPE
: MENU_ITEM_VIEW_TYPE;
mean result of position % 8 == 0
item (0,8,16,24,32...) in your list will be of NATIVE_EXPRESS_AD_VIEW_TYPE
type so it's position
dependent math.
Upvotes: 3
Reputation: 2847
Change your ArrayList<Object> arrayList
in your RecyclerViewAdapter.java
private ArrayList<Object> arrayList = new ArrayList<>();
to
private ArrayList<CardSetterGetter> arrayList = new ArrayList<>();
this will solve your problem
Upvotes: 0
Reputation: 463
The problem is in your member definition:
private ArrayList<Object> arrayList = new ArrayList<>();
It probably should be:
private ArrayList<CardSetterGetter> arrayList = new ArrayList<>();
The issue here is that ArrayList<CardSetterGetter>
is not assignable to ArrayList<Object>
- although CardSetterGetter
(and all objects) are assignable to Object
.
Upvotes: 1
Reputation: 5017
change arraylist initialization like this
private ArrayList<CardSetterGetter> arrayList = new ArrayList<>();
Upvotes: 0