Thursday, 11 April 2013

Alternative to Custom Adapter : Instant Adapter Example in Android

Get Instant Adapter Library from the following Url : https://github.com/ragunathjawahar/instant-adapter

Import and Add Instant library Project to your Project as shown below :





Then use the following code :

In activity_main.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ListView
        android:id="@+id/person_list_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</RelativeLayout>


In list_item.xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="16dp"
        android:layout_marginTop="14dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/phone_no"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/name"
        android:layout_marginLeft="47dp"
        android:layout_toRightOf="@id/name"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@id/phone_no"
        android:layout_below="@id/name"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>


Create Person.java Model

package com.rajeshvijayakumar.model;

import com.mobsandgeeks.adapters.InstantText;
import com.rajeshvijayakumar.insta.R;

public class Person {

    private String name;
    private String phoneNo;
    private String emailId;

    public Person(String name, String phoneNo, String emailId) {
        this.name = name;
        this.phoneNo = phoneNo;
        this.emailId = emailId;
    }

    @InstantText(viewId = R.id.name)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @InstantText(viewId = R.id.phone_no, formatString = "Mob : %s")
    public String getPhoneNo() {
        return phoneNo;
    }

    public void setPhoneNo(String phoneNo) {
        this.phoneNo = phoneNo;
    }

    @InstantText(viewId = R.id.email, formatString = "Email : %s")
    public String getEmailId() {
        return emailId;
    }

    public void setEmailId(String emailId) {
        this.emailId = emailId;
    }
}


MainActivity.java

package com.rajeshvijayakumar.insta;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

import com.mobsandgeeks.adapters.InstantAdapter;
import com.rajeshvijayakumar.model.Person;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {

    private ListView mPersonListView;
    private InstantAdapter mInsAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mPersonListView = (ListView) findViewById(R.id.person_list_view);
        mInsAdapter = new InstantAdapter<Person>(this, R.layout.list_item, Person.class, getPersons());
        mPersonListView.setAdapter(mInsAdapter);
    }

    private List<Person> getPersons() {

        List<Person> persons = new ArrayList<Person>();
        persons.add(new Person("Rajesh", "9600272370", "rajesh@gmail.com"));
        persons.add(new Person("Kuberan", "3224231223", "kuberan@gmail.com"));
        persons.add(new Person("Ramanan", "9873272323", "ramanan@gmail.com"));
        persons.add(new Person("Kalidasan", "9723476322", "kali@gmail.com"));

        return persons;
    }
}


Output :


Source Code :  Download this example here

No comments:

Post a Comment