Monday, 30 June 2014

List View With Search Option And Pagination


In general search option in list view is implemented by setting an addTextChangeListener
on EditText control where we should apply an filter on the list view by getting an i/p from
user which he enters on Edit Text and that filter will populate the data based on that filter.

But In some situations we need to populate the data based on custom search for example
If the list view is showing list of "Contact Objects" and user need to search based on both
the combinations of "name and phone" then we need to write our own search logic based
on the requirement.

And if the resultant search returns 100's of record you need to implement pagination for it.

You can Download the Source Code below.

DOWNLOAD NOW

In List View I have populated Contact Details

Like
Name1  email1  Phone1 Add1 add2
........................................................
........................................................
........................................................
Name20 email20 phone20 Add20 add20

while u test this application for search option u need to give i/p as follows
Name: name 1  etc..
Phone: phone 1 etc..

No validation are done for empty field and email address.

I will keep on updating when ever I get time.



Wednesday, 25 June 2014

Pagination with List view using Base Adapter in android

In Android we have an List View where u can populate data,
some times you may have an requirement where u should show the data with particular no,of records per page(as u see i many of web applications),then u should manually write the code with no,of pages and no,of records per page.
      
   Final result looks like this:
                                                                                                              
image 3
image 1
image 2














  You can Download the code here                                                                                                                   DOWNLOAD NOW



Here in my code i have use Base Adapter where u can populate data in u r desired format, and data i am populating is just an list of objects where u can send as an argument to Base Adapter .

If  required u can get it from database or an list of json objects according  to your  the requirement.




Tuesday, 24 June 2014

List View with alternate Colors

Here You can download the list view with alternate colors

DOWNLOAD NOW

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;
       
       
       
    }

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