Monday, 30 May 2016

Custom List view in Android

Activity_Custom.XML

<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="${relativePackage}.${activityClass}" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="16dp" >
    </ListView>

</RelativeLayout>

Custom Activity.Java
package com.example.customlist;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

public class CustomActivity extends Activity {

String[] values={"AAAAA","BBBBB","cccc"};
Context context;


int[] images={R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher};

ListView lv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom);

context=this;

lv=(ListView) findViewById(R.id.listView1);

lv.setAdapter(new CustomAdapter(getApplicationContext(),values,images));
}
}

Custom.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:orientation="horizontal" >
    <ImageView
        android:id="@+id/imageView1"
        android:layout_gravity="center"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="25dp"
        android:text="TextView" />

</LinearLayout>


CustomAdapter.class
package com.example.customlist;

import java.util.zip.Inflater;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class CustomAdapter extends BaseAdapter {
LayoutInflater inflater=null;
String[] result;
Context context;
int [] imageId;

   TextView tv;
   ImageView img;
//}

public CustomAdapter(Context applicationContext, String[] values,
int[] images) {
// TODO Auto-generated constructor stub
result=values;
context=applicationContext;
imageId=images;
inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

   
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return result.length;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
// public class Holder
//    {
//        TextView tv;
//        ImageView img;
//    }

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//return null;
//Holder holder=new Holder();
View rowView;
rowView=inflater.inflate(R.layout.custom, null);
tv=(TextView) rowView.findViewById(R.id.textView1);
img=(ImageView) rowView.findViewById(R.id.imageView1);
       
        tv.setText(result[position]);
        img.setImageResource(imageId[position]); 
        
        
         rowView.setOnClickListener(new OnClickListener() {            
           @Override
          public void onClick(View v) {
               // TODO Auto-generated method stub
               Toast.makeText(context, "You Clicked "+result[position], Toast.LENGTH_LONG).show();
           }
      });   
        return rowView;
}


}

OutPut:


No comments:

Post a Comment

Ads Inside Post