Sajib_Ahmed

ListView Adapter

Mar 28th, 2021
406
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.45 KB | None | 0 0
  1. package com.example.leaderinfo;
  2.  
  3. import android.content.Context;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.BaseAdapter;
  8. import android.widget.ImageView;
  9. import android.widget.TextView;
  10.  
  11. public class CustomAdapter extends BaseAdapter {
  12.     int[] imgs;
  13.     String[] leaderNames;
  14.     Context context;
  15.     private LayoutInflater inflater;
  16.  
  17.     CustomAdapter(Context context, String[] leaderNames,int[] imgs)
  18.     {
  19.         this.context=context;
  20.         this.leaderNames=leaderNames;
  21.         this.imgs=imgs;
  22.     }
  23.     @Override
  24.     public int getCount() {
  25.         return leaderNames.length;
  26.     }
  27.  
  28.     @Override
  29.     public Object getItem(int i) {
  30.         return null;
  31.     }
  32.  
  33.     @Override
  34.     public long getItemId(int i) {
  35.         return 0;
  36.     }
  37.  
  38.     @Override
  39.     public View getView(int position, View convertView, ViewGroup parent) {
  40.         if(convertView ==null)
  41.         {
  42.           inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  43.           convertView = inflater.inflate(R.layout.sample_view,parent,false);
  44.         }
  45.  
  46.         ImageView imageView= (ImageView) convertView.findViewById(R.id.imageViewId);
  47.         TextView textView= (TextView) convertView.findViewById(R.id.leaderName);
  48.         imageView.setImageResource(imgs[position]);
  49.         textView.setText(leaderNames[position]);
  50.         return convertView;
  51.     }
  52. }
  53.  
Advertisement
Add Comment
Please, Sign In to add comment