Android Interview Questions - SPLessons

Android ListView

Chapter 6

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Android ListView

Android ListView

Android ListView groups various items and display them in vertical scrollable list. The list items are automatically inserted in the listview using an Adapter that pulls content from either an array or database.

The following example illustrates how to display the components in ListView using custom Adapter.

Adapters in Android act as a bridge between the Adapter View (e.g. ListView) and the underlying data. Adapters follow the Pagination concepts.

Adapters call the getView() method, which returns a view for each item within the adapter view. The layout format and the corresponding data for an item within the adapter view is set in the getView() method.

If a getView () yields a new view every time it is called, it becomes very expensive in Android as you will need to loop through the view hierarchy (using the find ViewbyId () method) and then inflate the view to display it on the screen. It also places a great deal of pressure on the garbage collector, because, when the user is scrolling through the list, if a new view is created the old view is not referenced and becomes eligible for garbage collector. To handle this situation, Android recycles the views and reuses the view that goes out of focus.

In the above shown figure, you can see 5 items. When the user scrolls, the view Item1 becomes invisible and the adapter sends this Item1 to the recycler. When Item1 becomes invisible, the getView () method is called which returns Item6. The method getView () has a parameter called converter, which points to the unused view in the recycler. Through the convertview, the Adapter tries to get hold of the unused view and reuses it to display the new view.

Create under src/<your packagename>.

public class MainActivity extends Activity {
Context context;
public void onCreate(Bundle savedInstanceState) {

Button access=(Button)findViewById(;
final ListView list=(ListView)findViewById(;


final ArrayList aList=new ArrayList();
//on click access
access.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
//using custom adapter
CustomListView cl=new CustomListView(context, R.layout.custom_listview, aList);
//using default adapter
/*ArrayAdapter adapter=new ArrayAdapter(context, android.R.layout.simple_dropdown_item_1line,aList);
//on click listview(default adapter)
/*list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v,
int position, long id) {
“You clicked at Index”+position, Toast.LENGTH_SHORT).show();


Create activity_main.xml under res/layout folder.


    <Button />


Create which extends ArrayAdapter class in your source folder.

public class CustomListView extends ArrayAdapter{
ArrayList objects;
Context context;
ViewHolder holder;

public CustomListView(Context context, int resource, ArrayList objects) {
super(context, resource, objects);
// TODO Auto-generated constructor stub



public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
holder=new ViewHolder();
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=inflater.inflate(R.layout.custom_listview, null);
holder=(ViewHolder) convertView.getTag();

holder.catTitle. setOnClickListener(new View.OnClickListener() {

public void onClick(View arg0) {
Toast.makeText(context,"You clicked at Index"+position,Toast.LENGTH_LONG).show();

return convertView;

public class ViewHolder
TextView catTitle;

Button arrow;

Create custom_listview under res/layout folder.


    <Button />