List View using Array Adapters – Part III : Using Custom Row Layouts

Till now we have been using Android’s default row layout for populating the ListView. It might sometimes become necessary to use one that satisfies our needs, thereby creating a custom row layout. As I had said in my earlier posts that there are a number of ways f implementing List views, we are going to use one here that allows use of custom layout for every single row of the list view.

Since this is just another way of implementing, I will just provide a few points of difference worth noting from earlier implementations and the complete source code

  • We will be extending the ListActivity class here
  • We will not be using the setContentView() method here. This tells Android that we need only a List view in our activity spanning across the entire size.
  • We will also not be declaring the ListView here for the same reason.
  • We will assign the adapter to the ListView with the use of this keyword. See below for implementation

Array_Adapter3

COMPLETE SOURCE CODE

row_item.xml

MainActivity.java

[java]
package com.nero.myfirstapp;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Main extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String values[]=new String[]{"Vergil", "Dante", "Sparda", "Nero", "Arkham", "Agni", "Rudra", "Beowulf", "Nevan"};
this.setListAdapter(new ArrayAdapter(this, R.layout.row_item, R.id.textView1,values));
}
}
[/java]

Leave a Comment

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