Android ListView example using ListActivity




Most of the mobile applications will use List view. It is simple to create one using ListActivity class in Android.

ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However,we can customize the screen layout by setting our own view layout with setContentView() in onCreate(). 
To do this, our layout MUST contain a ListView object with the id "@android:id/list". ( NOT android:id="@+id/list" ).
if we define a ListView with the android:id attribute set to @+id/list then it will throw below RuntimeException


java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'


I am using ArrayAdapter to render the list. Data to be displayed in the List. We can use lists or arrays of custom objects. 

1). To render it using default layout we need to create a <TextView> in the layout xml file.

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="20sp" >
</TextView>


Using ArrayAdapter we can render the data in List view.

package com.example.listviewexample;

import java.util.Arrays;
import java.util.List;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author Prabu
 *
 */
public class MainActivity extends ListActivity {

 final List<String> elements = Arrays.asList("Element 1", "Element 2", "Element 3",
   "Element 4", "Element 5");

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  //setContentView(R.layout.activity_main);
  ListView listView = getListView();
  listView.setTextFilterEnabled(true); 
  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
    R.layout.activity_main,elements);
  listView.setAdapter(adapter);
  listView.setOnItemClickListener(new OnItemClickListener() {
   public void onItemClick(AdapterView<?> parent, View view,
     int position, long id) {
    Toast.makeText(getApplicationContext(),
      ((TextView) view).getText(), Toast.LENGTH_SHORT).show();
   }
  });
 }
}

Output:

Source Code:
ListView_default_layout.zip


2). To render the List inside a Linear Layout, we need to have a <TextView> and a <ListView> with the id "@android:id/list" as stated at the beginning of this tutorial.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/output"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#ffffff"
        android:padding="1px"/>

    <ListView
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

The ArrayAdapter constructor will take different arguments in this case,

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
    R.layout.activity_main,R.id.output,elements);



We need to pass the id of the TestView element additionally.
package com.example.listviewexample;

import java.util.Arrays;
import java.util.List;

import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @author Prabu
 *
 */
public class MainActivity extends ListActivity {

 final List<String> elements = Arrays.asList("Element 1", "Element 2", "Element 3",
   "Element 4", "Element 5");

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  ListView listView = getListView();
  listView.setTextFilterEnabled(true); 
  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
    R.layout.activity_main,R.id.output,elements);
  listView.setAdapter(adapter);
 }
}


Output:



Source code:
ListView_custom_layout.zip

 



Reactions:

0 comments :

Post a Comment