Tuesday, 24 June 2014

List View With Alternate Colors



Manifest File

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.listview"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
       
        <activity
            android:name="com.deepu.activity.ContactActivity"
            android:label="@string/app_name"
            android:screenOrientation="portrait"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>
 ==========================================================================
 Contact ListView(layout)
contactlistview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight=".08"
        android:orientation="horizontal"
        android:background="@drawable/controlbar2">

        <TextView
         
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Name"
            android:layout_marginLeft="15dp"
            android:textSize="12dp"
            android:textColor="#ffffff"
            android:gravity="center_vertical" >
        </TextView>

     
        <TextView
         
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Phone"
             android:textSize="12dp"

            android:textColor="#ffffff"
            android:gravity="center_vertical" >
        </TextView>


        <TextView
         
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"
        
            android:text="Email"
            android:singleLine="true"
             android:textSize="10dp"
            android:textColor="#ffffff"
            android:gravity="center_vertical" >
        </TextView>

      

        <TextView
         
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"
           android:text="Address1"
           android:textColor="#ffffff"
            android:textSize="12dp"
            android:gravity="center_vertical" >
        </TextView>

     

        <TextView
         
            android:layout_width="0dip"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="Address2"
         
             android:textSize="12dp"
            android:textColor="#ffffff"
            android:gravity="center_vertical" >
        </TextView>
    </LinearLayout>
  
     <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="0.84"
     
        android:orientation="horizontal" >
    <ListView
        android:background="#ffffff"
        android:id="@+id/listView1"
        android:dividerHeight="2sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>
    </LinearLayout>

</LinearLayout>
 ========================================================================
listview_iteams.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="horizontal"
     >
  
  
     <TextView
            android:id="@+id/nameTV"
            android:layout_width="0dip"
            android:layout_height="50dp"
            android:layout_weight="1"
            android:text="Name"
            android:layout_marginLeft="15dp"
            android:textSize="12dp"
            android:textColor="#000000"
            android:gravity="center_vertical" >
        </TextView>

     
        <TextView
            android:id="@+id/emailTV"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Email"
            android:textSize="12dp"
            android:textColor="#000000"
            android:gravity="center_vertical" >
        </TextView>


        <TextView
            android:id="@+id/phoneTV"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Phone"
            android:singleLine="true"
            android:textSize="10dp"
            android:textColor="#000000"
            android:gravity="center_vertical" >
        </TextView>

      

        <TextView
            android:id="@+id/address1TV"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Address1"
            android:textColor="#000000"
            android:textSize="12dp"
            android:gravity="center_vertical" >
        </TextView>

     

        <TextView
            android:id="@+id/address2TV"
            android:layout_width="0dip"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="address2"
            android:textSize="12dp"
            android:textColor="#000000"
            android:gravity="center_vertical" >
        </TextView>

</LinearLayout>
=========================================================================


 ContactPojo(pojo class)

package com.deepu.activity;

public class ContactPojo {
    String name;
    String email;
    String phone;
    String address1;
    String address2;
   
    //setters and getters
   
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
   
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getAddress1() {
        return address1;
    }
    public void setAddress1(String address1) {
        this.address1 = address1;
    }
    public String getAddress2() {
        return address2;
    }
    public void setAddress2(String address2) {
        this.address2 = address2;
    }
   
   

}


===========================================================================


Contact Activity

package com.deepu.activity;
import java.util.ArrayList;
import java.util.List;

import com.deepu.listview.R;

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

public class ContactActivity extends Activity {
    ListView listOfAccounts;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.contactlistview);
        listOfAccounts=(ListView) findViewById(R.id.listView1);
        List<ContactPojo> list=new ArrayList<ContactPojo>();
      
      
        for(int i=1;i<=10;i++){
            ContactPojo object=new ContactPojo();
            object.setName("name"+" "+i);
            object.setEmail("email"+" "+i);
            object.setPhone("phone"+" "+i);
            object.setAddress1("ADD"+" "+i);
            object.setAddress2("add"+" "+i) ;
            list.add(object);
        }
      
      
        System.out.println("size of list"+list.size());
        ListViewAdapter adapter =new ListViewAdapter(getApplicationContext(), list);
        listOfAccounts.setAdapter(adapter);
      
      
    }
}
==========================================================================



 ListViewAdapter:

package com.deepu.activity;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.deepu.listview.R;

public class ListViewAdapter extends BaseAdapter{
   
    private Context context;
    private int[] colors = new int[] { 0x30FF0000, 0x300000FF };
    private List<ContactPojo> list;
    public ListViewAdapter(Context c,List<ContactPojo> contactPojo){
        System.out.println("In constructor");
        context=c;
        list=contactPojo;
       
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return list.size();
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return list.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    @SuppressWarnings("static-access")
    @Override
    public View getView(int position, View contentView, ViewGroup parent) {
       
        ContactPojo pojo = list.get(position);
        System.out.println("In getView");
        View v=contentView;
       
   
       
        ViewHolder holder=new ViewHolder();
        if(contentView ==null){
           
       
            LayoutInflater inflater=(LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
           
            System.out.println("after layout inflater");
            v=inflater.inflate(R.layout.listview_iteams, null);
           
             int colorPos = position % colors.length;
              v.setBackgroundColor(colors[colorPos]);
           
            System.out.println("after view inflation");
            TextView ptp=(TextView) v.findViewById(R.id.nameTV);
            ptp.setText(pojo.getName());
           
            TextView tos=(TextView) v.findViewById(R.id.emailTV);
            tos.setText(pojo.getEmail());
           
            TextView account=(TextView) v.findViewById(R.id.phoneTV);
            account.setText(pojo.getPhone());
           
            TextView fname=(TextView) v.findViewById(R.id.address1TV);
            fname.setText(pojo.getAddress1());
           
            TextView area=(TextView) v.findViewById(R.id.address2TV);
            area.setText(pojo.getAddress2());
           
       
            holder.nameTextView=ptp;
            holder.emailTextView=tos;
            holder.phoneTextView=account;
            holder.address1TextView=fname;
            holder.address2TextView=area;
            v.setTag(holder);
       
           
           
        }
        else{
       
            holder=(ViewHolder) v.getTag();
        }
        return v;
    }
   
    public class ViewHolder{
       
        TextView nameTextView,emailTextView,phoneTextView,address1TextView,address2TextView;
       
       
       
    }

}
==========================================================================



No comments:

Post a Comment