Android ArrayList

ArrayList is a data structure to store and operate on a collection of values. It is a resizable-array implementation of List interface. Most of its methods run in linear time (O(n)). Below are some useful methods of the ArrayList.

-add(String object) appends a new item to end of the list.
-add(String object,int index) insert a new item to the list at the specified index.
-clear() removes all items of the list.
-get(int index) returns the item at the specified index.
-remove(int index) removes the item at the specified index from the list.

ArrayList doesn't have its own methods to sort or to reverse its items. However, you can use the sort() and reverse() methods of the Collections class to sort and reverse items of the ArrayList.



Now to have a workable example application of using ArrayList in Android, create a new Android project. Then modify the activity_main.xml to add one ListView, and two Buttons as shown below. The ListView displays the content of an ArrayList that contains names of twelve months. The first button is clicked to sort the ArrayList and the second button is clicked to reverse it.

<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=".MainActivity" >

   <ListView
     android:id="@+id/list"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
      android:layout_above="@+id/bgroup"
  
     />
  
<LinearLayout
    android:id="@+id/bgroup"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:layout_alignParentBottom="true"
    >
   <Button
     android:id="@+id/bt_sort"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Sort"
     android:onClick="sortList"
    
   />
   <Button
     android:id="@+id/bt_reverse"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Reverse"
     android:onClick="reverseList"
    
   />
</LinearLayout>

</RelativeLayout>


Then create another layout file called list_item.xml. It is a row template of the ListView.

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout  
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:padding="5dp" >
  
<TextView
     android:id="@+id/txt_month"
     android:layout_width="wrap_content"
     android:layout_height="match_parent"
     android:layout_marginRight="10dp"
       />

  
</RelativeLayout>


In the MainActivity class, you will write code to create an object of ArrayList, add items to it, make a reference to the ListView, create an ArrayAdapter object, and set the adapter to the ListView, and handle the button-click events.


  package com.example.androidexample;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
ArrayList<String> alist;
ListView listview;
ArrayAdapter<String> aa;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//create ArrayList object
alist=new ArrayList<String>();
//add 12 months to the list
alist.add("January");
alist.add("February");
alist.add("March");
alist.add("May");
alist.add("April");
alist.add("June");
alist.add("July");
alist.add("August");
alist.add("September");
alist.add("Octover");
alist.add("November");
alist.add("December");
listview=(ListView)findViewById(R.id.list);
aa=new ArrayAdapter<String>(this,R.layout.list_item,R.id.txt_month, alist);
listview.setAdapter(aa);


}


public void sortList(View view){
//sort list
Collections.sort(alist);
aa.notifyDataSetChanged();
}
public void reverseList(View view){
//reverse list
Collections.reverse(alist);
aa.notifyDataSetChanged();

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

Android-ArrayList

Posted by: Dara | post date: 08-28-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.