Picasso does not load in android studio - android

i receive NPE when i want to show pics in picasso,
pay attention that i've another adapter which i used picasso,
but in my new adapter this npe error for boolean happens,
and my friends used this code and it performed well,
here is the code that error says
public boolean isVideoNews() {
return !image.isEmpty() && !video.isEmpty() ;
}
and the part that is related to this code in my adapter
videoIndicator.setVisibility(news.isVideoNews() ? View.VISIBLE : View.GONE);
my adapter
public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.NewsViewHolder> {
private List<News> newsList;
public NewsAdapter(List<News> newsList) {
this.newsList = newsList;
}
#Override
public NewsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new NewsViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_news, parent, false));
}
#Override
public void onBindViewHolder(NewsViewHolder holder, int position) {
holder.bindNews(newsList.get(position));
}
#Override
public int getItemCount() {
return newsList.size();
}
class NewsViewHolder extends RecyclerView.ViewHolder {
private ImageView newsImageView;
private TextView titleTextView;
private TextView dateTextView;
private View videoIndicator;
public NewsViewHolder(View itemView) {
super(itemView);
newsImageView = itemView.findViewById(R.id.iv_news_image);
videoIndicator = itemView.findViewById(R.id.iv_news_VideoIndicator);
titleTextView = itemView.findViewById(R.id.tv_news_title);
dateTextView = itemView.findViewById(R.id.tv_news_date);
}
public void bindNews(News news) {
Picasso.get().load(news.getImage()).into(newsImageView);
videoIndicator.setVisibility(news.isVideoNews() ? View.VISIBLE : View.GONE);
titleTextView.setText(news.getTitle());
dateTextView.setText(news.getDate());
}
}
}
and this is my data model
public class News {
private int id;
private String title;
private String content;
private String date;
private String image;
private String video;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getVideo() {
return video;
}
public void setVideo(String video) {
this.video = video;
}
public boolean isVideoNews() {
return !image.isEmpty() && !video.isEmpty() ;
}
}
Error
FATAL EXCEPTION: main
Process: com.example.melal.newsapp, PID: 5958
java.lang.IllegalArgumentException: Path must not be empty.
at com.squareup.picasso.Picasso.load(Picasso.java:332)
at com.example.melal.newsapp.home.NewsAdapter$NewsViewHolder.bindNews(NewsAdapter.java:53)
at com.example.melal.newsapp.home.NewsAdapter.onBindViewHolder(NewsAdapter.java:31)
at com.example.melal.newsapp.home.NewsAdapter.onBindViewHolder(NewsAdapter.java:17)
FATAL EXCEPTION: main
Process: com.example.melal.newsapp, PID: 9523
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.isEmpty()' on a null object reference
at com.example.melal.newsapp.data.News.isVideoNews(News.java:60)
at com.example.melal.newsapp.home.NewsAdapter$NewsViewHolder.bindNews(NewsAdapter.java:53)
at com.example.melal.newsapp.home.NewsAdapter.onBindViewHolder(NewsAdapter.java:30)
at com.example.melal.newsapp.home.NewsAdapter.onBindViewHolder(NewsAdapter.java:16)
I get every data when I debug and my texts and title and every things show on UI but just my image does not show,my banners pic show in my another adapter on UI but main problem is the adapter that i told,
please help me

You have to check here null and empty values for path of Image.
Put following line in if condition like:
if(TextUtils.isEmpty(news.getImage())) {
// Load default image
newsImageView.setImageResource(R.drawable.placeholder);
} else {
Picasso.get().load(news.getImage()).into(newsImageView);
}
Hope it will work.

You need to provide seralized name and expose that field of Model class so that your gson can parse it. Currently you have not set it to any field and thus GSON is not parsing your response accordingly. Below is sample code for how to define it in class. You need to expose all your class fields accordingly.
#SerializedName("foo") //name inside quotes must match with your json field name.
#Expose
private String foo;

Related

How do I make readable TimeStamp from Cloud Firestore?

So below are the database of my cloud firestore. That DatePosted shows 2nd December, 2019 at 8.19 pm UTC +8. According to the code I used and I run my app, it shows an unreadable number. Is it correct on how I retrieve the data from cloud firebase? If its wrong, how do I retrieve that timestamp and make it readable?
ForumAdapter.java
public class ForumAdapter extends FirestoreRecyclerAdapter<Forum,ForumAdapter.ForumHolder> {
private OnItemClickListener listener;
public ForumAdapter(FirestoreRecyclerOptions<Forum> options) {
super(options);
}
#Override
public void onBindViewHolder(ForumHolder forumHolder, int i, Forum forum) {
forumHolder.textViewTitle.setText(forum.getTitle());
forumHolder.textViewDescription.setText(forum.getDescription());
forumHolder.timeStamp.setText(forum.getDatePosted().toString());
}
#NonNull
#Override
public ForumHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
android.view.View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardviewforumtitle,parent,false);
return new ForumHolder(v);
}
class ForumHolder extends RecyclerView.ViewHolder{
TextView textViewTitle;
TextView textViewDescription;
TextView timeStamp;
public ForumHolder(View itemView) {
super(itemView);
textViewTitle = itemView.findViewById(R.id.tvTitle);
textViewDescription = itemView.findViewById(R.id.tvDescription);
timeStamp = itemView.findViewById(R.id.tvTimestamp);
textViewTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = getAdapterPosition();
// NO_POSITION to prevent app crash when click -1 index
if(position != RecyclerView.NO_POSITION && listener !=null ){
listener.onItemClick(getSnapshots().getSnapshot(position),position);
}
}
});
}
}
public interface OnItemClickListener{
void onItemClick(DocumentSnapshot documentSnapshot, int position);
}
public void setOnItemClickListener(OnItemClickListener listener){
this.listener = listener;
}
#Override
public int getItemCount() {
return super.getItemCount();
}
}
Forum.java
public class Forum {
private String Title;
private String Description;
private String User;
private com.google.firebase.Timestamp DatePosted;
public Forum() {
}
public Forum(String title, String description, Timestamp datePosted,String user) {
Title = title;
Description = description;
DatePosted = datePosted;
User = user;
}
public String getUser() {
return User;
}
public void setUser(String user) {
User = user;
}
public String getTitle() {
return Title;
}
public void setTitle(String title) {
Title = title;
}
public String getDescription() {
return Description;
}
public void setDescription(String description) {
Description = description;
}
public Timestamp getDatePosted() {
return DatePosted;
}
public void setDatePosted(Timestamp datePosted) {
DatePosted = datePosted;
}
}
When you query a document with a timestamp field, you will get a Timestamp object back. If you'd rather have a Date object, you can use its toDate method.
You will need to write some code to format this for display. This is a very common task for mobile and web apps.

How am I getting correct item position in ViewHolder without using getAdapterPosition in Recyclerview?

At first I assigned clickListener in OnBindViewHolder and it was working just fine but as many here on stackoverflow advised that it is not a good practice and it is better if I handle all my click listener logic in ViewHolder. Now in OBVH I used holder's position to get current item' position but in VH I didn't even use any position or getAdapterPosition and still I am getting correct item position.
ViewHolder:
private class NewsHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ImageView mImageView;
private TextView mTextView;
private Results mResults;
private NewsHolder(View itemView) {
super(itemView);
mImageView = itemView.findViewById(R.id.news_image);
mTextView = itemView.findViewById(R.id.news_headlines);
itemView.setOnClickListener(this);
}
public void bind(Results results){
mResults = results;
mTextView.setText(mResults.getWebTitle());
String picture = mResults.getFields().getThumbnail();
if (!picture.isEmpty()) {
Picasso.get()
.load(picture)
.placeholder(R.drawable.display)
.resize(200, 200)
.into(mImageView);
} else {
Picasso.get()
.load(R.drawable.display)
.resize(200, 200)
.into(mImageView);
}
}
#Override
public void onClick(View view){
String itemWebUrl = mResults.getWebUrl();
Uri currentItemUri = Uri.parse(itemWebUrl);
Intent websiteIntent = new Intent(Intent.ACTION_VIEW, currentItemUri);
view.getContext().startActivity(websiteIntent);
}
}
OnBindViewHolder
#Override
public void onBindViewHolder(#NonNull NewsHolder holder, int position) {
try {
Results results = mResults.get(position);
holder.bind(results);
} catch (Throwable throwable) {
Log.d(TAG, " " + throwable);
}
}
Results model class:
public class Results{
#SerializedName("id")
#Expose
public String id;
#SerializedName("sectionId")
#Expose
private String sectionId;
#SerializedName("webTitle")
#Expose
private String webTitle;
#SerializedName("webUrl")
#Expose
private String webUrl;
#SerializedName("fields")
#Expose
private Fields fields;
public String getId(){
return id;
}
public String getSectionId(){
return sectionId;
}
public String getWebTitle(){
return webTitle;
}
public String getWebUrl(){
return webUrl;
}
public Fields getFields(){
return fields;
}
}
Is it possible to get correct item position without getAdapterPosition()?
You are already getting proper Results object using position, which is used in NewsHolder. So nothing has to be modified.
Results results = mResults.get(position);
holder.bind(results);

Firebase display integers inside a textfield in an android app

I am trying to show my integers saved in the firebase database in my Recyclerview. I have two Strings I can easily show in my App, but somehow it doesn't work with integers. Even more strange is that when I save a String where before there was an integer I got an error.
Here my database structure
Here I populate my Viewholder
#Override
public void onStart() {
super.onStart();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<ListItem, ListViewHolder>(
ListItem.class,
R.layout.card_item,
ListViewHolder.class,
mDatabase.orderByChild("minusAccandShare").endAt(0)
) {
#Override
protected void populateViewHolder(ListViewHolder viewHolder, ListItem model, final int position) {
viewHolder.setImage(getActivity().getApplicationContext(), model.getImage());
viewHolder.setHeading(model.getHeading());
viewHolder.setStoreName(model.getStoreName());
viewHolder.setAcceptCount(model.getAcceptCount());
viewHolder.mView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent i = new Intent(getActivity(), DetailActivity.class);
Bundle extras = new Bundle();
extras.putString(EXTRA_QUOTE, firebaseRecyclerAdapter.getRef(position).getKey());
i.putExtra(BUNDLE_EXTRAS, extras);
startActivity(i);
}
});
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
My ViewHolder
public static class ListViewHolder extends RecyclerView.ViewHolder {
View mView;
public ListViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setImage(Context ctx, String image){
ImageView postImage = (ImageView) mView.findViewById(R.id.im_item_icon);
Picasso.with(ctx).load(image).resize(200,200).into(postImage);
}
public void setHeading(String heading){
TextView card_heading = (TextView) mView.findViewById(R.id.lbl_item_sub_title);
card_heading.setText(heading);
}
public void setStoreName(String storeName){
TextView card_storeName = (TextView) mView.findViewById(R.id.lbl_item_text);
card_storeName.setText(storeName);
}
public void setAcceptCount(String accepts){
TextView accepts_count = (TextView) mView.findViewById(R.id.lbl_accepts_count);
accepts_count.setText(accepts);
}
}
And finally my model
public class ListItem {
private String heading, storeName, image;
private Long accepts;
public ListItem() {
}
public ListItem(String heading,String storeName, Long accepts, String image){
this.heading = heading;
this.storeName = storeName;
this.image = image;
this.accepts = accepts;
}
public String getHeading() {
return heading;
}
public String getStoreName() {
return storeName;
}
public String getImage() {return image;}
public String getAcceptCount() {
return String.valueOf(accepts);
}
In this way I see null where there should stand 200 instead. I tried everything. I even saved an String instead of an integer and tried to show it the same way as the heading String ( which works perfectly) but then I see a blank place... Please help. Thanks
you should create a getter and setter for accepts in ListItem.
public Long getAccepts() {
return accepts;
}
public void setAccepts(Long accepts) {
this.accepts = accepts;
}

Android Retrofit 2 issue in adapter

I have problem with adapter. I'm using retrofit 2 to parse JSON data. JSON example, models and adapter code are below.
JSON example:
[{
"id":1,
"type":"TEMPERATURE",
"measurements":[
{
"value":"22.58",
"time":"2017-01-11T12:20:44.701"
}]
},{
"id":2,
"type":"HUMIDITY",
"measurements":[
{
"value":"52.366",
"time":"2017-01-11T12:20:44.731"
}]
},{
"id":3,
"type":"LUMINOSITY",
"measurements":[
{
"value":"1.0",
"time":"2017-01-11T12:20:44.742"
}]
}]
Model Senzori:
public class Senzori {
#SerializedName("id")
#Expose
private Integer id;
#SerializedName("type")
#Expose
private String type;
#SerializedName("measurements")
#Expose
private List<Measurement> measurements = new ArrayList<>();
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public List<Measurement> getMeasurements() {
return measurements;
}
public void setMeasurements(List<Measurement> measurements) {
this.measurements = measurements;
}
}
Model Measurement:
public class Measurement {
#SerializedName("value")
#Expose
private String value;
#SerializedName("time")
#Expose
private String time;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
Adapter:
public class SenzoriAdapter extends RecyclerView.Adapter<SenzoriAdapter.Holder>{
private List<Senzori> mSenzori;
public SenzoriAdapter(){
mSenzori = new ArrayList<>();
}
#Override
public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
View row = LayoutInflater.from(parent.getContext()).inflate(R.layout.main_item, parent, false);
return new Holder(row);
}
#Override
public void onBindViewHolder(Holder holder, int position) {
Senzori curr = mSenzori.get(position);
holder.senzoriType.setText(curr.getType());
holder.senzoriLastAlive.setText(curr.getMeasurements().getTime());
holder.senzoriMeasurement.setText(curr.getMeasurements().getValue());
}
#Override
public int getItemCount() {
return mSenzori.size();
}
public void addSenzori(Senzori senzori) {
mSenzori.add(senzori);
notifyDataSetChanged();
}
public class Holder extends RecyclerView.ViewHolder {
private TextView senzoriType, senzoriLastAlive, senzoriMeasurement;
public Holder(View itemView) {
super(itemView);
senzoriType = (TextView) itemView.findViewById(R.id.senzoriType);
senzoriLastAlive = (TextView) itemView.findViewById(R.id.senzoriLastAlive);
senzoriMeasurement = (TextView) itemView.findViewById(R.id.senzoriMeasurement);
}
}
}
I'm getting "Cannot resolve method" on getTime() and getValue().
Since getMeasurements() returns a List and the JSON you have shown contains only one item. Hence you should call it like this:
holder.senzoriLastAlive.setText(curr.getMeasurements().get(0).getTime());
holder.senzoriMeasurement.setText(curr.getMeasurements().get(0).getValue());
I would suggest adding a check to see if getMeasurements() is not empty, else you will get an Exception.
The problem is you haven't given any information on what item from the List that you wish to find the value or time of.
holder.senzoriMeasurement.setText(curr.getMeasurements().get(<item you wish to get>).getValue;
If you want to get the first item in the List
holder.senzoriMeasurement.setText(curr.getMeasurements().get(0).getValue;

How can i set another data into constructor DataModel in Retrofit in Android

In my application I should load data from server and for this job I use Retrofit library.
In my application i want load string data from server and i should load images from drawable folder .
I can load string from server and show it on textview, but when add images i don't know how can i it?!
DataModel:
public class Retrofit_ColoniesModel {
//Load from server
#SerializedName("id")
private Integer id;
#SerializedName("slug")
private String slug;
#SerializedName("title")
private String title;
#SerializedName("description")
private String description;
#SerializedName("parent")
private Integer parent;
#SerializedName("post_count")
private Integer post_count;
//Load from local
private int[] image;
public Retrofit_ColoniesModel(Integer id, String slug, String title, String description, Integer parent, Integer post_count,
int[] image) {
this.id = id;
this.slug = slug;
this.title = title;
this.description = description;
this.parent = parent;
this.post_count = post_count;
this.image = image;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getSlug() {
return slug;
}
public void setSlug(String slug) {
this.slug = slug;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public Integer getParent() {
return parent;
}
public void setParent(Integer parent) {
this.parent = parent;
}
public Integer getPost_count() {
return post_count;
}
public void setPost_count(Integer post_count) {
this.post_count = post_count;
}
public int[] getImage() {
return image;
}
public void setImage(int[] image) {
this.image= image;
}
DataModelResponse:
public class Retrofit_ColoniesModelResponse {
#SerializedName("status")
private String status;
#SerializedName("count")
private int count;
#SerializedName("categories")
private List<Retrofit_ColoniesModel> categories;
public List<Retrofit_ColoniesModel> getCategories() {
return categories;
}
public void setCategories(List<Retrofit_ColoniesModel> categories) {
this.categories = categories;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
Retrofit code in activity:
// Retrofit //////////
Retrofit_ApiInterface apiInterface = Retrofit_ApiClient.getClient().create(Retrofit_ApiInterface.class);
Call<Retrofit_ColoniesModelResponse> call = apiInterface.getResponse();
call.enqueue(new Callback<Retrofit_ColoniesModelResponse>() {
#Override
public void onResponse(Call<Retrofit_ColoniesModelResponse> call, Response<Retrofit_ColoniesModelResponse> response) {
List<Retrofit_ColoniesModel> models = response.body().getCategories();
mAdaper = new ColoniesAdapter(context, models);
colonies_RecyclerView.setAdapter(mAdaper);
}
#Override
public void onFailure(Call<Retrofit_ColoniesModelResponse> call, Throwable t) {
}
});
//////////////////////
I want save images into Array, such as :
final int[] colImages = {
R.drawable.colonies_image_food,
R.drawable.colonies_image_medical,
R.drawable.colonies_image_tecgnolegy,
R.drawable.colonies_image_entertenement,
R.drawable.colonies_image_car,
R.drawable.colonies_image_model,
R.drawable.colonies_image_sport,
};
Adapter:
public class ColoniesAdapter extends RecyclerView.Adapter<ColoniesAdapter.ViewHolder> {
private List<Retrofit_ColoniesModel> mDateSet;
private Context mContext;
private SparseBooleanArray expandState = new SparseBooleanArray();
public ColoniesAdapter(Context context, List<Retrofit_ColoniesModel> dataSet) {
this.mContext = context;
this.mDateSet = dataSet;
for (int i = 0; i < mDateSet.size(); i++) {
expandState.append(i, false);
}
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.colonies_row, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.colonies_title.setText(mDateSet.get(position).getTitle());
holder.colonies_title.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pos = holder.getPosition();
Retrofit_ColoniesModel model = mDateSet.get(pos);
v.getContext().startActivity(new Intent(v.getContext(), Category_page.class)
.putExtra("categoryTitle", model.getTitle())
.putExtra("categoryID", model.getId()));
}
});
Glide.with(mContext)
.load(mDateSet.get(position).getImage()[position])
.placeholder(R.drawable.post_image)
.crossFade()
.override(700, 400)
.into(holder.colonies_image);
holder.colonies_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int pos = holder.getPosition();
Retrofit_ColoniesModel model = mDateSet.get(pos);
v.getContext().startActivity(new Intent(v.getContext(), Category_page.class)
.putExtra("categoryTitle", model.getTitle())
.putExtra("categoryID", model.getId()));
}
});
holder.colonies_description.setText(mDateSet.get(position).getDescription());
holder.colonies_count.setText("مطالب موجود در کلونی : " + mDateSet.get(position).getPost_count());
holder.expandableLayout.setInterpolator(mDateSet.get(position).getInterpolator());
holder.expandableLayout.setExpanded(expandState.get(position));
holder.expandableLayout.setListener(new ExpandableLayoutListenerAdapter() {
#Override
public void onPreOpen() {
createRotateAnimator(holder.buttonLayout, 0f, 180f).start();
expandState.put(position, true);
}
#Override
public void onPreClose() {
createRotateAnimator(holder.buttonLayout, 180f, 0f).start();
expandState.put(position, false);
}
});
holder.buttonLayout.setRotation(expandState.get(position) ? 180f : 0f);
holder.buttonLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View v) {
onClickButton(holder.expandableLayout);
}
});
}
private void onClickButton(final ExpandableLayout expandableLayout) {
expandableLayout.toggle();
}
#Override
public int getItemCount() {
return mDateSet.size();
}
public void remove(int position) {
mDateSet.remove(position);
notifyItemRemoved(position);
}
public void clear() {
mDateSet.clear();
notifyDataSetChanged();
}
public void add(List<Retrofit_ColoniesModel> models) {
mDateSet.addAll(models);
notifyDataSetChanged();
}
public void update(List<Retrofit_ColoniesModel> models) {
mDateSet.clear();
mDateSet.addAll(models);
notifyDataSetChanged();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private TextView colonies_title, colonies_description, colonies_count;
private ImageView colonies_image;
private ExpandableLinearLayout expandableLayout;
private RelativeLayout buttonLayout;
public ViewHolder(View itemView) {
super(itemView);
colonies_title = (TextView) itemView.findViewById(R.id.colonies_colony_title_text);
colonies_image = (ImageView) itemView.findViewById(R.id.colonies_cover_image);
colonies_description = (TextView) itemView.findViewById(R.id.colonies_expandable_description_text);
colonies_count = (TextView) itemView.findViewById(R.id.colonies_count_title_text);
buttonLayout = (RelativeLayout) itemView.findViewById(R.id.colonies_expandable_button);
expandableLayout = (ExpandableLinearLayout) itemView.findViewById(R.id.colonies_expandable_layout);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/* v.getContext().startActivity(new Intent(v.getContext(), PostShow_page.class)
.putExtra("title", model.getTitle())
.putExtra("image", model.getThumbnail()));*/
}
});
}
}
public ObjectAnimator createRotateAnimator(final View target, final float from, final float to) {
ObjectAnimator animator = ObjectAnimator.ofFloat(target, "rotation", from, to);
animator.setDuration(300);
animator.setInterpolator(Utils.createInterpolator(Utils.LINEAR_INTERPOLATOR));
return animator;
}
}
I don't know how can i add int[] into my constructor, because in retrofit fill the constructor with List<Retrofit_ColoniesModel> models = response.body().getCategories(); .
How can i fix my issue? I really need this tutorial, please help me. Thanks all <3
If the array of images is static (don't depend on the server) then you could just do the following:
public class Retrofit_ColoniesModel {
...
private static final int[] colImages = {
R.drawable.colonies_image_food,
R.drawable.colonies_image_medical,
R.drawable.colonies_image_tecgnolegy,
R.drawable.colonies_image_entertenement,
R.drawable.colonies_image_car,
R.drawable.colonies_image_model,
R.drawable.colonies_image_sport,
};
But if this array is not static (depends on the server response) then I suggest you to map to an array of Strings that describe each image, and the server should respond with those Strings, for example:
"images":["IMAGE1", "IMAGE2"]
And then have a helper class that could map between those string keys to the actual R.drawable resources.
Let me know if you need sample code.
Non-static Nested Classes (Inner Classes)
Non-static nested classes in Java are also called inner classes. Inner classes are associated with an instance of the enclosing class. Thus, you must first create an instance of the enclosing class to create an instance of an inner class. Here is an example inner class definition:
public class Outer {
public class Inner {
}
}
Here is how you create an instance of the Inner class:
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
Notice how you put new after the reference to the outer class in order to create an instance of the inner class.
Non-static nested classes (inner classes) have access to the fields of the enclosing class, even if they are declared private. Here is an example of that:

Categories

Resources