Android Custom ListView Example with BaseAdapter – Android Tutorials

Please follow and like us:

Android Custom ListView Example with BaseAdapter

 

Download Simple Android Custom ListView Example with BaseAdapter. Here we can customize view of list items and set values using custom adapter. Let’s see step by step Custom ListView with BaseAdapter example.

 

Android Custom ListView Example using BaseAdapter

 

package com.nkdroid.listview.demo;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {


    private ListView lvProfiles;
    private MyAppAdapter myAppAdapter;
    private ArrayList<ProfileModel> profileModelArrayList;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        profileModelArrayList=new ArrayList<>();
        profileModelArrayList.add(new ProfileModel("Nirav","India",R.drawable.male));
        profileModelArrayList.add(new ProfileModel("Surabhi","India",R.drawable.female));
        profileModelArrayList.add(new ProfileModel("Martins","Nigeria",R.drawable.male));
        profileModelArrayList.add(new ProfileModel("Steven","USA",R.drawable.male));
        profileModelArrayList.add(new ProfileModel("Akeba","USA",R.drawable.female));
        profileModelArrayList.add(new ProfileModel("Ravi","India",R.drawable.male));
        profileModelArrayList.add(new ProfileModel("Nirav","India",R.drawable.male));
        profileModelArrayList.add(new ProfileModel("Surabhi","India",R.drawable.female));
        profileModelArrayList.add(new ProfileModel("Martins","Nigeria",R.drawable.male));
        profileModelArrayList.add(new ProfileModel("Steven","USA",R.drawable.male));
        profileModelArrayList.add(new ProfileModel("Akeba","USA",R.drawable.female));
        profileModelArrayList.add(new ProfileModel("Ravi","India",R.drawable.male));
        profileModelArrayList.add(new ProfileModel("Nirav","India",R.drawable.male));
        profileModelArrayList.add(new ProfileModel("Surabhi","India",R.drawable.female));
        profileModelArrayList.add(new ProfileModel("Martins","Nigeria",R.drawable.male));
        profileModelArrayList.add(new ProfileModel("Steven","USA",R.drawable.male));
        profileModelArrayList.add(new ProfileModel("Akeba","USA",R.drawable.female));
        profileModelArrayList.add(new ProfileModel("Ravi","India",R.drawable.male));


        lvProfiles= (ListView)findViewById(R.id.lvProfiles);
        myAppAdapter=new MyAppAdapter(profileModelArrayList,MainActivity.this);
        lvProfiles.setAdapter(myAppAdapter);


    }

    public class MyAppAdapter extends BaseAdapter {

        public class ViewHolder {
            TextView username,country;
            ImageView profilePic;

        }

        public ArrayList<ProfileModel> profileList;

        public Context context;


        private MyAppAdapter(ArrayList<ProfileModel> apps, Context context) {
            this.profileList = apps;
            this.context = context;

        }

        @Override
        public int getCount() {
            return profileList.size();
        }

        @Override
        public Object getItem(int position) {
            return position;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

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

            View rowView = convertView;
            ViewHolder viewHolder;

            if (rowView == null) {
                LayoutInflater inflater = getLayoutInflater();
                rowView = inflater.inflate(R.layout.item_profile, parent,false);
                // configure view holder
                viewHolder = new ViewHolder();
                viewHolder.profilePic = (ImageView) rowView.findViewById(R.id.imgProfile);
                viewHolder.username = (TextView) rowView.findViewById(R.id.txtUsername);
                viewHolder.country = (TextView) rowView.findViewById(R.id.txtCountry);
                rowView.setTag(viewHolder);

            } else {
                viewHolder = (ViewHolder) convertView.getTag();
            }

            viewHolder.username.setText(profileList.get(position).getUsername()+"");
            viewHolder.country.setText(profileList.get(position).getCountry()+"");
            Glide.with(MainActivity.this).load(profileList.get(position).getProfilePic()).into(viewHolder.profilePic);

            return rowView;


        }


    }
}
package com.nkdroid.listview.demo;

/**
 * Created by nirav on 09/01/17.
 */

public class ProfileModel {

    private String username;

    private String country;

    private int profilePic;

    public ProfileModel(String username, String country, int profilePic) {
        this.username = username;
        this.country = country;
        this.profilePic = profilePic;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getCountry() {
        return country;
    }

    public void setCountry(String country) {
        this.country = country;
    }

    public int getProfilePic() {
        return profilePic;
    }

    public void setProfilePic(int profilePic) {
        this.profilePic = profilePic;
    }
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
android:orientation="vertical"
    tools:context="com.nkdroid.listview.demo.MainActivity">

    <ListView
        android:padding="4dp"
        android:drawSelectorOnTop="true"
        android:id="@+id/lvProfiles"
        android:divider="#787878"
        android:dividerHeight="1dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"

    android:layout_width="match_parent"
    android:background="#fff"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imgProfile"
        android:layout_width="100dp"
        android:layout_height="100dp" />

    <LinearLayout
        android:gravity="center"
        android:padding="16dp"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:textColor="#000"
            android:id="@+id/txtUsername"
            android:textSize="18sp"
            android:textStyle="bold"
            android:text="Username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />


        <TextView
            android:textColor="#000"
            android:id="@+id/txtCountry"
            android:textSize="14sp"
            android:text="country"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.nkdroid.listview.demo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Hope you guys like our simple tutorial about custom listview with base adapter example. stay connected for latest android tutorials.

nirav kalola