Listview Introduction:-

Listview  helps you to displaying the data in the form of a scrollable list . it is implemented by importing android.widget.ListView class. it is a default scrollable so we do not need to use other scroll view.

ListView VS RecyclerView

Since Android 5.0 came out, ListViews have an alternative — RecyclerViews. These offer more options and functionality, but are slightly more complex to use. For now, we’ll use ListView and in an upcoming tutorial,
we’ll make the same example app using a RecyclerView for comparison.

 

Here is the Demo video of Listview:-

 

In the activity_main.xml file in layout directory, add the Listview widget.

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context=".MainActivity">

<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#58f"
android:dividerHeight="1dp"
android:listSelector="#D81B69"
/>

</RelativeLayout>
 
Create a custom layout list_item.xml file with following code.
 

Adapter Used in Listview:-

 

Adapter is bridge between UI component and Data source which  helps to fulfill the data in UI component.

In Android Mainly  two types of  Adapter is  used:-

 a. Array Adapter

b. Custom(Base)  Adapter

 
 

1. Array Adapter

we display  name of student in listview using Array Adapter.

  Output as below:-

create a new layout  name list_item and add below code in  list_item.xml

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearlayout"
android:layout_margin="3dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<TextView
android:id="@+id/textViewName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#000"
android:layout_margin="5dp"
android:gravity="center_vertical"
android:textSize="18sp"/>


</LinearLayout>

 

Finally Add the following code in MainActivity.java

package com.androidcreation.listview;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity
{

ListView simpleList;
String studentList[] = {"Joseph","Cameron","William","Jake","Joshua","Jamie","Lewis"};
int image[] = {R.drawable.josheph, R.drawable.cameron, R.drawable.william, R.drawable.jake,
R.drawable.joshua,R.drawable.jamie, R.drawable.lewis};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
simpleList = findViewById(R.id.listview);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.list_item2, R.id.textViewName, studentList);
simpleList.setAdapter(arrayAdapter);

}
}


2. Custom(Base) Adapter

Custom Adapter  that populate custom row of android listview.

we display  name of student with image in listview using Custom Adapter.

 

create a new layout  name list_item_image and add below code in  list_item_image.xml

list_item_image.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearlayout"
android:layout_margin="3dp"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
android:id="@+id/imageView"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:scaleType="centerCrop"
android:layout_marginEnd="10dp"
android:contentDescription="Icon" />

<TextView
android:id="@+id/textViewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:layout_margin="5dp"
android:layout_toEndOf="@id/imageView"
android:layout_toRightOf="@id/imageView"
android:gravity="center_vertical"
android:textSize="18sp"/>


</LinearLayout>

 

now create CustomAdapter.java class which extends BaseAdapter Class. This is basically a class that sits between our main Java file and our row layout file — we use it to populate the rows of the ListView with data from the Java file. and  add the below code:-

 

CustomAdapter.java

package com.androidcreation.listview;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomAdapter extends BaseAdapter {
Context context;
String studentList[];
int image[];
LayoutInflater inflter;

public CustomAdapter(Context applicationContext, String[] studentList, int[] image) {
this.context = context;
this.studentList = studentList;
this.image = image;
inflter = (LayoutInflater.from(applicationContext));
}

@Override
public int getCount() {
return studentList.length;
}

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

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

@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.list_item, null);
TextView name = view.findViewById(R.id.textViewName);
ImageView imageView = view.findViewById(R.id.imageView);
name.setText(studentList[i]);
imageView.setImageResource(image[i]);
return view;
}
}

 

and finally MainActivity.java class code as below:-


MainActivity.java

package com.androidcreation.listview;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity
{

ListView simpleList;
String studentList[] = {"Joseph","Cameron","William","Jake","Joshua","Jamie","Lewis"};
int image[] = {R.drawable.josheph, R.drawable.cameron, R.drawable.william, R.drawable.jake,
R.drawable.joshua,R.drawable.jamie, R.drawable.lewis};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
simpleList = findViewById(R.id.listview);
CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), studentList, image);
simpleList.setAdapter(customAdapter);

}
}
0 CommentsClose Comments

Leave a comment

shares

Newsletter Subscribe

Get the Latest Posts & Articles in Your Email

We Promise Not to Send Spam:)