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

Popular posts from this blog

Overriding fonts

make a java file with this code import java.lang.reflect.Field; import android.content.Context; import android.graphics.Typeface; public final class FontsOverride {     public static void setDefaultFont(Context context,             String staticTypefaceFieldName, String fontAssetName) {         final Typeface regular = Typeface.createFromAsset(context.getAssets(),                 fontAssetName);         replaceFont(staticTypefaceFieldName, regular);     }     protected static void replaceFont(String staticTypefaceFieldName,             final Typeface newTypeface) {         try {             final F...

Navigation drawer, material design theme

Aim: To make an android app with navigation drawer, material design theme Feat: Toolbar is used instead of the ActionBar, When the user runs the app for the time , he/she can see the drawer opened on the launch of the app, also rotation doesn't effect the drawer (whose layout is a fragment). Used: Fragments in both Code:   MainActivity.java ______________________________________________________________________________________________________ DrawerList.Java______________________________________________________________________________________________ package cts.fastester; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.widget.DrawerLayout; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.widget.Toolbar; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGr...