Home Android ArrayAdapter : ListView and Array

Android ArrayAdapter : ListView and Array

Android ArrayAdapter : ListView and Array




Android Arrayadapter Listview

Introduction

Adapters like ArrayAdapter help bridge the gap between model data and the adapterview,two very different stuff with different responsibilities.Our model data represents our data source.It maybe from a database or a simple data collection,like say array or arraylist.The adapterview represents the visual representations.They are view children hence are concerned with showing data.Adapterviews include ListViews,GridViews and spinner. Our adapter shall help link these two.

ArrayAdapters work with an array or java.util.List instance.

It takes :

  1. Context.
  2. Resource ID of the view to be used in the adapterview.
  3. List of data to be bound.

What we do :

  • Fill our adapterview with data.
  • Our adapterview in this case is a simple ListView.
  • We use ArrayAdapter as our adapter.
  • So we instantiate and set to our listview using the setAdapter() method.
  • While instantiating our adapter we need to pass a context,layout and data source.
  • We handle our itemclicks and show a simple toast message.

 

What You do :

  • Create a project in android studio.
  • Give it a name and choose minimum and target SDKs.

 

 

SECTION 1 : Our Dependencies

Build.Gradle

  • Android Studio has added for us dependencies for AppCompat and Design support libraries.
  • Note we are subclassing AppCompatActivity to make our MainActivity class an activity.

 

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:24.2.0'


}

 

 

SECTION 2 : Our Activity

Our MainActivity

Main Responsibility : LAUNCH OUR APP.

  • It extends AppcompatActivity hence is an activity.
  • Activities are the entry point of android applications.This is no exception.
  • It loads our activity layout.
  • Our activity layout has our ListView.
  • We therefore reference that ListView and set its adapter.
  • We first instantiate our array adapter,passing in context,layout and data source.
  • Our array adapter is of generic type string.
  • To set our adapter to listview we use the setAdapter() method.
  • Our data source is a simple array.

package com.tutorials.hp.listviewarray;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

 ListView lv;
 String[] spacecrafts={"Juno","Hubble","Casini","WMAP","Spitzer","Pioneer","Columbia","Challenger","Apollo","Curiosity"};

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);

 lv= (ListView) findViewById(R.id.lv);

 //ADAPTER
 ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,spacecrafts);
 lv.setAdapter(adapter);

 //LISTENER
 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
 @Override
 public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
 Toast.makeText(MainActivity.this, spacecrafts[i], Toast.LENGTH_SHORT).show();
 }
 });

 }

}

 

SECTION 3 : Our Layouts

ActivityMain.xml Layout.

  • Inflated as our activity's view.
  • Add ListView here.

Our XML Layout

<?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: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.tutorials.hp.listviewarray.MainActivity">

    <ListView
        android:id="@+id/lv"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
         />
</RelativeLayout>

 

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Leave a Comment