Issue
I'm trying to get Android resource ID and set ImageView src to that specific ID from a JSON array.
I'm using the following code
String str = mDataset.get(position).icon_res.substring(0, mDataset.get(position).icon_res.lastIndexOf('.'));
int res = Resources.getSystem().getIdentifier("ic_" + str, "drawable", this.getClass().getPackage().getName());
if(res == 0)
{
Log.d("is null", "null");
Log.d("string = ", "ic_" + str);
Log.d("package", this.getClass().getPackage().getName());
}
But from some reason it's detecting the res int as 0. Package name seems correct, string is correct but for some reason it's not finding the correct drawable resource. What am I doing wrong here?
EDIT:
Full adapter code
package com.xyz
import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.media.Image;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Kreso on 23.3.2017..
*/
public class CategoryAdapter extends RecyclerView.Adapter<CategoryAdapter.ViewHolder>{
private List<Category> mDataset;
private Context context;
public CategoryAdapter(List<Category> categoriesList) {
mDataset = categoriesList;
}
// Provide a reference to the views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
public class ViewHolder extends RecyclerView.ViewHolder {
// each data item is just a string in this case
public TextView txtHeader;
public ConstraintLayout holderLayout;
public ImageView icon;
public ViewHolder(View v) {
super(v);
txtHeader = (TextView) v.findViewById(R.id.firstLine);
holderLayout = (ConstraintLayout) v.findViewById(R.id.itemHolder);
icon = (ImageView) v.findViewById(R.id.icon);
context = v.getContext();
}
}
/*public void add(int position, String item) {
mDataset.add(position, item);
notifyItemInserted(position);
}*/
public void remove(int position) {
mDataset.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, mDataset.size());
}
// Provide a suitable constructor (depends on the kind of dataset)
/* public CategoryAdapter(ArrayList<String> myDataset) {
mDataset = myDataset;
}*/
// Create new views (invoked by the layout manager)
@Override
public CategoryAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
// create a new view
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item_row, parent, false);
// set the view's size, margins, paddings and layout parameters
ViewHolder vh = new ViewHolder(v);
return vh;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
final String name = mDataset.get(position).name;
holder.txtHeader.setText(mDataset.get(position).name);
//holder.icon.setImageResource(R.drawable.ic_beer);
String str = mDataset.get(position).icon_res.substring(0, mDataset.get(position).icon_res.lastIndexOf('.'));
int res = context.getResources().getSystem().getIdentifier("ic_" + str, "drawable", context.getPackageName());
if(res == 0)
{
Log.d("is null", "null");
Log.d("string = ", "ic_" + str);
Log.d("package", this.getClass().getPackage().getName());
}
holder.icon.setImageResource(res);
holder.holderLayout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
remove(position);
}
});
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataset.size();
}
}
Solution
use this code, to get image from Resources :
int res = context.getResources().getIdentifier("ic_" + str, "drawable", context.getPackageName());
Answered By - Narendra Sorathiya
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.