Monday, 14 January 2013

Custom Adapter for Listview with Holder Pattern in android

Project Structure :



 

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/contact_list_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</RelativeLayout>


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" >

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

        <ImageButton
            android:id="@+id/edit_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginTop="10dp"
            android:layout_marginRight="15dp"
            android:background="@android:color/transparent"
            android:src="@drawable/edit" />

    </RelativeLayout>

Contact.java


package com.rajeshvijayakumar.model;

public class Contact {

    private int id;
    private String name;
    private String phoneNumber;
    private String emailId;

    public Contact(int id, String name, String phoneNumber, String emailId) {
        this.id = id;
        this.name = name;
        this.phoneNumber = phoneNumber;
        this.emailId = emailId;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getEmailId() {
        return emailId;
    }

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

ContactAdapter.java


package com.rajeshvijayakumar.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.sax.StartElementListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.rajeshvijayakumar.list.R;
import com.rajeshvijayakumar.list.TargetActivity;
import com.rajeshvijayakumar.model.Contact;

public class ContactAdapter extends ArrayAdapter<Contact> {

    private List<Contact> contactLst;
    private Context adapContext;

    public ContactAdapter(Context context, int textViewResourceId,
            List<Contact> contactLst) {
        super(context, textViewResourceId, contactLst);
        this.contactLst = contactLst;
        adapContext = context;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View view = convertView;
        ContactHolder contactHolder = null;

        if (convertView == null) {
            view = View.inflate(adapContext, R.layout.list_item, null);
           
            contactHolder = new ContactHolder();
            contactHolder.contactNameView = (TextView) view.findViewById(R.id.name_text_view);
            contactHolder.contactEditButton = (ImageButton) view.findViewById(R.id.edit_button);
           
            view.setTag(contactHolder);
        } else {
            contactHolder = (ContactHolder) view.getTag();
        }

        Contact contact = contactLst.get(position);
        final String name = contact.getName();
        contactHolder.contactNameView.setText(name);
        contactHolder.contactEditButton.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View v) {

                switch(v.getId()) {
                case R.id.edit_button :
                    Toast.makeText(adapContext, "You have Pressed Edit Button For " + name, Toast.LENGTH_LONG).show();
                    break;
                }
            }
        });
        return view;
    }

    static class ContactHolder {
        public TextView contactNameView;
        public ImageButton contactEditButton;
    }
}


MainActivity.java


package com.rajeshvijayakumar.list;

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

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

import com.rajeshvijayakumar.adapter.ContactAdapter;
import com.rajeshvijayakumar.model.Contact;

public class MainActivity extends Activity {

    private ListView mContactListView;
    private List<Contact> mContactList;
    private ContactAdapter mContactAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        mContactListView = (ListView) findViewById(R.id.contact_list_view);
        mContactList = getContacts();
        mContactAdapter = new ContactAdapter(MainActivity.this, R.layout.list_item, mContactList);
        mContactListView.setAdapter(mContactAdapter);

    }

    private List<Contact> getContacts() {

        List<Contact> contactData = new ArrayList<Contact>();
        contactData.add(new Contact(1, "Aakash", "1111111111", null));
        contactData.add(new Contact(2, "Akshay", "2222222222", null));
        contactData.add(new Contact(3, "Ashwin", "3333333333", null));
        contactData.add(new Contact(4, "Mahesh", "4444444444", null));
        contactData.add(new Contact(5, "Mrithula", "5555555555", null));
        contactData.add(new Contact(6, "Rajesh", "6666666666", null));
        contactData.add(new Contact(7, "Sonika", "7777777777", null));
        return contactData;
    }
}


In AndroidMainfest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rajeshvijayakumar.list"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="7"
        android:targetSdkVersion="16" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>




Source Code : Download this example Here


No comments:

Post a Comment