Android Array Adapter
Android Array Adapter
In Android Array Adapter , any time we want to show a vertical list of scroll able items we will use a ListView which has data populated using an Adapter. The simplest adapter to use is called an ArrayAdapter because the adapter converts an ArrayList of objects into View items loaded into the ListView container.
The ArrayAdapter fits in between an ArrayList (data source) and the ListView (visual representation) and configures two aspects:
- Which array to use as the data source for the list
- How to convert any given item in the array into a corresponding View object
Note as shown above that there are other data sources besides an ArrayAdapter such as the CursorAdapter which instead binds directly to a result set from a Local SQLite Database
You can use this adapter when your data source is an array. By default, ArrayAdapter creates a view for each array item by calling toString() on each item and placing the contents in a TextView. Consider you have an array of strings you want to display in a ListView, initialize a new ArrayAdapter using a constructor to specify the layout for each string and the string array –
ArrayAdapter adapter = new ArrayAdapter<String>(this,R.layout.ListView,StringArray);
Here are arguments for this constructor −
- First argumentthis is the application context. Most of the case, keep it this.
- Second argument will be layout defined in XML file and havingTextView for each string in the array.
- Final argument is an array of strings which will be populated in the text view.
Once you have array adapter created, then simply call setAdapter() on your ListView object as follows −
ListView listView = (ListView) findViewById(R.id.listview);listView.setAdapter(adapter);
Step By Step Tutorial
Step 1 : Open the Android studio.
Step 2 : Click on New Project option
Step 3 : Give the name of your application. (for example: ArrayList). Give the Company domain name or you can leave it as. And give the project location.
Step 4. After New Project Name Screen there come where you have to choose the particular android version. It implies/ tells us the compatibility of our project in different android platforms. To Study about Available Android Version Click here.
Step 5. In this part you have to select from the provided templates and the result project will be as similar that is displayed on the particular template as follows. It Act as the MainActivity of this Project.
Here we Select the Empty Activity where we can do all the coding.
Step 6. Give Activity Name, Layout Name, and title to selected Activity. In this I name It “ListDisplay” and the layout name as “activity_list_display”
Step 7. After you click on finish button on the last screen then the android will start to build the empty project for you. where you have to design your own layout and implement your own function. once finish it redirect us to the ListDisplay.java page. where all the coding part is done.
Step 8. Now we are working on the Layout part where we create a list view to display the windowArray List. as shown in the snapshotI use Simple List View to implement.
<ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/window_list"> </ListView>
Step 9.
To run the app from Android studio, open one of your project’s activity files and click Run icon from the tool bar. Android studio installs the app on your AVD and starts it and if everything is fine with your set-up and application, it will display following Emulator window –
Here i use the android phone to run the project because Android Emulator takes so much time to process and sometimes it stuck in the process.
process to run Android project in android phone directly via USB Cable
1. Install the drivers of you phone known as MTP(MEDIA TRANSPORT PROTOCOL) from official site.
2.Enable Development mode through 7 times click on version under About Phone Setting.
3.Now open development mode and enable the usb debugging.
that’s it now your android studio automatically pick your mobile details. until unless the android SDK is not correctly installed.
Coding:
ListDisplay.java
package com.example.nexus.arraylist; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.ListViewCompat; import android.widget.ArrayAdapter; import android.widget.ListView; public class ListDisplay extends AppCompatActivity { //Array of String String[] windowArray = {"Win XP","Win 7","Ubuntu","Mac OSX","Win 8","Win 8.1","Win 10","Linux","Red Hat"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list_display); ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.activity_listview, windowArray); ListView listView = (ListView)findViewById(R.id.window_list); listView.setAdapter(adapter); } }
activity_list_display.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_list_display" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.nexus.arraylist.ListDisplay"> <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/window_list"> </ListView> </RelativeLayout>
Now Save your Project and RUN IT the output is same as the
Output
String[] windowArray = {"Win XP","Win 7", String[] windowArray = {"Win XP","Win 7", "Ubuntu","Mac OSX","Win 8","Win 8.1", "Ubuntu","Mac OSX","Win 8","Win 8.1", "Win 10","Linux"}; "Win 10","Linux","Red Hat"};
it took only in seconds to implement the changes because after 2.1.1 update in android studio it enables insta RUN Technique. so if you do any changes then there is no need for clear and rebuild the project just click on RUN button and done.