Friday, May 29, 2015

Adapters in Android

Adapters in android is like our mobile charging adapter to connect your device and get charge.Imagine what would have been the smartphone without adapters! definitely your smartphones battery gets blast! :)
Adapters in android act as bridge between the view (eg. ListView) and flow of input data for that Views.

 Just think for few seconds WHAT IF Adapter is not there?
Without Adapters, to implement the listview functionality, you will need to:
  1. create a TextView within a ScrollView group.
  2. Then you will have to implement pagination concept for the contents of the TextView.
  3. You will also have to write additional code to identify the click event on a particular row in the TextView. 
Quite cumbersome job! Isn’t it?

Below is a conceptual diagram which shows the high level working of the Android Adapter:

 


Let us now understand the internal working of an Android Adapter and how it acts as a data pump to the adapter view.
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. Now, it will be a performance nightmare if getView() returns a new View every time it is called. Creating a new view is 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 finally display it on the screen.It also puts a lot of pressure on the garbage collector. That is because when the user is scrolling through the list, if a new view is created; the old view (since it is not recycled) is not referenced and becomes a candidate to be picked up by the garbage collector. So what Android does is that it recycles the views and reuses the view that goes out of focus.

Below is a visual representation of this recycle process:

 


In the above figure, let us assume we are displaying the months in a year in a ListView. To begin with, the months January till May are shown in the screen. When you scroll the view, the month January goes out of the display area of the mobile screen. As soon as the January view goes out of the screen, the Adapter View (ListView in this case) sends the view to something called a recycler.So when you scroll up, the getView () method is called to get the next view (which is June). This method getView() has a parameter called convertview which points to the unused view in the recycler. Through the convertview, the Adapter tries to get hold of the unused view and reuse it to display the new view (which is June in this case).

So, when you are creating a custom Adapter View, you should code your
getView () as mentioned below:

@Override
 public View getView(int position, View convertView, ViewGroup parent) {

  ViewHolder holder = null;

  LayoutInflater mInflater = (LayoutInflater) context
    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
  if (convertView == null) {
   convertView = mInflater.inflate(R.layout.mangmt_list_item, null);
   holder = new ViewHolder();

   holder.member_name = (TextView) convertView
     .findViewById(R.id.member_name);
   holder.profile_pic = (ImageView) convertView
     .findViewById(R.id.profile_pic);
   holder.status = (TextView) convertView.findViewById(R.id.status);
   holder.contactType = (ImageView) convertView
     .findViewById(R.id.contact_type);

   ManagementRowItem row_pos = managementRowItems.get(position);

   holder.profile_pic.setImageResource(row_pos.getProfile_pic_id());
   holder.member_name.setText(row_pos.getMember_name());
   holder.status.setText(row_pos.getStatus());
   holder.contactType.setImageResource(row_pos.getContactType_id());

   convertView.setTag(holder);
  } else {
   holder = (ViewHolder) convertView.getTag();
  }

  return convertView;
 }
 


above content refer from  Edureka.co

stay tune happy coding :)


No comments:

Post a Comment