Custom ListView

ListView is a type of user interface and a view group that provide a view of list with scroll able items.The list items are auto inserted to the list using an Adapter. Adapter is bridge that coordinate between code of different nature.Here it insert content from a Java source to XML source. Java source may be an array or database query and XML are ListView and Spinner etc.Adapter will pick data items from Java source and place it as XML Spinner or ListView contents.
For simple list view read the blog. Today we will discuss to customize the list view according to our data and requirement.


AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.ezeesoltech.customadapter">

  <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      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>
MainActivity.java
public class MainActivity extends AppCompatActivity {
  ArrayList<EmployeeItem> employeeList;
  ListView listView;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      listView=findViewById(R.id.listview);
      employeeList=new ArrayList<>();
      EmployeeItem obj1=new EmployeeItem(R.drawable.tehmina,01,"Tehmina","Rawalpindi","Project Manager");
      employeeList.add(obj1);
      employeeList.add(new EmployeeItem(R.drawable.saba,02,"Saba","Islamabad","Android Developer"));
      employeeList.add(new EmployeeItem(R.drawable.safa,03,"Safa","Peshawar","Sr. Android Developer"));
      employeeList.add(new EmployeeItem(R.drawable.sabahat,04,"Sabahat","Quetta","Graphic Designer"));
      employeeList.add(new EmployeeItem(R.drawable.sania,05,"Sania","Karachi","Marketing Manager"));
      listView.setAdapter(new CustomerAdapter(MainActivity.this,employeeList));
  }
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <ListView
      android:id="@+id/listview"
      android:scrollbars="none"
      android:layout_width="match_parent"
      android:layout_height="match_parent" />

</LinearLayout>
EmployeeItem.java
public class EmployeeItem {
  int profile, id;
  String name,address,designation;

  public EmployeeItem(int profile, int id, String name, String address, String designation) {
      this.profile = profile;
      this.id = id;
      this.name = name;
      this.address = address;
      this.designation = designation;
  }

  public int getProfile() {
      return profile;
  }

  public int getId() {
      return id;
  }

  public String getName() {
      return name;
  }

  public String getAddress() {
      return address;
  }

  public String getDesignation() {
      return designation;
  }
}
employee_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent">

  <ImageView
      android:id="@+id/profile"
      android:layout_width="50dp"
      android:layout_height="50dp"
      android:layout_margin="5dp"
      android:src="@mipmap/ic_launcher" />

  <TextView
      android:id="@+id/name"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_margin="5dp"
      android:layout_toRightOf="@+id/profile"
      android:text="name"
      android:textColor="@color/colorPrimaryDark" />

  <TextView
      android:id="@+id/designation"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/name"
      android:layout_margin="5dp"
      android:layout_toRightOf="@+id/profile"
      android:text="name" />

  <TextView
      android:id="@+id/address"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_below="@+id/designation"
      android:layout_margin="5dp"
      android:layout_toRightOf="@+id/profile"
      android:text="name" />

</RelativeLayout>
CustomAdapter.java
public class CustomerAdapter extends ArrayAdapter<EmployeeItem> {
  Context ctx;
  ArrayList<EmployeeItem> list;

  public CustomerAdapter(Context context, ArrayList<EmployeeItem> list) {
      super(context, R.layout.employee_item, list);
      ctx = context;
      this.list = list;
  }

  @NonNull
  @Override
  public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
      if(convertView==null){
          LayoutInflater inflater=LayoutInflater.from(ctx);
          convertView=inflater.inflate(R.layout.employee_item,null);
      }
      ImageView profile=convertView.findViewById(R.id.profile);
      TextView name=convertView.findViewById(R.id.name);
      TextView designation=convertView.findViewById(R.id.designation);
      TextView address=convertView.findViewById(R.id.address);

      EmployeeItem employeeItem=list.get(position);
      profile.setImageResource(employeeItem.getProfile());
      name.setText(employeeItem.getName());
      designation.setText(employeeItem.getDesignation());
      address.setText(employeeItem.getAddress());
      return convertView;
  }
}

Comments