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 MainActivity.java under src/<your packagename>.

MainActivity.java:


public class MainActivity extends Activity {
Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button access=(Button)findViewById(R.id.button1);
final ListView list=(ListView)findViewById(R.id.listView1);

context=this;

final ArrayList aList=new ArrayList();
aList.add("Mango");
aList.add("Orange");
aList.add("Banana");
aList.add("Watermelon");
//on click access
access.setOnClickListener(new View.OnClickListener() {

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

}
});*/
}
}

Create activity_main.xml under res/layout folder.

activity_main.xml:




    <Button />

     
     


Create CustomListView.java which extends ArrayAdapter class in your source folder.

CustomListView.java:


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
this.objects=objects;

this.context=context;

}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView==null)
{
holder=new ViewHolder();
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=inflater.inflate(R.layout.custom_listview, null);
holder.catTitle=(TextView)convertView.findViewById(R.id.textViewcat);
holder.arrow=(Button)convertView.findViewById(R.id.arrow);
convertView.setTag(holder);
}
else
{
holder=(ViewHolder) convertView.getTag();
}

holder.catTitle.setText(objects.get(position));
holder.catTitle. setOnClickListener(new View.OnClickListener() {

@Override
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.

custom_listview.xml:




   
    <Button />


AndroidManifest.xml:




    

    
        
            
                

                
            
        
    


Output: