1

What are the mandatory functions we have to implement while writing a custom adapter by extending BaseAdapter class?

  • i. only getView
  • ii. getItem
  • iii. getItemId
  • iv. getCount
  • i
  • i & ii
  • i, ii, & iii
  • i, ii, iii, & iv

Answer: D i, ii, iii, & iv
Description: getView, getItem, getItemId, and getCount are the mandatory functions that have to be implemented in Custom Adapter.

2

What is base adapter?what are the functions available in it?

  • Base Adapter is a common base class of common implementation for an Adapter that can be used in both ListView and Spinner.

    Functions available in Base Adapter:

    1. getCount()
    2. getItem() 3. getItemId() 4. getConvertView()
  • Base Adapter is a common base class for any adapter that can be used in both ListView and Spinner. Base adapter is an abstract class which implements both ListAdapter and SpinnerAdapter interfaces.
    While creating custom adapters, mostly programmers will extend this class.
    Functions available in Base Adapter:
    1. getCount()
    2. getItem()
    3. getList()
    4. getView()
  • Base Adapter is a common base class of common implementation for an Adapter that can be used in both ListView and Spinner.
    Functions available in Base Adapter:
    1. onCreateView()
    2. getItem()
    3. getItemId()
    4. getConvertView()
  • None are correct

Answer: B Base Adapter is a common base class for any adapter that can be used in both ListView and Spinner. Base adapter is an abstract class which implements both ListAdapter and SpinnerAdapter interfaces.
While creating custom adapters, mostly programmers will extend this class.
Functions available in Base Adapter:
1. getCount()
2. getItem()
3. getList()
4. getView()
Description:

3

How to generate alternate colours for child views in list view?

  • It is not possible. We can give only one color to the views because we have only one xml file for the views in the listview.
  • Take two layout xml files with a view. First xml file will contain a view with red color, and second xml file will contain a view with gree color. In custom adapter of the listview, go to getView() method and check the current position, if it is divisible by 2 apply first xml else apply second xml.
  • take one xml file with a view. Go to custom adapter of the listview and go to getView() method. In that method check the current position, if it is divisible by 2 set one background color for the view else set a different background color for the same view.
  • both option 2 or option 3 can be used. Both will give the same effect.

Answer: D both option 2 or option 3 can b used. Both will give the same effect.
Description:

4
In custom adapter what is converView parameter in getView() function?

public View getView(int position, View convertView, ViewGroup parent)
{
....
}
  • It contains information about which view user has clicked in the adapterview.
  • it will be null if android is returning previous view which was created and went out of scope for reuse.
  • it will be not null if android is returning previous view which was created and went out of scope, for reuse.
  • it is for backward compatibility, it will be always null.

Answer: C it will be not null if android is returning previous view which was created and went out of scope, for reuse.
Description: getView function has to create view for each row for adapterview and return it. But at a given point of time only few rows or elements of an adapterview will be visible to user. So it is unnecessary to create all the views if it is not visible to user. Rather we can reuse the old views which went out of scope. In order to reuse the previously created rows/ view which are currently not visible to user, they will be passed as second parameter to this function to reuse those views.

Programmer can check if(convertView != null) , then he or she can reuse that view.

5
In custom adapter getView function, while inflating a view what will happen if we pass true as last parameter to inflate function?
Note: below function is a customAdapters function.

public View getView(int position, View convertView, ViewGroup parent) {
RelativeLayout rl = (RelativeLayout) getLayoutInflater().inflate(R.layout.row, parent, false);
}
  • It will inflate the view two times in the parent adapter view.
  • Nothing will happen, still it will be inflated only once in the parent adapter view.
  • it will not be inflated, if we pass true as last parameter.
  • none of the above.

Answer: A It will inflate the view two times in the parent adapter view.
Description: Don't pass true, other wise same view will be inflated to list view or destination two times.
Just pass false.

6
Android listview with custom row with a radio button, focus problem:
If row.xml is having a radio button, and a text view, then will onItemClickListener work for this adapter view (assume listview) on which we are setting this custom adapter?
Note: below function is a customAdapters function.

public View getView(int position, View convertView, ViewGroup parent) {
RelativeLayout rl = (RelativeLayout) getLayoutInflater().inflate(R.layout.row, parent, false);
....
}
  • If user clicks on any item on each row, it will call onItemClickListener of the adapterView.
  • It will not call onItemClickListener if there are any focus able items like radio button available in each row. to solve this make onFocusable="false" in the xml file of Row.
  • it will never call onItemClickListener, whether we make focus able of radio true or false.
  • it depends, if user clicks on radio button, then it will not call onItemClickListener of adapterview, if user clicks on textview then it will call that function.

Answer: B It will not call onItemClickListener if there are any focus able items like radio button available in each row. to solve this make onFocusable="false" in the xml file of Row.
Description: It will not call onItemClickListener if there are any focus able items like radio button available in each row. to solve this make onFocusable="false" in the xml file of Row.