For this we need following three classes
Along with these three java classes we need two xml file one would be default xml file (activity_custom_adapter_example.xml) for main class and another one for making Listview layout (simple_list_items.xml)
In activity_custom_adapter_example.xml, take a ListView element from the Widgets and in simple_list_items.xml, take two textviews.
Here Goes the Complete Code for the same:
CustomAdapterExample.java
package com.AndroidDevelopmentGuru.customadapter_second;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ListView;
public class CustomAdapterExample extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_adapter_example);
//Construct the data source
ArrayList<User> arrayofUsers = new ArrayList<User>();
arrayofUsers.add(new User("Anita", "987456578"));
arrayofUsers.add(new User("Rahul", "897456579"));
arrayofUsers.add(new User("Harminder", "987456578"));
arrayofUsers.add(new User("Manisha", "987456578"));
arrayofUsers.add(new User("Varun", "45565798"));
arrayofUsers.add(new User("Manish", "987456578"));
// Create the adapter to convert the array to views
CustomAdapter adapter = new CustomAdapter(CustomAdapterExample.this, arrayofUsers);
//Attach the adapter to Listview
ListView list = (ListView)findViewById(R.id.list);
list.setAdapter(adapter);
}
}
CustomAdapter.java
package com.AndroidDevelopmentGuru.customadapter_second;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class CustomAdapter extends BaseAdapter {
View view;
ArrayList<User> arraylist;
public CustomAdapter(CustomAdapterExample customAdapterExample,
ArrayList<User> arrayofUsers) {
// TODO Auto-generated constructor stub
arraylist=arrayofUsers;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return arraylist.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return arraylist.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int positon, View view, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
view = inflater.inflate(R.layout.simple_list_items, parent,false);
final User users = arraylist.get(positon);
TextView name = (TextView)view.findViewById(R.id.textView1);
name.setText(users.getName());
TextView contact =(TextView)view.findViewById(R.id.textView2);
contact.setText(users.getContactno());
return view;
}
}
User.java
package com.AndroidDevelopmentGuru.customadapter_second;
public class User {
private String name;
private String Contactno;
public User(String name, String contactno) {
// TODO Auto-generated constructor stub
this.name=name;
this.Contactno=contactno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContactno() {
return Contactno;
}
public void setContactno(String contactno) {
Contactno = contactno;
}
}
activity_custom_adapter_example.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=".CustomAdapterExample" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
simple_list_items.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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
Download This Code Here
OutPut:
- Main Class where we would be implementing listview using custom Adapter(CustomAdapterExample.java)
- Second Class for designing our Custom Adapter(CustomAdapter.java)
- And the last one for getter setter method for data transfer(User.java)
Along with these three java classes we need two xml file one would be default xml file (activity_custom_adapter_example.xml) for main class and another one for making Listview layout (simple_list_items.xml)
In activity_custom_adapter_example.xml, take a ListView element from the Widgets and in simple_list_items.xml, take two textviews.
Here Goes the Complete Code for the same:
CustomAdapterExample.java
package com.AndroidDevelopmentGuru.customadapter_second;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.widget.ListView;
public class CustomAdapterExample extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_adapter_example);
//Construct the data source
ArrayList<User> arrayofUsers = new ArrayList<User>();
arrayofUsers.add(new User("Anita", "987456578"));
arrayofUsers.add(new User("Rahul", "897456579"));
arrayofUsers.add(new User("Harminder", "987456578"));
arrayofUsers.add(new User("Manisha", "987456578"));
arrayofUsers.add(new User("Varun", "45565798"));
arrayofUsers.add(new User("Manish", "987456578"));
// Create the adapter to convert the array to views
CustomAdapter adapter = new CustomAdapter(CustomAdapterExample.this, arrayofUsers);
//Attach the adapter to Listview
ListView list = (ListView)findViewById(R.id.list);
list.setAdapter(adapter);
}
}
CustomAdapter.java
package com.AndroidDevelopmentGuru.customadapter_second;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class CustomAdapter extends BaseAdapter {
View view;
ArrayList<User> arraylist;
public CustomAdapter(CustomAdapterExample customAdapterExample,
ArrayList<User> arrayofUsers) {
// TODO Auto-generated constructor stub
arraylist=arrayofUsers;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return arraylist.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return arraylist.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int positon, View view, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
view = inflater.inflate(R.layout.simple_list_items, parent,false);
final User users = arraylist.get(positon);
TextView name = (TextView)view.findViewById(R.id.textView1);
name.setText(users.getName());
TextView contact =(TextView)view.findViewById(R.id.textView2);
contact.setText(users.getContactno());
return view;
}
}
User.java
package com.AndroidDevelopmentGuru.customadapter_second;
public class User {
private String name;
private String Contactno;
public User(String name, String contactno) {
// TODO Auto-generated constructor stub
this.name=name;
this.Contactno=contactno;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContactno() {
return Contactno;
}
public void setContactno(String contactno) {
Contactno = contactno;
}
}
activity_custom_adapter_example.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=".CustomAdapterExample" >
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
simple_list_items.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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
Download This Code Here
OutPut:
Comments
Post a Comment