Thursday, 31 March 2011

Custmise ListView

To make an customise List View in Android  You have to make a new adepter class  extend Array Adapter or Base Adapter and  @Override  the getView and specify your required layout to list to be return by this method and use this adapter in our list view.





 







frist make required layout in xml like

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/continent_search" android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:padding="6dip" >
   
    <ImageView android:id="@+id/icon" android:layout_width="70dp"
        android:layout_height="70dp" android:layout_marginRight="6dip" android:scaleType="fitXY"
        />
   
    <LinearLayout android:padding="10dip"
        android:orientation="vertical" android:layout_width="0dip"
        android:layout_weight="1" android:layout_height="fill_parent">
        <TextView android:id="@+id/toptext" android:layout_width="fill_parent"
            android:layout_height="0dip" android:layout_weight="1"
            android:gravity="center_vertical" android:textColor="@drawable/white"
              android:text=""/>
        <TextView android:layout_width="fill_parent" android:ellipsize="end"
            android:layout_height="0dip" android:layout_weight="1"                    android:id="@+id/bottomtext"
android:text="" android:singleLine="true"
            android:textColor="@drawable/white"  />
    </LinearLayout>

</LinearLayout>    


then make an adepter class

class Name_Adapter extends ArrayAdapter<UR_Adapter_Type> {
       

        public Name_Adapter(Context context, int textViewResourceId,
                UR_Adapter_Type objects) {
            super(context, textViewResourceId, objects);

           // Do your stuff if what you needed.....

        }

        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
        
  // Make Your view Here By using  Layout inflate  like ....

LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View v = convertView;
            v = vi.inflate(R.layout.UR_XML_NAME, null);
//Getting View  Element

ImageView iv = (ImageView) v.findViewById(R.id.icon);
            TextView t1 = (TextView) v.findViewById(R.id.toptext);
            TextView t2 = (TextView) v.findViewById(R.id.bottomtext);
//  set  your data to view  like image src text values etc .
                    .
                    .
                    .
                    .
// return your view 
   return v;
          
        }
    }
}


Now in Activity onCreate method get list view refrence and make adapter object and  and set to List View  then you get requried output

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cakemenu);
        ListView li = (ListView) findViewById(R.id.listview);
  // make adapter object

    Name_Adapter      m = new Name_Adapter(this, R.layout.<layout_for_list>,       UR_Adapter_Type_object);
       li.setAdapter(m);



}











No comments:

Post a Comment