Sunday, 11 August 2013

Instant Adapter View Handler 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/name_list_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</RelativeLayout>


In list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/person_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/person_name"
            android:layout_width="wrap_content"
            android:layout_gravity="center_vertical"
            android:layout_height="wrap_content"
            android:text="TextView" />
      
    </LinearLayout>

</RelativeLayout>

Create Person.java Model

package com.rajeshvijayakumar.insta;

public class Person {

    private int photoResId;
    private String name;

    public Person(int photoResId, String name) {

        this.photoResId = photoResId;
        this.name = name;
    }

    public int getPhotoResId() {
        return photoResId;
    }

    public void setPhotoResId(int photoResId) {
        this.photoResId = photoResId;
    }

    public String getName() {
        return name;
    }

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

}

MainActivity.java

package com.rajeshvijayakumar.insta;
public class MainActivity extends Activity implements Evaluator<Person>,
        OnItemClickListener {

    private ListView mPersonListView;
    private InstantAdapter<Person> mAdapter;
    private List<Person> mPersons;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mPersonListView = (ListView) findViewById(R.id.name_list_view);
        mPersons = getPersons();
        mAdapter = new InstantAdapter<Person>(MainActivity.this,
                R.layout.list_item, Person.class, mPersons);
        mAdapter.setViewHandler(R.layout.list_item, MainActivity.this);
        mAdapter.setViewHandler(R.id.person_image, MainActivity.this);
        mAdapter.setViewHandler(R.id.person_name, MainActivity.this);
        mPersonListView.setAdapter(mAdapter);
        mPersonListView.setOnItemClickListener(this);
    }

    private List<Person> getPersons() {
        List<Person> personLst = new ArrayList<Person>();
        personLst.add(new Person(R.drawable.ic_fav, "Rajesh"));
        personLst.add(new Person(R.drawable.ic_rate, "Mahesh"));
        personLst.add(new Person(R.drawable.ic_person, "Akshay"));
        personLst.add(new Person(R.drawable.ic_group, "Aakash"));
        return personLst;
    }

    @Override
    public void handleView(ListAdapter adapter, View parent, View view,
            Person person, int position) {

        switch (view.getId()) {
        case R.id.person_image:
            ((ImageView) view).setImageResource(person.getPhotoResId());
            break;
        case R.id.person_name:
            ((TextView) view).setText(person.getName());
            break;
        }
    }

    @Override
    public void onItemClick(AdapterView<?> adpaView, View v, int position,
            long id) {
        Toast.makeText(this, mPersons.get(position).getName().toString(),
                Toast.LENGTH_SHORT).show();
    }
}

Output :


Source Code :  Coming soon to Github...........

No comments:

Post a Comment