Dynamic Customized List View in android

  • How to develop custom ListView in android ? 
  • Sample code to develop customize listview in android.
  • Download sample code of dynamic ListView in android.
  • listview in android.
  • listview in android with image and text.
  • listview in android sample code download free.
  • How to set custom adapter in listview.
  • Add row in listview with image and text.

CustomListViewDemoActivity.
java


package www.gtumca.co.cc.dynamiclistview;

import java.util.Vector;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class CustomListViewDemoActivity extends Activity
{
    ListView gtumcaDynamicListView;
    Vector<GtuMcaBean> gtuMcaBean= new Vector<GtuMcaBean>();
    MyCustomAdaptor adaptor;
   
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        gtumcaDynamicListView=(ListView) findViewById(R.id.gtumcaDynamicListView);

            /*** adding data ***/       
        gtuMcaBean.add(new GtuMcaBean("1 First", "1 Last", "1 Birthdate"));
        gtuMcaBean.add(new GtuMcaBean("2 First", "2 Last", "2 Birthdate"));
        gtuMcaBean.add(new GtuMcaBean("3 First", "3 Last", "3 Birthdate"));
        gtuMcaBean.add(new GtuMcaBean("4 First", "4 Last", "4 Birthdate"));
        gtuMcaBean.add(new GtuMcaBean("5 First", "5 Last", "5 Birthdate"));
        gtuMcaBean.add(new GtuMcaBean("6 First", "6 Last", "6 Birthdate"));
        gtuMcaBean.add(new GtuMcaBean("7 First", "7 Last", "7 Birthdate"));
        gtuMcaBean.add(new GtuMcaBean("8 First", "8 Last", "8 Birthdate"));
        gtuMcaBean.add(new GtuMcaBean("9 First", "9 Last", "9 Birthdate"));

        adaptor = new MyCustomAdaptor(CustomListViewDemoActivity.this, R.layout.data, gtuMcaBean);
        gtumcaDynamicListView.setAdapter(adaptor);
 }
}


MyCustomAdaptor.java

package www.gtumca.co.cc.dynamiclistview;
import java.util.Vector;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyCustomAdaptor extends ArrayAdapter<GtuMcaBean>
{
    Context context;
    int layoutResourceId;  
    static String datesort;
   
    GtuMcaBean currentMRB;
    Vector<GtuMcaBean> data;
    /** Called when the activity is first created. */
    // TODO Auto-generated constructor stub
    public MyCustomAdaptor(Context context, int layoutResourceId, Vector<GtuMcaBean> data)
    {
        super(context,layoutResourceId,data);
        this.layoutResourceId = layoutResourceId;
        this.context=context;
        this.data = data;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        View row = convertView;
        MyStringReaderHolder holder;
       
        if(row==null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent,false);
           
            holder= new MyStringReaderHolder();
           
            holder.gtumcaTvFirstName =(TextView)row.findViewById(R.id.gtumcaTvFirstName);
            holder.gtumcaTvLastName =(TextView)row.findViewById(R.id.gtumcaTvLastName);
            holder.gtumcaTvBirthDate  =(TextView)row.findViewById(R.id.gtumcaTvBirthDate);
            holder.gtumcaIvIcon=(ImageView) row.findViewById(R.id.gtumcaIvIcon);
           
            row.setTag(holder);
        }
        else
        {
            holder=(MyStringReaderHolder) row.getTag();
        }
       
        GtuMcaBean mrb =data.elementAt(position);
        System.out.println("Position="+position);
     
         holder.gtumcaTvFirstName.setText(mrb.gtumcaFirstName);
        holder.gtumcaTvLastName.setText(mrb.gtumcaLastName);
        holder.gtumcaTvBirthDate.setText(mrb.gtumcaBirthdate);
        holder.gtumcaIvIcon.setImageResource(R.drawable.ic_launcher);
        return row;
    }
    static class MyStringReaderHolder
    {
        TextView gtumcaTvFirstName,gtumcaTvLastName,gtumcaTvBirthDate;
        ImageView gtumcaIvIcon;
    }
}







GtuMcaBean.java

package www.gtumca.co.cc.dynamiclistview;

public class GtuMcaBean
{
    String gtumcaFirstName,gtumcaLastName,gtumcaBirthdate;  
    GtuMcaBean(String gtumcaFirstName,String gtumcaLastName,String gtumcaBirthdate)
    {
        this.gtumcaFirstName = gtumcaFirstName;
        this.gtumcaLastName = gtumcaLastName;
        this.gtumcaBirthdate = gtumcaBirthdate;
    }
}

main.xml
<?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="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="@string/hello" />

    <ListView
        android:id="@+id/gtumcaDynamicListView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

data.xml
<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/relativeLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <ImageView
            android:id="@+id/gtumcaIvIcon"
            android:layout_width="64dp"
            android:layout_height="64dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/gtumcaTvFirstName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_toRightOf="@+id/gtumcaIvIcon"
            android:text="Large Text"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <TextView
            android:id="@+id/gtumcaTvLastName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@+id/gtumcaTvFirstName"
            android:layout_below="@+id/gtumcaTvFirstName"
            android:text="Medium Text"
            android:textAppearance="?android:attr/textAppearanceMedium" />

        <TextView
            android:id="@+id/gtumcaTvBirthDate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/gtumcaTvLastName"
            android:layout_alignParentRight="true"
            android:layout_marginRight="14dp"
            android:text="Small Text"
            android:textAppearance="?android:attr/textAppearanceSmall" />

    </RelativeLayout>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="www.gtumca.co.cc.dynamiclistview"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".CustomListViewDemoActivity"
            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>

Output

9 comments:

Post a Comment