Google Search

Custom Listview Using Custom Adapter

=> Friends here I am Post the Important Tutorial of Custom List view whenever any  want to implment Custom lstview then also useful my this Tutorial.

=>Custom listview implement in using custom adapter class and also that constucter.

main_activity xmlfile

custom listview

 


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >
    </ListView>

</RelativeLayout>

 

customlistviewitem xmlfile 

custom listview in android

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Cust"

    android:orientation="horizontal">

    <TextView
        android:id="@+id/tv1"
        android:layout_width="96dp"
        android:layout_height="wrap_content"
                android:background="#ff00ff"
                android:textColor="#565656"
        android:text="no"
        android:textSize="30dp" />

    <TextView
        android:id="@+id/tv2"
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:background="#00ff00"
        android:text="name"
        android:gravity="center"
        android:textColor="#ff0000"
        android:textSize="30dp" />

    <TextView
        android:id="@+id/tv3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:background="#ff0000"
        android:text="city"
        android:textColor="#00ff00"
        android:textSize="30dp"  />

</LinearLayout>

Main_Activity javafile 

 package com.example.listview;

import com.example.listview.R.drawable;

import android.os.Bundle;
import android.R.anim;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {
ListView ls;
Context context;
public static String [] s_id={"1","2","3","4","5"};
public static String [] s_name={"Gautam","Nilesh","Rajesh","Prakash","Rakesh"};
public static String [] s_city={"Rangpur","Rajkot","Botad","Chotila","Valsad"};
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ls=(ListView)findViewById(R.id.listView1);
        //String[] ss={"A","V"};
        context=this;
        try
        {
        ls.setAdapter(new Cust(MainActivity.this,s_id,s_name,s_city));
        }
        catch(Exception e)
        {
            Toast.makeText(getApplicationContext(),""+ e, 50).show();
        }
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

 

           Custom_Adapter javafile

 


package com.example.listview;
import java.util.zip.Inflater;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.text.Layout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class Cust extends BaseAdapter {
    View vi;
    int [] imgId;
    String[]  Result;
    String[]  Result2;
    String[]  Result3;
    static Context context;
    //private static LayoutInflater inflater=null;
   
    public Cust(MainActivity mainActivity, String[] s_no, String[] s_name,
            String[] s_city) {
        // TODO Auto-generated constructor stub
        Result=s_no;
        Result2=s_name;
        Result3=s_city;
        context=mainActivity;
        
    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return Result.length;
    }
    public String getItem(String arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }
    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }
   
   
    @Override
    public View getView( int arg0, View arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub
        TextView tv1,tv2,tv3;
        
        
        vi=arg1;
           if (vi == null) {
                LayoutInflater mInflater = (LayoutInflater) context
                        .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
                vi= mInflater.inflate(R.layout.activity_cust, null);
            }
        tv1=(TextView)vi.findViewById(R.id.tv1);
    tv2=(TextView)vi.findViewById(R.id.tv2);
    tv3=(TextView)vi.findViewById(R.id.tv3);
        
        
        tv1.setText(Result[arg0].toString());
        tv2.setText(Result2[arg0].toString());
        tv3.setText(Result3[arg0].toString());
        
        vi.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Toast.makeText(null, "Click", 50).show();
            }
        });
        
        return vi;
        
    }
    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }
   
   
   
}

custom listview output 

 

custom adpter class