Mohammad Nouri
Mohammad Nouri

Reputation: 2305

How to set image from drawable into imageview in Android

I want show image into RecyclerView and i should use this images from drawable folder. I write below codes, but show me this error :

java.lang.IllegalArgumentException: Unknown type class [I. You must provide a Model of a type for which there is a registered ModelLoader, if you are using a custom model, you must first call Glide#register with a ModelLoaderFactory for your custom model class
at com.bumptech.glide.RequestManager.loadGeneric(RequestManager.java:629)
at com.bumptech.glide.RequestManager.load(RequestManager.java:598)
at com.tellfa.colony.Adapters.ColoniesAdapter.onBindViewHolder(ColoniesAdapter.java:66)
at com.tellfa.colony.Adapters.ColoniesAdapter.onBindViewHolder(ColoniesAdapter.java:29)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5768)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5801)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5037)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4913)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:578)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3260)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3069)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3518)
at android.view.View.layout(View.java:15631)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
at android.view.View.layout(View.java:15631)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15631)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1705)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1559)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1468)
at android.view.View.layout(View.java:15631)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15631)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1705)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1559)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1468)
at android.view.View.layout(View.java:15631)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
at android.view.View.layout(View.java:15631)
at android.view.ViewGroup.layout(ViewGroup.java:4966)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2101)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1858)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1077)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5845)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5272)
at java.lang.reflect.Method.invoke(Native Method)
at jav

Model codes:

public class MainDataModel implements Serializable {

    private static final long serialVersionUID = 1L;
    private String category;
    private String categoryID;
    private String categoryDescription;
    private int[] categoryImages;

    private TimeInterpolator interpolator;

    public MainDataModel(String categoryID, String category, String categoryDescription, int[] categoryImages,
                         TimeInterpolator interpolator) {
        this.categoryID = categoryID;
        this.category = category;
        this.categoryDescription = categoryDescription;
        this.categoryImages = categoryImages;
        this.interpolator = interpolator;
    }

    public String getCategory() {
        return category;
    }

    public void setCategory(String category) {
        this.category = category;
    }

    public String getCategoryID() {
        return categoryID;
    }

    public void setCategoryID(String caategoryID) {
        this.categoryID = caategoryID;
    }

    public String getCategoryDescription() {
        return categoryDescription;
    }

    public void setCategoryDescription(String categoryDescription) {
        this.categoryDescription = categoryDescription;
    }

    public TimeInterpolator getInterpolator() {
        return interpolator;
    }

    public void setInterpolator(TimeInterpolator interpolator) {
        this.interpolator = interpolator;
    }

    public int[] getCategoryImages() {
        return categoryImages;
    }

    public void setCategoryImages(int[] categoryImages) {
        this.categoryImages = categoryImages;
    }

}

AsyncTask codes and set image :

public class ColoniesDataInfo {

    private static Context mContext;
    private String ServerAddress = ServerIP_colonies.getColoniesIP();

    public void getColoniesDataInfo(Context context) {
        mContext = context;
        new getInfo().execute(ServerAddress);
    }

    private class getInfo extends AsyncTask<String, Void, String> {
        EventBus bus = EventBus.getDefault();
        private String ou_response;
        private List<MainDataModel> infoModels;

        @Override
        protected void onPreExecute() {
            CustomProcessDialog.createAndShow(mContext);
            infoModels = new ArrayList<>();
        }

        @Override
        protected String doInBackground(String... params) {
            OkHttpClient client = new OkHttpClient();

            //String url = (String) params[0];
            Request request = new Request.Builder()
                    .url(ServerAddress)
                    //.cacheControl(CacheControl.FORCE_NETWORK)
                    .build();

            Response response;
            try {
                response = client.newCall(request).execute();
                ou_response = response.body().string();
                response.body().close();
                if (ou_response != null) {
                    try {
                        JSONObject postObj = new JSONObject(ou_response);
                        JSONArray postsArray = postObj.optJSONArray("categories");
                        infoModels = new ArrayList<>();

                        for (int i = 0; i <= infoModels.size(); i++) {
                            JSONObject postObject = (JSONObject) postsArray.get(i);

                            String colID = postObject.getString("id");
                            String colTitle = postObject.getString("title");
                            String colDescription = postObject.getString("description");

                            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,
                            };

                            Log.d("Data", "Colonies ID: " + colID);
                            Log.d("Data", "Colonies title: " + colTitle);
                            Log.d("Data", "Colonies Desc: " + colDescription);
                            Log.d("Data", "---------------------------------");

                            //Use the title and id as per your requirement
                            infoModels.add(new MainDataModel(colID, colTitle, colDescription, colImages,
                                    Utils.createInterpolator(Utils.BOUNCE_INTERPOLATOR)));
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return ou_response;
        }

        @Override
        protected void onPostExecute(String result) {
            CustomProcessDialog.dissmis();
            if (result != null) {
                bus.post(infoModels);
            } else {
                Toast.makeText(mContext, "Empty", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

Adapter codes and set image into adapter :

public class ColoniesAdapter extends RecyclerView.Adapter<ColoniesAdapter.ViewHolder> {

    private List<MainDataModel> mDateSet;
    private Context mContext;
    private SparseBooleanArray expandState = new SparseBooleanArray();

    public ColoniesAdapter(Context context, List<MainDataModel> 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).getCategory());
        holder.colonies_title.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int pos = holder.getPosition();
                MainDataModel model = mDateSet.get(pos);
                v.getContext().startActivity(new Intent(v.getContext(), Category_page.class)
                        .putExtra("categoryTitle", model.getCategory())
                        .putExtra("categoryID", model.getCategoryID()));
            }
        });

        Glide.with(mContext)
                .load(mDateSet.get(position).getCategoryImages())
                .placeholder(R.drawable.post_image)
                .crossFade()
                .override(700, 400)
                .into(holder.colonies_image);
        holder.colonies_description.setText(mDateSet.get(position).getCategoryDescription());

        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<MainDataModel> models) {
        mDateSet.addAll(models);
        notifyDataSetChanged();
    }

    public void update(List<MainDataModel> models) {
        mDateSet.clear();
        mDateSet.addAll(models);
        notifyDataSetChanged();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        private TextView colonies_title, colonies_description;
        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);

            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;
    }
}

How can i fix it?

Upvotes: 2

Views: 2681

Answers (2)

Sahana Prabhakar
Sahana Prabhakar

Reputation: 581

First of all why are you loading all the array of images into a single imageView? Your method getCategoryImages() gives you an integer array of images. retrieve one by one corresponding to the position.

If the problem still persists you can refer to the below link with the same issue

https://github.com/bumptech/glide/issues/588

Glide.with(mContext)
            .load(mDateSet.get(position).getCategoryImages()[position])
            .placeholder(R.drawable.post_image)
            .crossFade()
            .override(700, 400)
            .into(holder.colonies_image);

Upvotes: 3

Marijan
Marijan

Reputation: 731

You are trying to load a list of ints and that is why Glide isn't working. As you gave one ImageView as destination, only one image can be loaded there. What you need to do is extract the image you wish to load and then pass it to Glide.

Upvotes: 1

Related Questions