Custom ArrayAdapter with ListView in Android
In the previous article ArrayAdapter in Android with Example, it’s been discussed how the ArrayAdapter works and what are the data sources which can be attached to the ArrayAdapter with ListView. In this article, it’s been discussed how to implement custom ArrayAdapter with the ListView. Have a look at the following image in which a single view in the ArrayAdapter can be customized.
Steps to Implement the Custom ArrayAdapter
Step 1: Create an Empty Activity project
- Create an Empty Activity Android studio project. Refer to Android | How to Create/Start a New Project in Android Studio?
- And make sure to select the programming as Java.
Step 2: Working with the activity_main.xml
- In the activity_main.xml file, the root view is ListView. Invoke the following code into the activity_main.xml file and mention the appropriate ID for the ListView.
Step 3: Creating a custom View for ListView
- Under layout, the folder creates a layout as custom_list_view.xml and invokes the following code.
- For every single item in the ListView this layout creates the following view for every single item in the array adapter.
Step 4: Create a custom class for custom layout
- By creating this custom class we invoke the getter and setter manually for the custom_list_view layout.
- Create a custom class called NumbersView under the package folder of the Application.
- And invoke the following code.
Step 5: Now create a custom ArrayAdapter class of the type NumbersView
- Under the same package name, create a NumbersViewAdapter.java class of the type NumbersView which extends the ArrayAdapter class.
- And invoke the following code inside the NumbersViewAdapter.java file. Comments are added for better understanding.
Step 6: Working with the MainActivity.java file
- In this case, there is a need to create a custom ArrayList of all the items that are Image for ImageView, Text for TextView 1, Text for TextView 2.
Output: Run on Emulator
My Personal Notes arrow_drop_up
Please Login to comment...