Search code examples
androidlistadapter

Using ArrayAdapter to create a list in Android


I'm new at this and an explanation of how things work would be really useful.

So I have this class

        static class ListAdapter extends ArrayAdapter<String>{
            List<String> elements;
            Context context;

            public ListAdapter(Context context, List<String> elements) {
                super(context, 0, elements);
                this.context = context;
                this.elements = elements;
            }


            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
                    convertView = LayoutInflater.from(context).inflate(R.layout.list_item_ex, null, false);
                }

                TextView tvTitle = convertView.findViewById(R.id.tv_title);
                TextView tvDescription = convertView.findViewById(R.id.tv_description);
                ImageView ivExample = convertView.findViewById(R.id.iv_image);

                ivExample.setImageResource(R.drawable.offer_1);
                tvTitle.setText(elements.get(position));
                tvDescription.setText(String.valueOf(position));

                return convertView;
            }
        }

I added in MainActivity a few elements to the list to see how it works, but I'm having a hard time to understand how you can add elements, each with different picture, description etc.


Solution

  • To do this you have To create an ArrayAdapter with a custom class : Here is the steps you have to folow :

    1. create class Custom

      public class Custom {
      int image;
      String title;
      String description;
      //constructeur 
      //getters and setters }
      
    2. create adapter extends ArrayAdapter

      public class CustomArrayAdapter extends ArrayAdapter<Custom> {
      
      ArrayList<Custom> list;
      
          public CustomArrayAdapter(Context context, ArrayList<Custom> list) {
          super(context, R.layout.list_item_ex, list);this.list = list;
      }
      
      
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
      
          Custom custom=getItem(position);
      
          if (convertView == null) {
              convertView = LayoutInflater.from(getContext()).inflate(R.layout.list_item_ex, parent, false);
          }
      
          TextView tvTitle = convertView.findViewById(R.id.tv_title);
          TextView tvDescription = convertView.findViewById(R.id.tv_description);
          ImageView ivExample = convertView.findViewById(R.id.iv_image);
      
          ivExample.setImageResource(custom.getImage());
          tvTitle.setText(custom.getTitle());
          tvDescription.setText(custom.getDescription());
      
          return convertView;
      }
      

      }

    3. fill an ArrayList and then listView.setAdapter(customArrayAdapter);

      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
      
          ListView listView=(ListView) findViewById(R.id.listView) ;
          ArrayList<Custom> list=new ArrayList<>();
          list.add(new Custom(R.mipmap.ic_launcher,"title","desc"));
      
          CustomArrayAdapter customArrayAdapter=new CustomArrayAdapter(this,listView);
      
          listView.setAdapter(customArrayAdapter);}