Android ListView is a view which groups several items and display them in vertical scrollable list. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database.
An adapter actually bridges between UI components and the data source that fill data into UI Component. Adapter holds the data and send the data to adapter view, the view can takes the data from adapter view and shows the data on different views like as spinner, list view, grid view etc.
The ListView and GridView are subclasses of AdapterView and they can be populated by binding them to an Adapter, which retrieves data from an external source and creates a View that represents each data entry.
Android provides several subclasses of Adapter that are useful for retrieving different kinds of data and building views for an AdapterView ( i.e. ListView or GridView). The common adapters are ArrayAdapter,Base Adapter, CursorAdapter, SimpleCursorAdapter,SpinnerAdapter and WrapperListAdapter. We will see separate examples for both the adapters.
MainActivity.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:background="@color/white"
android:layout_height="match_parent"
android:padding="5dp"
tools:context=".ListView_Activity">
<GridView
android:layout_width="match_parent"
android:numColumns="2"
android:layout_height="match_parent"
android:id="@+id/list_view"/>
</LinearLayout>
MainActivity.Java
package com.example.demo9;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.Toast;
import com.example.demo9.Adapter.MyAdapater;
public class ListView_Activity extends AppCompatActivity {
private GridView list_view;
String[] city={"Surat","mumbai","Delhi","vapi","Mp","UP","Kolkata","Surat","mumbai"};
int[] images={R.drawable.ic_launcher_background,R.drawable.img,R.drawable.ic_launcher_background,R.drawable.ic_launcher_background,R.drawable.ic_launcher_background,R.drawable.ic_launcher_background,R.drawable.ic_launcher_background,R.drawable.ic_launcher_background,R.drawable.ic_launcher_background};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
list_view=findViewById(R.id.list_view);
MyAdapater adapater =new MyAdapater(ListView_Activity.this,city,images);
list_view.setAdapter(adapater);
list_view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
}
Adapter.Java
package com.example.demo9.Adapter;
import android.app.Activity;
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 com.example.demo9.ListView_Activity;
import com.example.demo9.R;
public class MyAdapater extends BaseAdapter {
Activity activity;
String[] city;
int[] images;
public MyAdapater(ListView_Activity listView_activity, String[] city, int[] images) {
activity=listView_activity;
this.city=city;
this.images=images;
}
@Override
public int getCount() {
return city.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = LayoutInflater.from(activity).inflate(R.layout.list_item,parent,false);
TextView txt=view.findViewById(R.id.txt);
ImageView img=view.findViewById(R.id.img);
txt.setText(city[position]);
img.setImageResource(images[position]);
return view;
}
}
Item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
app:cardBackgroundColor="@color/white"
app:cardCornerRadius="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp">
<ImageView
android:id="@+id/img"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/txt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="ABC"
android:textColor="@color/black"
android:textSize="20dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
#RecyclerView = https://applications-code.blogspot.com/2022/01/android-recyclerview-example.html
0 Comments