MainActivity.java
package com.example.test;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
ListView listview;
UsersAdapter adapter;
ArrayList<User> dataSource = new ArrayList<User>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
createDataSource();
listview=(ListView) findViewById(R.id.listview);
adapter = new UsersAdapter(this, dataSource);
listview.setAdapter(adapter);
listview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
User user=adapter.getItem(position);
Toast.makeText(getBaseContext(), user.name, Toast.LENGTH_LONG).show();
}
});
}
private void createDataSource() {
User user=new User();
user.setName("Raju");
user.setAge("26");
dataSource.add(user);
user=new User();
user.setName("Rozy");
user.setAge("22");
dataSource.add(user);
user=new User();
user.setName("Meera");
user.setAge("32");
dataSource.add(user);
user=new User();
user.setName("Bibhu");
user.setAge("34");
dataSource.add(user);
user=new User();
user.setName("Prakash");
user.setAge("24");
dataSource.add(user);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar,if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class UsersAdapter extends ArrayAdapter<User> {
// View lookup cache
private class ViewHolder {
TextView tvName;
TextView tvAge;
}
public UsersAdapter(Context context, ArrayList<User> users) {
super(context, R.layout.item_user, users);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
User user = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
ViewHolder viewHolder; // view lookup cache stored in tag
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.item_user, parent, false);
viewHolder.tvName = (TextView) convertView.findViewById(R.id.tvName);
viewHolder.tvAge = (TextView) convertView.findViewById(R.id.tvAge);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
// Populate the data into the template view using the data object
viewHolder.tvName.setText(user.name);
viewHolder.tvAge.setText(user.age);
// Return the completed view to render on screen
return convertView;
}
}
}
main.xml
<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"
android:orientation="vertical" >
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E09090" />
</LinearLayout>
item_user.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/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Name"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tvAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="23"
android:textSize="18sp" />
</LinearLayout>
User.java
package com.example.test;
public class User {
String name,age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}