Safcode Institute by Sir Safder





  • Custom List View

    XAML CODE: 
    
    
    
    
        
    
        
            
            
    
        
    
    
    JAVA CODE: 
    
    ListView listview;
        String title[] = {"Facebook", "Youtube", "Instagaram"};
        String subtitle[] = {"FB Description","YT Description","InsT Description"};
        int images[] = {R.drawable.mypic, R.drawable.safcode, R.drawable.mypic};
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            listview = findViewById(R.id.lview);
    
            MyAdapter adapter = new MyAdapter(this, title, subtitle, images);
            listview.setAdapter(adapter);
    
    		}
    
    class MyAdapter extends ArrayAdapter {
            Context context;
            String ntitle[];
            String nsubtitle[];
            int nimage[];
    
            MyAdapter(Context c, String title[], String subt[], int img[]) {
                super(c, R.layout.row, R.id.title, title);
                this.context = c;
                this.ntitle = title;
                this.nsubtitle = subt;
                this.nimage = img;
            }
    
            @NonNull
            @Override
            public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
                LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                View row = layoutInflater.inflate(R.layout.row, parent, false);
                ImageView image = row.findViewById(R.id.image);
                TextView mytitle = row.findViewById(R.id.title);
                TextView mysubtitle = row.findViewById(R.id.subtitle);
    
                image.setImageResource(nimage[position]);
                mytitle.setText(ntitle[position]);
                mysubtitle.setText(nsubtitle[position]);
    
    
                return row;
            }
        }



  • You Can Also Watch Our Tutorial