Tuesday, December 3, 2013
Custom ListView using Base Adapter in Android Example
Manchu Bhargavi
7:08 AM
android
,
android custom listview example
,
base adapter
,
custom listview for base adapter
,
customlistview
,
listiview using android example
,
listview
,
listview using base adapter
1 comment
:
Hi,
This is an Example for Custom Listview using Base Adapter in Android
In this tutorial we are going to show you how to implement custom ListView in Android with Images and Text for each list item. In order to display a series of data you can use ListView.
This is an Example for Custom Listview using Base Adapter in Android
In this tutorial we are going to show you how to implement custom ListView in Android with Images and Text for each list item. In order to display a series of data you can use ListView.
1.The Main layout for our project is “activity_main” which has a ListView to display the array with images.
activity_main.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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<RelativeLayout
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:background="#000000" >
<Button
android:id="@+id/listbtn"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="false"
android:layout_centerHorizontal="false"
android:layout_centerVertical="true"
android:gravity="left"
/>
<TextView
android:id="@+id/headtxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/listbtn"
android:gravity="center"
android:text="EXPENSE"
android:textColor="#FFFFFF"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/RelativeLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/RelativeLayout1"
android:layout_below="@+id/RelativeLayout1" >
<TextView
android:id="@+id/txtview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/textView2"
android:gravity="left"
android:text="textview"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:text="TextView" />
</RelativeLayout>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="280dp"
android:layout_alignLeft="@+id/bottomLayout"
android:layout_alignParentRight="true"
android:layout_below="@+id/RelativeLayout2" >
</ListView>
<LinearLayout
android:id="@+id/bottomLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:orientation="horizontal"
android:weightSum="3" >
<TextView
android:id="@+id/edt_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="RS 456688"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:textStyle="bold"/>
<TextView
android:id="@+id/edt_txt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="RS 672677"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:paddingBottom="@dimen/activity_vertical_margin"
android:textStyle="bold"/>
<TextView
android:id="@+id/edt_txt4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:text="RS 1384"
android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:id="@+id/bottomLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/bottomLayout2"
android:layout_alignLeft="@+id/bottomLayout2"
android:background="#000000"
android:orientation="horizontal"
android:weightSum="3" >
<TextView
android:id="@+id/txt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:text="INCOME"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/txt4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:text="EXPENSE"
android:textColor="#FFFFFF" />
<TextView
android:id="@+id/txt5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="5dp"
android:paddingTop="5dp"
android:text="BALANCE"
android:textColor="#FFFFFF" />
</LinearLayout>
</RelativeLayout>
2. Next step is to create a layout for the list item that is to be displayed in ListView. Create the layout as list_adapter.xml which has a TextView to display to the Array and a ImageView to display set of images in each list item. Here I have created the layout as RelativeLayout with ImageView and TextView in a Row.
list_adapter.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="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="5dp"
android:layout_marginTop="3dp"
android:padding="5dp" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView2"
android:layout_toRightOf="@+id/imageView1"
android:paddingLeft="30sp"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:paddingLeft="30sp"
android:layout_toRightOf="@+id/imageView1"
android:text="TextView" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
</LinearLayout>
3.Create a RowItem class generating setter and getter method.Using this we can call file in BaseAdapter class.
RowItem.class
package com.example.listview;
public class RowItem {
private String text;
private String no;
private int Imageid;
public RowItem(String text, String no, int imageid) {
super();
this.text = text;
this.no = no;
Imageid = imageid;
}
public int getImageid() {
return Imageid;
}
public void setImageid(int imageid) {
Imageid = imageid;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getNo() {
return no;
}
public void setNo(String no) {
this.no = no;
}
}
4.Create the Activity class for customlistview extends BaseAdapter and using RowItem class integrate with adapter class.
MainActivtiy.java:
package com.example.listview;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
ListView lv;
List<RowItem> rowitems;
public static final String[] car=new String[]{"BENZ","MAURTHI","TATA","MAN","CAR"};
public static final String[] no=new String[]{"1233","13256","1272878","7217","28198"};
public static final Integer[] image=new Integer[]{R.drawable.baseball_ball,R.drawable.corporate_art,R.drawable.golf_ball,R.drawable.magic_ball,R.drawable.tennis_ball};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv=(ListView)findViewById(R.id.listView1);
lv.setVerticalScrollBarEnabled(false);
rowitems=new ArrayList<RowItem>();
for(int i=0;i<car.length;i++)
{
RowItem item=new RowItem(car[i],no[i],image[i]);
rowitems.add(item);
}
lv.setAdapter(new CustomBaseAdapter(this,rowitems));
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(getApplicationContext(),"item is"+arg2, 4).show();
}
});
}
public class CustomBaseAdapter extends BaseAdapter {
Context context;
List<RowItem> rowItems;
public CustomBaseAdapter(Context context, List<RowItem> items)
{
this.context = context;
this.rowItems = items;
}
/*private view holder class*/
private class ViewHolder
{
ImageView imageView,imageView2;
TextView txt1;
TextView txt2;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater)
context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_adapter, null);
holder = new ViewHolder();
holder.txt1 = (TextView) convertView.findViewById(R.id.textView1);
holder.txt2 = (TextView) convertView.findViewById(R.id.textView2);
holder.imageView = (ImageView) convertView.findViewById(R.id.imageView1);
holder.imageView2=(ImageView)convertView.findViewById(R.id.imageView2);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
RowItem rowItem = (RowItem) getItem(position);
holder.txt1.setText(rowItem.getText());
holder.txt2.setText(rowItem.getNo());
holder.imageView.setImageResource(rowItem.getImageid());
return convertView;
}
@Override
public int getCount() {
return rowItems.size();
}
@Override
public Object getItem(int position) {
return rowItems.get(position);
}
@Override
public long getItemId(int position) {
return rowItems.indexOf(getItem(position));
}
}
}
5.Finally run the project in the Emulator.
Subscribe to:
Post Comments
(
Atom
)
Thanks a lot.It really helped me
ReplyDelete