How to show imageViews in RecyclerView (Android)












0















I create a image's array : (logos.xml)



<integer-array name="icons"><Item>@drawable/pic_01 to @drawable/pic_04<item>


Then , in my class Function : I have



public Function(String name , int icon) { this.icon = icon }

public void getIcon() { return icon; }


Final , in FunctionAdapter , I get Imageview



punlic FunctionAdapter(Context context) { icons = context.getResources().getIntArray(R.array.icons);


my question is in onBindViewHolde(@NonNull FunViewHolder holder, int position)



 holder.itemImageView.setImageResource(icons[position])


is not working , I want to know what am I miss ? Thanks Help.



I want to change my ImageView (pic1 to pic4)



my Adapter :



public class FunctionAdapter extends RecyclerView.Adapter<FunctionAdapter.FunViewHolder>
{
private final String functions;
private final int icons;
Context context;

public FunctionAdapter(Context context){
this.context = context;
functions = context.getResources().getStringArray(R.array.functions);
icons = context.getResources().getIntArray(R.array.icons);
}

public FunViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {

View view = LayoutInflater.from(context).inflate(R.layout.group_layout, parent,false);
return new FunViewHolder(view);
}

public void onBindViewHolder(@NonNull FunViewHolder holder, int position) {

holder.nameText.setText(functions[position]);
holder.itemImageView.setImageResource(icons[position]);
}

public int getItemCount() { return functions.length; }

public class FunViewHolder extends RecyclerView.ViewHolder{

TextView nameText;
ImageView itemImageView;
public FunViewHolder(@NonNull View itemView) {
super(itemView);
nameText = itemView.findViewById(R.id.group_title);
itemImageView = itemView.findViewById(R.id.group_logo);
}
}


Update :



my mistake , logos.xml maybe the problem , but I don't know Why?



I decide to edit my icons :



private final int icons = new int{R.drawable.pic_01, R.drawable.pic_02, R.drawable.pic_03, R.drawable.pic_04} ;


then , pics will change.










share|improve this question

























  • can you add the error?

    – Anjani Mittal
    Nov 15 '18 at 7:01











  • @ Anjani Mittal , the error is no imageView show in RecyclerView .

    – Tzuyu Lin
    Nov 15 '18 at 7:09











  • can you post the whole adapter class

    – Kevin Kurien
    Nov 15 '18 at 7:17











  • use Picasso library to set image

    – Anjani Mittal
    Nov 15 '18 at 8:41
















0















I create a image's array : (logos.xml)



<integer-array name="icons"><Item>@drawable/pic_01 to @drawable/pic_04<item>


Then , in my class Function : I have



public Function(String name , int icon) { this.icon = icon }

public void getIcon() { return icon; }


Final , in FunctionAdapter , I get Imageview



punlic FunctionAdapter(Context context) { icons = context.getResources().getIntArray(R.array.icons);


my question is in onBindViewHolde(@NonNull FunViewHolder holder, int position)



 holder.itemImageView.setImageResource(icons[position])


is not working , I want to know what am I miss ? Thanks Help.



I want to change my ImageView (pic1 to pic4)



my Adapter :



public class FunctionAdapter extends RecyclerView.Adapter<FunctionAdapter.FunViewHolder>
{
private final String functions;
private final int icons;
Context context;

public FunctionAdapter(Context context){
this.context = context;
functions = context.getResources().getStringArray(R.array.functions);
icons = context.getResources().getIntArray(R.array.icons);
}

public FunViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {

View view = LayoutInflater.from(context).inflate(R.layout.group_layout, parent,false);
return new FunViewHolder(view);
}

public void onBindViewHolder(@NonNull FunViewHolder holder, int position) {

holder.nameText.setText(functions[position]);
holder.itemImageView.setImageResource(icons[position]);
}

public int getItemCount() { return functions.length; }

public class FunViewHolder extends RecyclerView.ViewHolder{

TextView nameText;
ImageView itemImageView;
public FunViewHolder(@NonNull View itemView) {
super(itemView);
nameText = itemView.findViewById(R.id.group_title);
itemImageView = itemView.findViewById(R.id.group_logo);
}
}


Update :



my mistake , logos.xml maybe the problem , but I don't know Why?



I decide to edit my icons :



private final int icons = new int{R.drawable.pic_01, R.drawable.pic_02, R.drawable.pic_03, R.drawable.pic_04} ;


then , pics will change.










share|improve this question

























  • can you add the error?

    – Anjani Mittal
    Nov 15 '18 at 7:01











  • @ Anjani Mittal , the error is no imageView show in RecyclerView .

    – Tzuyu Lin
    Nov 15 '18 at 7:09











  • can you post the whole adapter class

    – Kevin Kurien
    Nov 15 '18 at 7:17











  • use Picasso library to set image

    – Anjani Mittal
    Nov 15 '18 at 8:41














0












0








0








I create a image's array : (logos.xml)



<integer-array name="icons"><Item>@drawable/pic_01 to @drawable/pic_04<item>


Then , in my class Function : I have



public Function(String name , int icon) { this.icon = icon }

public void getIcon() { return icon; }


Final , in FunctionAdapter , I get Imageview



punlic FunctionAdapter(Context context) { icons = context.getResources().getIntArray(R.array.icons);


my question is in onBindViewHolde(@NonNull FunViewHolder holder, int position)



 holder.itemImageView.setImageResource(icons[position])


is not working , I want to know what am I miss ? Thanks Help.



I want to change my ImageView (pic1 to pic4)



my Adapter :



public class FunctionAdapter extends RecyclerView.Adapter<FunctionAdapter.FunViewHolder>
{
private final String functions;
private final int icons;
Context context;

public FunctionAdapter(Context context){
this.context = context;
functions = context.getResources().getStringArray(R.array.functions);
icons = context.getResources().getIntArray(R.array.icons);
}

public FunViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {

View view = LayoutInflater.from(context).inflate(R.layout.group_layout, parent,false);
return new FunViewHolder(view);
}

public void onBindViewHolder(@NonNull FunViewHolder holder, int position) {

holder.nameText.setText(functions[position]);
holder.itemImageView.setImageResource(icons[position]);
}

public int getItemCount() { return functions.length; }

public class FunViewHolder extends RecyclerView.ViewHolder{

TextView nameText;
ImageView itemImageView;
public FunViewHolder(@NonNull View itemView) {
super(itemView);
nameText = itemView.findViewById(R.id.group_title);
itemImageView = itemView.findViewById(R.id.group_logo);
}
}


Update :



my mistake , logos.xml maybe the problem , but I don't know Why?



I decide to edit my icons :



private final int icons = new int{R.drawable.pic_01, R.drawable.pic_02, R.drawable.pic_03, R.drawable.pic_04} ;


then , pics will change.










share|improve this question
















I create a image's array : (logos.xml)



<integer-array name="icons"><Item>@drawable/pic_01 to @drawable/pic_04<item>


Then , in my class Function : I have



public Function(String name , int icon) { this.icon = icon }

public void getIcon() { return icon; }


Final , in FunctionAdapter , I get Imageview



punlic FunctionAdapter(Context context) { icons = context.getResources().getIntArray(R.array.icons);


my question is in onBindViewHolde(@NonNull FunViewHolder holder, int position)



 holder.itemImageView.setImageResource(icons[position])


is not working , I want to know what am I miss ? Thanks Help.



I want to change my ImageView (pic1 to pic4)



my Adapter :



public class FunctionAdapter extends RecyclerView.Adapter<FunctionAdapter.FunViewHolder>
{
private final String functions;
private final int icons;
Context context;

public FunctionAdapter(Context context){
this.context = context;
functions = context.getResources().getStringArray(R.array.functions);
icons = context.getResources().getIntArray(R.array.icons);
}

public FunViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {

View view = LayoutInflater.from(context).inflate(R.layout.group_layout, parent,false);
return new FunViewHolder(view);
}

public void onBindViewHolder(@NonNull FunViewHolder holder, int position) {

holder.nameText.setText(functions[position]);
holder.itemImageView.setImageResource(icons[position]);
}

public int getItemCount() { return functions.length; }

public class FunViewHolder extends RecyclerView.ViewHolder{

TextView nameText;
ImageView itemImageView;
public FunViewHolder(@NonNull View itemView) {
super(itemView);
nameText = itemView.findViewById(R.id.group_title);
itemImageView = itemView.findViewById(R.id.group_logo);
}
}


Update :



my mistake , logos.xml maybe the problem , but I don't know Why?



I decide to edit my icons :



private final int icons = new int{R.drawable.pic_01, R.drawable.pic_02, R.drawable.pic_03, R.drawable.pic_04} ;


then , pics will change.







android android-recyclerview imageview adapter






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 8:52







Tzuyu Lin

















asked Nov 15 '18 at 6:57









Tzuyu LinTzuyu Lin

35




35













  • can you add the error?

    – Anjani Mittal
    Nov 15 '18 at 7:01











  • @ Anjani Mittal , the error is no imageView show in RecyclerView .

    – Tzuyu Lin
    Nov 15 '18 at 7:09











  • can you post the whole adapter class

    – Kevin Kurien
    Nov 15 '18 at 7:17











  • use Picasso library to set image

    – Anjani Mittal
    Nov 15 '18 at 8:41



















  • can you add the error?

    – Anjani Mittal
    Nov 15 '18 at 7:01











  • @ Anjani Mittal , the error is no imageView show in RecyclerView .

    – Tzuyu Lin
    Nov 15 '18 at 7:09











  • can you post the whole adapter class

    – Kevin Kurien
    Nov 15 '18 at 7:17











  • use Picasso library to set image

    – Anjani Mittal
    Nov 15 '18 at 8:41

















can you add the error?

– Anjani Mittal
Nov 15 '18 at 7:01





can you add the error?

– Anjani Mittal
Nov 15 '18 at 7:01













@ Anjani Mittal , the error is no imageView show in RecyclerView .

– Tzuyu Lin
Nov 15 '18 at 7:09





@ Anjani Mittal , the error is no imageView show in RecyclerView .

– Tzuyu Lin
Nov 15 '18 at 7:09













can you post the whole adapter class

– Kevin Kurien
Nov 15 '18 at 7:17





can you post the whole adapter class

– Kevin Kurien
Nov 15 '18 at 7:17













use Picasso library to set image

– Anjani Mittal
Nov 15 '18 at 8:41





use Picasso library to set image

– Anjani Mittal
Nov 15 '18 at 8:41












3 Answers
3






active

oldest

votes


















0














you have to use String instead of String on constructor.



use



public Function(String name , int icon) { this.icon = icon }


instead of



public Function(String name , int icon) { this.icon = icon }





share|improve this answer































    0














    Use setImageDrawable() method to load local drawables instead of using setImageResource.



    holder.itemImageView.setImageDrawable(context.getResources().getDrawable(icons[position]))





    share|improve this answer


























    • I get NullPointerException error messages.

      – Tzuyu Lin
      Nov 15 '18 at 8:09











    • Then use context.getResources().getDrawable()

      – Vicky
      Nov 15 '18 at 8:16











    • I get "android.content.res.Resources$NotFoundException: Resource ID #0x0 "

      – Tzuyu Lin
      Nov 15 '18 at 8:34











    • Then your @drawable/pic_01 is not in drawable folder which you mentioned in logos.xml

      – Vicky
      Nov 15 '18 at 13:17











    • @tzuyuLin Is it solve your problem?

      – Vicky
      Nov 24 '18 at 7:21



















    0














    try this code 

    private int confirmIcon = {R.drawable.ic_action_green,
    R.drawable.ic_action_yellow,
    R.drawable.ic_action_red};



    switch ("your condition") {
    case 0:
    holder.ivConfirm.setImageResource(confirmIcon[0]);
    break;
    case 1:
    holder.ivConfirm.setImageResource(confirmIcon[1]);
    break;
    case 2:
    holder.ivConfirm.setImageResource(confirmIcon[2]);
    break;


    }





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313966%2fhow-to-show-imageviews-in-recyclerview-android%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      you have to use String instead of String on constructor.



      use



      public Function(String name , int icon) { this.icon = icon }


      instead of



      public Function(String name , int icon) { this.icon = icon }





      share|improve this answer




























        0














        you have to use String instead of String on constructor.



        use



        public Function(String name , int icon) { this.icon = icon }


        instead of



        public Function(String name , int icon) { this.icon = icon }





        share|improve this answer


























          0












          0








          0







          you have to use String instead of String on constructor.



          use



          public Function(String name , int icon) { this.icon = icon }


          instead of



          public Function(String name , int icon) { this.icon = icon }





          share|improve this answer













          you have to use String instead of String on constructor.



          use



          public Function(String name , int icon) { this.icon = icon }


          instead of



          public Function(String name , int icon) { this.icon = icon }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 '18 at 7:16









          dinesh sharmadinesh sharma

          11




          11

























              0














              Use setImageDrawable() method to load local drawables instead of using setImageResource.



              holder.itemImageView.setImageDrawable(context.getResources().getDrawable(icons[position]))





              share|improve this answer


























              • I get NullPointerException error messages.

                – Tzuyu Lin
                Nov 15 '18 at 8:09











              • Then use context.getResources().getDrawable()

                – Vicky
                Nov 15 '18 at 8:16











              • I get "android.content.res.Resources$NotFoundException: Resource ID #0x0 "

                – Tzuyu Lin
                Nov 15 '18 at 8:34











              • Then your @drawable/pic_01 is not in drawable folder which you mentioned in logos.xml

                – Vicky
                Nov 15 '18 at 13:17











              • @tzuyuLin Is it solve your problem?

                – Vicky
                Nov 24 '18 at 7:21
















              0














              Use setImageDrawable() method to load local drawables instead of using setImageResource.



              holder.itemImageView.setImageDrawable(context.getResources().getDrawable(icons[position]))





              share|improve this answer


























              • I get NullPointerException error messages.

                – Tzuyu Lin
                Nov 15 '18 at 8:09











              • Then use context.getResources().getDrawable()

                – Vicky
                Nov 15 '18 at 8:16











              • I get "android.content.res.Resources$NotFoundException: Resource ID #0x0 "

                – Tzuyu Lin
                Nov 15 '18 at 8:34











              • Then your @drawable/pic_01 is not in drawable folder which you mentioned in logos.xml

                – Vicky
                Nov 15 '18 at 13:17











              • @tzuyuLin Is it solve your problem?

                – Vicky
                Nov 24 '18 at 7:21














              0












              0








              0







              Use setImageDrawable() method to load local drawables instead of using setImageResource.



              holder.itemImageView.setImageDrawable(context.getResources().getDrawable(icons[position]))





              share|improve this answer















              Use setImageDrawable() method to load local drawables instead of using setImageResource.



              holder.itemImageView.setImageDrawable(context.getResources().getDrawable(icons[position]))






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 15 '18 at 8:16

























              answered Nov 15 '18 at 7:21









              VickyVicky

              3,70712326




              3,70712326













              • I get NullPointerException error messages.

                – Tzuyu Lin
                Nov 15 '18 at 8:09











              • Then use context.getResources().getDrawable()

                – Vicky
                Nov 15 '18 at 8:16











              • I get "android.content.res.Resources$NotFoundException: Resource ID #0x0 "

                – Tzuyu Lin
                Nov 15 '18 at 8:34











              • Then your @drawable/pic_01 is not in drawable folder which you mentioned in logos.xml

                – Vicky
                Nov 15 '18 at 13:17











              • @tzuyuLin Is it solve your problem?

                – Vicky
                Nov 24 '18 at 7:21



















              • I get NullPointerException error messages.

                – Tzuyu Lin
                Nov 15 '18 at 8:09











              • Then use context.getResources().getDrawable()

                – Vicky
                Nov 15 '18 at 8:16











              • I get "android.content.res.Resources$NotFoundException: Resource ID #0x0 "

                – Tzuyu Lin
                Nov 15 '18 at 8:34











              • Then your @drawable/pic_01 is not in drawable folder which you mentioned in logos.xml

                – Vicky
                Nov 15 '18 at 13:17











              • @tzuyuLin Is it solve your problem?

                – Vicky
                Nov 24 '18 at 7:21

















              I get NullPointerException error messages.

              – Tzuyu Lin
              Nov 15 '18 at 8:09





              I get NullPointerException error messages.

              – Tzuyu Lin
              Nov 15 '18 at 8:09













              Then use context.getResources().getDrawable()

              – Vicky
              Nov 15 '18 at 8:16





              Then use context.getResources().getDrawable()

              – Vicky
              Nov 15 '18 at 8:16













              I get "android.content.res.Resources$NotFoundException: Resource ID #0x0 "

              – Tzuyu Lin
              Nov 15 '18 at 8:34





              I get "android.content.res.Resources$NotFoundException: Resource ID #0x0 "

              – Tzuyu Lin
              Nov 15 '18 at 8:34













              Then your @drawable/pic_01 is not in drawable folder which you mentioned in logos.xml

              – Vicky
              Nov 15 '18 at 13:17





              Then your @drawable/pic_01 is not in drawable folder which you mentioned in logos.xml

              – Vicky
              Nov 15 '18 at 13:17













              @tzuyuLin Is it solve your problem?

              – Vicky
              Nov 24 '18 at 7:21





              @tzuyuLin Is it solve your problem?

              – Vicky
              Nov 24 '18 at 7:21











              0














              try this code 

              private int confirmIcon = {R.drawable.ic_action_green,
              R.drawable.ic_action_yellow,
              R.drawable.ic_action_red};



              switch ("your condition") {
              case 0:
              holder.ivConfirm.setImageResource(confirmIcon[0]);
              break;
              case 1:
              holder.ivConfirm.setImageResource(confirmIcon[1]);
              break;
              case 2:
              holder.ivConfirm.setImageResource(confirmIcon[2]);
              break;


              }





              share|improve this answer




























                0














                try this code 

                private int confirmIcon = {R.drawable.ic_action_green,
                R.drawable.ic_action_yellow,
                R.drawable.ic_action_red};



                switch ("your condition") {
                case 0:
                holder.ivConfirm.setImageResource(confirmIcon[0]);
                break;
                case 1:
                holder.ivConfirm.setImageResource(confirmIcon[1]);
                break;
                case 2:
                holder.ivConfirm.setImageResource(confirmIcon[2]);
                break;


                }





                share|improve this answer


























                  0












                  0








                  0







                  try this code 

                  private int confirmIcon = {R.drawable.ic_action_green,
                  R.drawable.ic_action_yellow,
                  R.drawable.ic_action_red};



                  switch ("your condition") {
                  case 0:
                  holder.ivConfirm.setImageResource(confirmIcon[0]);
                  break;
                  case 1:
                  holder.ivConfirm.setImageResource(confirmIcon[1]);
                  break;
                  case 2:
                  holder.ivConfirm.setImageResource(confirmIcon[2]);
                  break;


                  }





                  share|improve this answer













                  try this code 

                  private int confirmIcon = {R.drawable.ic_action_green,
                  R.drawable.ic_action_yellow,
                  R.drawable.ic_action_red};



                  switch ("your condition") {
                  case 0:
                  holder.ivConfirm.setImageResource(confirmIcon[0]);
                  break;
                  case 1:
                  holder.ivConfirm.setImageResource(confirmIcon[1]);
                  break;
                  case 2:
                  holder.ivConfirm.setImageResource(confirmIcon[2]);
                  break;


                  }






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 10:23









                  veerendranveerendran

                  364




                  364






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53313966%2fhow-to-show-imageviews-in-recyclerview-android%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      這個網誌中的熱門文章

                      Tangent Lines Diagram Along Smooth Curve

                      Yusuf al-Mu'taman ibn Hud

                      Zucchini