ListView in Android Studio Java
![](https://blog.imraju.com/wp-content/uploads/2020/06/Implementing-List-View-in-Android-App-using-Java.png)
Using a Custom Adapter
This time we need to change your activity_list_item.xml file. Just copy the bellow code and paste
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_margin="5dp"
app:cardCornerRadius="6dp"
app:cardElevation="2dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:orientation="horizontal"
android:weightSum="4">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="@mipmap/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="3"
android:layout_marginLeft="5dp"
android:weightSum="3">
<TextView
android:id="@+id/item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left|center"
android:text="Fruit name"
android:textStyle="bold"
android:textColor="#000000" />
<TextView
android:id="@+id/subitem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_gravity="center"
android:text="fruit list"
android:textSize="12dp"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
First we create a java class name CustomAdapter. Inside CustomAdapter.java file copy and paste the bellow code without changing package name
package com.andrious.listview;
/**
* Created by MD.ISRAFIL MAHMUD on 06/24/2020.
*/
import android.content.Context;
import android.media.Image;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.zip.Inflater;
public class CustomAdapter extends BaseAdapter {
Context context;
String Item[];
String SubItem[];
int flags[];
LayoutInflater inflter;
public CustomAdapter(Context applicationContext, String[] Item, String[] SubItem , int[] flags) {
this.context = context;
this.Item = Item;
this.SubItem = SubItem;
this.flags = flags;
inflter = (LayoutInflater.from(applicationContext));
}
@Override
public int getCount() {
return Item.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.activity_list_item, null);
TextView item = (TextView) view.findViewById(R.id.item);
TextView subitem = (TextView) view.findViewById(R.id.subitem);
ImageView image = (ImageView) view.findViewById(R.id.image);
item.setText(Item[i]);
subitem.setText(SubItem[i]);
image.setImageResource(flags[i]);
return view;
}
}
Few change into our MainActivity.java file. Create two string type array one for Item name and one for Item description. One int type array for holding drawable food image name. Look like this
String Item[] = {"Apple", "Banana", "Lemon", "Cherry", "Strawberry", "Avocado"};
String SubItem[] = {"The apple tree is a deciduous tree in the rose family best known for its sweet, pomaceous fruit, the apple.",
"The banana is an edible fruit – botanically a berry – produced by several kinds of large herbaceous flowering plants in the genus Musa.",
"The lemon, Citrus limon Osbeck, is a species of small evergreen tree in the flowering plant family Rutaceae, native to Asia.",
"A cherry is the fruit of many plants of the genus Prunus, and is a fleshy drupe.",
"The garden strawberry is a widely grown hybrid species of the genus Fragaria, collectively known as the strawberries.",
"The avocado is a tree, long thought to have originated in South Central Mexico, classified as a member of the flowering plant family Lauraceae."};
Now we need some pictuer into our Item for creating int type array for holding drawable image name for each Item.
strawberrie lemon avocado banana cherry apple
Download this images and paste into your drawable folder. You can also be able to download images from google or any other site for your satisfaction.
Now we declare int array for holding drawable image name for each Item.
int flags[] = {R.drawable.apple, R.drawable.banana, R.drawable.lemon, R.drawable.cherry, R.drawable.strawberrie, R.drawable.avocado};
Now, we just need to connect this adapter to a ListView to be populated:
CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), Item,SubItem, flags);
ListView simpleList = (ListView)findViewById(R.id.ListView);
simpleList.setAdapter(customAdapter);
simpleList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),Item[position],Toast.LENGTH_SHORT).show();
}
});
Our MainActivity.java file look this
package com.andrious.listview;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
String Item[] = {"Apple", "Banana", "Lemon", "Cherry", "Strawberry", "Avocado"};
String SubItem[] = {"The apple tree is a deciduous tree in the rose family best known for its sweet, pomaceous fruit, the apple.",
"The banana is an edible fruit – botanically a berry – produced by several kinds of large herbaceous flowering plants in the genus Musa.",
"The lemon, Citrus limon Osbeck, is a species of small evergreen tree in the flowering plant family Rutaceae, native to Asia.",
"A cherry is the fruit of many plants of the genus Prunus, and is a fleshy drupe.",
"The garden strawberry is a widely grown hybrid species of the genus Fragaria, collectively known as the strawberries.",
"The avocado is a tree, long thought to have originated in South Central Mexico, classified as a member of the flowering plant family Lauraceae."};
int flags[] = {R.drawable.apple, R.drawable.banana, R.drawable.lemon, R.drawable.cherry, R.drawable.strawberrie, R.drawable.avocado};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), Item,SubItem, flags);
ListView simpleList = (ListView)findViewById(R.id.ListView);
simpleList.setAdapter(customAdapter);
simpleList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),Item[position],Toast.LENGTH_SHORT).show();
}
});
}
}
When you run this your application will look like this
![](https://blog.imraju.com/wp-content/uploads/2020/06/Screenshot_1593017136-576x1024.png)
Leave a Comment
(0 Comments)