Monday, 9 March 2015

Adapter in Android


Why or where adapter is required?
-If you want to show a large data on screen says 200 or more records on screen then adapter will be more memory efficient.

Adapter
From Android Blog: An Adapter object acts as a bridge between an AdapterView and the underlying data for that view.


Adapter is a interface which is used to create the view from the data and give it to Adapter View. Adapter View will display the data on screen as per your need. Adapter is responsible for managing the data(index/position).


Adapter View: An AdapterView is a view whose children are determined by an Adapter. E.g: ListView, ExpandableListView, Gallery, GridView,, Spinner


What is the parent class of AdapterView abstract class?




Type of Adapters/ Adapters Architecture:




Difference between listadapter and Array Adapter:
When you use ListAdapter as the type for your variable, you are really interested in the interface. You may not be interested in calling some specific method of ArrayAdapter<String> not available in ListAdapter. That is mainly because you will just assign the adapter as the list adapter for the view.


ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, songNames);


ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, songsArray);


Difference Between ArrayAdapter and Simple Adapter
  • ArrayAdapter enables us to add in new item dynamically, even after the list is being shown during that time. Note that, ArrayAdapter is having method add.
  • SimpleAdapter is used for case where there are no more new item can be added.


Difference Between Cursor Adapter and Simple Cursor Adapter
  • CursorAdapter is abstract and is to be extended. On the other hand, SimpleCursorAdapter is not abstract.


  • SimpleCursorAdapter is a readymade widget from google and you just specify how your layout look like and what ids of widgets you want to fill with your chunks of data. You dont have to override methods of CursorAdapter and implement them. But you can use only TextView and ImageView in your layout, because it doesn't support more widgets so far.


  • CursorAdapter have more options for customization.




Interview Questions:
  • What is adapter?
  • What is Adapter View?
  • is Adapter an abstract class?
  • is BaseAdapter a class?
  • What is the immediate parent of Array Adapter?
  • What is the immediate parent of Simple Adapter?
  • What is the immediate parent of Cursor Adapter?
  • Can we create instance of CursorAdapter? Why?
  • Can we create instance of SimpleCursorAdapter? Why?
  • Which type of data does ArrayAdapter class accept?
  • Difference between listadapter and Array Adapter?
  • Difference Between ArrayAdapter and Simple Adapter?
  • Difference Between Cursor Adapter and Simple Cursor Adapter?
  • is AdapterView an interface?

Sunday, 9 February 2014

How to create library files in java using some other libraries (dependencies)

If you have written some functions and want others to use it. You can create a library file(.jar) and publish it. 

Creating a Library File:

Step 1: Create a Java project.




Step 2: Keep your code ready.(For simplicity, i have taken just one add function but you create your desired functions)



Step 3: export the project(Don't forget to remove .project and .classpath option while exporting)
Go to project explorer> Right Click on your project> Export



Enhancing Library Features:

If you want to use someone else Library and want to publish it by enhancing its feature. In above step, i have created a library with the name kapillib.jar. Now you can enhance the feature of kapillib.jar library and can create your own library yourlib.jar.

Step 1:  Create your project and add jar/library files







Step 2: Enhance the features of library



Step 3: export the project as we did earlier (Don't forget to remove .project and .classpath option while exporting)

Step 4: Merge both the libraries and create one single jar/library file(If you dont want to merge both the libraries then you need to supply both libraries to user)

For merging libraries, use jar splice in two steps. 1. add jars 2. create fat jar

Add both the jars kapillib.jar and yourlib.jar. Click on 4th option of below screen shot to merge both the library and create final.jar.



How to use Library file.

Step 1:  Create your project and add jar/library file (final.jar) as we did in (enhancing library features's step:1)

Step 2:  Use the library and enjoy.




If you find errors in this tutorial, please feel free to contact me at kapil.sharma91812@gmail.com