Skip to main content

Create custom listview

  • Prepare data sources [Images+Titles+Description] to displayed in each row
  • Define single row appearance in layout with ids for every element you want to appear different.
  • Create Custom Adpater

extend a class to ArrayAdapter, define a constructor, override getView() method

  • Add Listview to the layout , and add reference of it in the respective java class.
  • In the custom list adapter add constructor with passing every element of the row

Context context;
    int[] images;
    String[] titles;
    String[] location;
    String[] dates;
    String[] times;
    public MainListAdapter(Context context, String[] titles,int[] images,String[] location,String[] dates,String[] times) {
        super(context, R.layout.singlerowmain,R.id.listTitle,titles);
        this.context=context;
        this.images=images;
        this.titles=titles;
        this.location=location;
        this.dates=dates;
        this.times=times;
    }

  • add getView() method in the adapter and add a reference of LayoutInflater in the getView()

LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

  • Create a view that is row in same getView()

View row=inflater.inflate(R.layout.singlerowmain,parent,false);

  • add reference of each element of the row inside getView()

public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater= (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row=inflater.inflate(R.layout.singlerowmain,parent,false);
        ImageView iv=(ImageView)row.findViewById(R.id.listImage);
        TextView title=(TextView)row.findViewById(R.id.listTitle);
        TextView loc=(TextView)row.findViewById(R.id.listLocation);
        TextView date=(TextView)row.findViewById(R.id.listDate);
        TextView time=(TextView)row.findViewById(R.id.listTime);

        iv.setImageResource(images[position]);
        title.setText(titles[position]);
        loc.setText(location[position]);
        date.setText(dates[position]);
        time.setText(times[position]);

        return row;
    }

Comments