Wednesday, 8 May 2013

List View in android Example


Today i am posting sample code custom list view using inflate serves with custom layouts.

-->Create lyt_view.xml in res folder

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" android:background="@drawable/raw_selector">

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"       
        android:paddingRight="5dp" >

        <LinearLayout
            android:id="@+id/linearLayout3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="center_vertical"
            android:orientation="vertical"
            android:paddingLeft="5dp" >

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:paddingTop="2dp" >

                <TextView
                    android:id="@+id/lyt_txt_name"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.5"
                    android:ellipsize="end"
                    android:gravity="center_vertical"
                    android:singleLine="true"
                    android:text="Name"
                    android:textColor="@color/darkGrey1"
                    android:textSize="16dp"
                    android:textStyle="bold" />

                <TextView
                    android:id="@+id/lyt_txt_distance"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:ellipsize="end"
                    android:gravity="center_vertical"
                    android:singleLine="true"
                    android:text="1234.56"
                    android:textColor="@color/black"
                    android:textSize="12dp"
                    android:textStyle="bold" />
            </LinearLayout>

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_weight="1"
                android:paddingTop="5dp" >

                <TextView
                    android:id="@+id/lyt_txt_add"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="0.5"
                    android:ellipsize="end"
                    android:gravity="center_vertical"
                    android:singleLine="true"
                    android:text="Name"
                    android:textColor="@color/darkGrey2"
                    android:textSize="12dp" />

                <TextView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="2"
                    android:gravity="center_vertical"
                    android:singleLine="true"
                    android:text="mile away"
                    android:textColor="@color/darkGrey2"
                    android:textSize="12dp" />
            </LinearLayout>

        </LinearLayout>

        <LinearLayout
            android:id="@+id/linearLayout4"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_marginLeft="2dp"
            android:layout_marginRight="2dp"
            android:gravity="center"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/imgbtn_arrow"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/arrow" />
        </LinearLayout>
    </LinearLayout>

   
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="@color/darkGrey1" />

</LinearLayout>
      


//Implementation class of custom list view 
private LayoutInflater lyt_Inflater = null;
private ItemsAdapter adpt;
private ListView Listview;

ItemsAdapter  adpt=new ItemsAdapter(Act_Parking.this,arr_phm);       
                           Listview.setAdapter(adpt);
                           Listview.invalidate();


-->This is custom adapter for inflate row in listview.
private class ItemsAdapter extends BaseAdapter
       {

              private ArrayList<pharmacists> arrphm;

              public ItemsAdapter(Context context,ArrayList<pharmacists> items)
              {
                     this.arrphm = items;
              }

              @Override
              public int getCount()
              {                   
                     return arrphm.size();
              }

              @Override
              public Object getItem(int position)
              {                   
                     return null;
              }

              @Override
              public long getItemId(int position)
              {                   
                     return 0;
              }

              @Override
              public View getView(final int position, View convertView, ViewGroup parent)
              {
                     View view_lyt = convertView;
                     try
                     {     
                           if(arr_phm.size()>0)
                           {                                
                                  final pharmacists phmst =arr_phm.get(position);
                                  String Name=phmst.name;
                                  String Distance=phmst.distance;
                                   String ParkingType=phmst.parking_type_name;


                                  lyt_Inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);                        
                                  view_lyt = lyt_Inflater.inflate(R.layout.lyt_view,null);

                                  TextView txtnm=(TextView) view_lyt.findViewById(R.id.lyt_txt_name);
                                  txtnm.setText(Name);

                                  TextView txtdistance=(TextView) view_lyt.findViewById(R.id.lyt_txt_distance);
                                  txtdistance.setText(Distance);

                                  TextView txtadd=(TextView) view_lyt.findViewById(R.id.lyt_txt_add);
                                  txtadd.setText("Parking Type:"+" "+ParkingType);

                                  view_lyt.setBackgroundResource(R.drawable.raw_selector);
                                  view_lyt.setOnClickListener(new OnClickListener()
                                  {                         
                                         @Override
                                         public void onClick(View v)
                                         {            
                                                Constants.CODE_TAB=3;
                                                Intent i=newIntent(Act_Parking.this,Act_Details.class);
                                                i.putExtra("ID", position);
                                                startActivity(i);

                                         }
                                  });          
                           }

                     }
                     catch (Exception e)
                     {
                           Log.i("Exception==", e.toString());
                     }                         

                     return view_lyt;
              }
       }

Simple list view:
http://www.coderzheaven.com/2012/01/08/single-selection-listview-in-android/
http://www.androidhive.info/2011/10/android-listview-tutorial/ 

List With icon/Custom Adapter:
http://www.javasrilankansupport.com/2012/05/android-listview-example-with-image-and.html
http://www.coderzheaven.com/2011/03/25/android-image-listview/ 
http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/ 

Filter in List view:
http://www.coderzheaven.com/2011/12/14/flitering-a-listview-using-an-input-from-an-edittext-in-android/ 

List with Load more:
http://www.androidhive.info/2012/03/android-listview-with-load-more-button/ 

List with Section: 
http://www.coderzheaven.com/2012/02/24/listview-with-sections-in-android/ 

List with layout inflate:
http://android-codes-examples.blogspot.in/2011/03/customized-listview-items-selection.html 

Expandable List:
http://www.coderzheaven.com/expandable-listview-android-simpleexpandablelistadapter-simple-example/
http://myandroidsolutions.blogspot.in/2012/08/android-expandable-list-example.html

I will be happy if you will provide your feedback or follow this blog. Any suggestion and help will be appreciated.

Thank you :)