Tuesday, 12 August 2014

customize listview android

customize listview android       

                   In this tutorial i have explain about create simple customize listview android.By using  a custom listadapter.In normal listview you cannot add more data's like images.So we have use adapter to set the another view into the listview.By using adapter  you can  show multiple items inside the listview.Here i have writen simple coding for create customize listview android .

Steps to create customize listview android,
1)Create listview in the MainActivity.
MainActivity.Java
public class MainActivity extends Activity
{
ListView listview;
@Override
protected void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview=(ListView)findViewById(R.id.listview);
//set customize adapter with the listview..
listview.setAdapter(new ListviewAdapter(this));
}
}
main_activity.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=".MainActivity" >
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
2)Create adapter xml to set with the listview..
adapter.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="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/relativeLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" >
<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_centerVertical="true"
android:layout_toLeftOf="@+id/imageView2" android:layout_toRightOf="@+id/imageView1"
android:text="Large Text" android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView android:id="@+id/imageView2"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="@drawable/android_icon" />
</RelativeLayout> </RelativeLayout>
3)Finally,Create ListviewAdapter to set the view to the customize listview android .
public class ListviewAdapter extends BaseAdapter
{
LayoutInflater inflater;
Context context;
public ListviewAdapter(Context context)
{
this.context=context;
//get the layout inflater
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount()
{
// TODO Auto-generated method stub
return Dec.data.length;
}
@Override
public Object getItem(int arg0)
{
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int arg0)
{
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View view, ViewGroup parent)
{ // TODO Auto-generated method stub
ViewHolder holder;
/* A ViewHolder object stores each of the component views inside the tag field of the Layout, so you can immediately access them without the need to look them up repeatedly */
if (view == null)
{
holder = new ViewHolder();
//Instantiates a layout XML file into its corresponding View objects.
view = inflater.inflate(R.layout.adapter, null);
holder.data = (TextView) view.findViewById(R.id.textView1);
holder.image=(ImageView)view.findViewById(R.id.imageView1);
holder.icon=(ImageView)view.findViewById(R.id.imageView2);
// the setTag is used to store the data within this view
view.setTag(holder);
}
else
{
// the getTag returns the viewHolder object set as a tag to the view
holder = (ViewHolder)view.getTag();
}
holder.data.setText(Dec.data[position]); holder.image.setBackgroundResource(Dec.images[position]);holder.icon.setBackgroundResource(Dec.icons[position]);
return view;
}
public class ViewHolder
{ TextView data;
ImageView image,icon;
}
}  
 Screenshot :
customize listview android
customize listview android

No comments: