0% found this document useful (0 votes)
216 views16 pages

ArrayAdapter for Android GridView

This document discusses how to create an adapter with a GridView in Android. It provides the following steps: 1. Create an empty project and add a GridView to the activity_main.xml layout file. 2. Create an XML layout (card_item.xml) for the grid items with text and an image. 3. Create a CourseModel class to store the data for each grid item. 4. Create a CourseGVAdapter class extending ArrayAdapter to populate the GridView. 5. Modify the MainActivity class to retrieve the GridView, populate an arraylist of CourseModel objects, and set the adapter.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
216 views16 pages

ArrayAdapter for Android GridView

This document discusses how to create an adapter with a GridView in Android. It provides the following steps: 1. Create an empty project and add a GridView to the activity_main.xml layout file. 2. Create an XML layout (card_item.xml) for the grid items with text and an image. 3. Create a CourseModel class to store the data for each grid item. 4. Create a CourseGVAdapter class extending ArrayAdapter to populate the GridView. 5. Modify the MainActivity class to retrieve the GridView, populate an arraylist of CourseModel objects, and set the adapter.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

Array Adapter with Grid

View
Output
What is Gridview?
• A GridView is a type of AdapterView that
displays items in a two-dimensional scrolling
grid.
• Items are inserted into this grid layout from
a database or from an array.
• The adapter is used for displaying this
data, setAdapter() method is used to join
the adapter with GridView. 
• The main function of the adapter in GridView
is to fetch data from a database or array and
insert each piece of data in an appropriate
item that will be displayed in GridView.
XML Attributes of GridView
• android:numColumns: This attribute of GridView will be used
to decide the number of columns that are to be displayed in
Grid.
• android:horizontalSpacing: This attribute is used to define
the spacing between two columns of GridView.
• android:verticalSpacing: This attribute is used to specify the
spacing between two rows of GridView.
Step 1: Creating a New Project
• Create an Empty Project and select Java as a your
programming Language
<?xml version="1.0" encoding="utf-8"?>
Step 2: Modify <[Link]

activity_main.x xmlns:android="[Link]
ml file xmlns:tools="[Link]
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
Add GridView
to activity_main <!-- android:numColumns=2 is the number of columns for
Grid View
.xml file. android:horizontalSpacing is the space between
horizontal
grid items.-->
<GridView
android:id="@+id/idGVcourses"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:horizontalSpacing="6dp"
android:numColumns="2"
android:verticalSpacing="6dp" />

</[Link]>
<?xml version="1.0" encoding="utf-8"?>
Step 3: Create an <!--XML implementation of Card Layout-->
XML layout file for <[Link]
GridView xmlns:android="[Link]
xmlns:app="[Link]
android:layout_width="match_parent"
android:layout_height="120dp"
android:layout_gravity="center"
Create an XML file for grid item to android:layout_margin="5dp"
be displayed in GridView. Click on app:cardCornerRadius="5dp"
the app > res > layout > Right- app:cardElevation="5dp">
Click > Layout Resource file and <LinearLayout
then name the file as card_item. android:layout_width="match_parent"
Below is the code for android:layout_height="wrap_content"
android:orientation="vertical">
the card_item.xml file.
Step 3:
Continuation of the <ImageView
XML file android:id="@+id/idIVcourse"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="center"
Note: Please add a small android:src="@mipmap/ic_launcher" />
icon like image in the
imageview inside drawable <TextView
android:id="@+id/idTVCourse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name"
android:textAlignment="center" />

</LinearLayout>

</[Link]>
Step 4: Create a Modal Class for storing
Data
• Modal Class is the JAVA Class that handles data to be added in
each GridView item of GridView. For Creating Modal Class.
• Now click on app > java > apps package name > Right-Click on
it.
• Then Click on New > Java Class.
• Name your Java Class file as CourseModel.
public class CourseModel {

Step 4: Create a Modal//// string course_name for storing course_name


and imgid for storing image id.
Class for storing Data private String course_name;
private int imgid;

public CourseModel(String course_name, int imgid) {


this.course_name = course_name;
[Link] = imgid;
}

public String getCourse_name() {


return course_name;
}

public void setCourse_name(String course_name) {


this.course_name = course_name;
}

public int getImgid() {


return imgid;
}

public void setImgid(int imgid) {


[Link] = imgid;
}
}
Step 5: Create an Adapter Class
• Adapter Class adds the data from Modal Class in each item of
GridView which is to be displayed on the screen. For Creating
Adapter Class.
• Now click on app > java > apps package name > Right-
Click on it.
• Then Click on New > Java Class.
• Name your Java Class file as CourseGVAdapter.
Layout Inflaters
• "Inflating" a view means taking the layout XML and parsing it to
create the view and viewgroup objects from the elements and
their attributes specified within, and then adding the
hierarchy of those views and viewgroups to the parent
ViewGroup.
• When you call setContentView(), it attaches the views it
creates from reading the XML to the activity.
• You can also use LayoutInflater to add views to another
ViewGroup, which can be a useful tool in a lot of
circumstances.
@NonNull
Step 5: Create an Adapter Class @Override
public View getView(int position, @Nullable View
import [Link];
convertView, @NonNull ViewGroup parent) {
import [Link];
View listitemView = convertView;
import [Link];
if (listitemView == null) {
import [Link];
// Layout Inflater inflates each item to be
import [Link];
displayed in GridView.
import [Link];
listitemView =
import [Link];
[Link](getContext()).inflate([Link]
import [Link];
_item, parent, false);
import [Link];
}
import [Link];
CourseModel courseModel = getItem(position);
TextView courseTV =
public class CourseGVAdapter extends
[Link]([Link]);
ArrayAdapter<CourseModel> {
ImageView courseIV =
public CourseGVAdapter(@NonNull Context
[Link]([Link]);
context, ArrayList<CourseModel>
[Link](courseModel.getCourse_name());
courseModelArrayList) {
super(context, 0, courseModelArrayList);
[Link]([Link]());
}
return listitemView;
}
}
Step 6: Modify
the [Link] file
Now in this import [Link];
file, we will import [Link];
perform all import [Link];
backend import [Link];
operations that
will be adding public class MainActivity extends AppCompatActivity {
data to
GridView. GridView coursesGV;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);

coursesGV = findViewById([Link]);
Step 6: Modify ArrayList<CourseModel> courseModelArrayList = new
the [Link] file ArrayList<CourseModel>();
[Link](new CourseModel("DSA",
[Link].ic_gfglogo));
[Link](new CourseModel("JAVA",
Now in this [Link].ic_gfglogo));
file, we will [Link](new CourseModel("C++",
perform all [Link].ic_gfglogo));
backend [Link](new CourseModel("Python",
operations that [Link].ic_gfglogo));
will be adding [Link](new CourseModel("Javascript",
data to [Link].ic_gfglogo));
GridView. [Link](new CourseModel("DSA",
[Link].ic_gfglogo));

CourseGVAdapter adapter = new CourseGVAdapter(this,


courseModelArrayList);
[Link](adapter);
}
}
Output

You might also like