0% found this document useful (0 votes)
18 views

Binding Data With The AdapterView Class - New

fhd

Uploaded by

bhavanimohancs
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Binding Data With The AdapterView Class - New

fhd

Uploaded by

bhavanimohancs
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 41

BINDING DATA WITH

THE ADAPTERVIEW
CLASS
Binding Data with the AdapterView Class
• In Android Programming, you can display the stored data using
the AdapterView subclass of the ViewGroup class.
• With the help of the Adapter interface, the AdapterView class
can bind the data to a particular view.
• The data to be displayed using the AdapterView subclass can be
stored in an external resource, such as strings or drawables.
• Some examples of the AdapterView subclasses are ListView,
Spinner, and Gellery, which allow you to bind the stored data
and display the data in a specific manner.
Binding Data with the AdapterView Class
• Firstly, the AdapterView object needs to fill the data in the layout
and then this object is used to retrieve the item selected by the
user.
• Following classes are used to bind and display the stored data:
• ListView
• Spinner
• Gallery
Using the ListView Class
• The ListView class allows you to create the list of items that can be
scrolled by the users.
• You can add the list items to the ListView by using the ListAdapter
interface.
• Firstly, your Activity class should extend the ListActivity class
instead of the Activity class.
• Further, the setListAdapter (ListAdapter) method is being invoked to
add a ListView class to occupy the entire screen of the Activity class.
• The ArrayAdapter class used to manage the array of list of items is
passed as an argument to the setListAdapter() method.
• The array of list of items would be displayed into the ListView class.
Using the ListView Class
Using the ListView Class
Using the ListView Class
Using the Spinner

• In android programming spinner is a widget that displays


the list of items as a drop-down list.
• It can be used to display the multiple options to the user in
which only one item can be selected by the user.
• Android spinner is associated with AdapterView. So we need to
use one of the adapter classes with spinner.
• In the following code snippet, we first create the instance of the
Spinner class and then that of the ArrayAdapter class.
Using the Spinner

//Craeting the instance of the Spinner class


Spinner spinner1=(Spinner) findviewById(R.id.spinner);
// Create the instance of the ArrayAdapter class to bind the
data
ArrayAdapter
array_adapter=ArrayAdapter.createFromResource(this,
R.array.sevenwonders,
android.R.layout.Simple_spinner_item);
Using the Spinner

• In the preceding code snippet, the spinner1 is the instance


of the Spinner class that has been initialized by retrieving
the spinner View being defined in the XML layout.
• The instance of the ArrayAdapter class has been created
and initialized by using the createFromResource() method.
• Let us now set the array_adapter for the Spinner object and
also set how the drop-down list would appear.
Using the Spinner
Using the Spinner
Using the Spinner
Using the Gallery View

• The Gallery view displays the images horizontally as a


scrolling list. To display the images in this view, you first
need to ensure that the images have been saved in the
res/drawable folder of the application.
Designing the AutoCompleteTextView
• In Android, AutoCompleteTextView is a class that provides an
editable text view, which also displays the list of suggestions
that are prompted when the user is typing the text in the view.
• You can create the AutoCompleteTextView view by using the
<AutoCompleteTextView> element in the Android XML file.
• Further, in the Activity class, you need to create an array of the
values that you want to be displayed as suggestions to the user.
• Let us create the AutoCompleteTextView Android application in
which we would create the AutoCompleteTextView view, which
would provide suggestions for the name of the books as the
user types the first three alphabets of the book name.
Designing the AutoTextCompleteView

• Perform the following steps to create and execute the


AutoTextComplete application:
• Create an Android application, named AutoTextComplete, in
Android Studio.
• Modify the activity_main.xml file located in the res/layout
folder by adding the code.
Designing the AutoTextCompleteView

• Perform the following steps to create and execute the


AutoTextComplete application:
• Create an Android application, named AutoTextComplete, in
Android Studio.
• Modify the activity_main.xml file located in the res/layout
folder by adding the code.
Designing the AutoTextCompleteView
Designing the AutoTextCompleteView
Designing the AutoTextCompleteView
Designing the AutoTextCompleteView
Designing the AutoTextCompleteView
• An array of String type is defined with a list of various books details.
• The ArrayAdapter class is initializaed and the created String array is
attached to it.
• The autoCompleteTextView object is created and initialized the
AutoCompleteTextView class.
• The findViewById() method is used to search a book on the basis of the
provided ID.
• The setThreshold() method is used to provide the number of characters
must be typed by a user, so that the application will provide the
suggestion of words.
• The setAdapter() method is used to bind the object of the ArrayAdapter
class with the autoCompleteTextView object.
Designing the Views Programmatically
• Apart from creating the UI (Views) by using the Android XML file,
you can also create the UI components dynamically, which are
generated during runtime.
• The need for dynamic creation of UI occurs when you want to
design the UI based on the user selection.
• For example, if you are creating the railways ticket reservation
application, the availability of seats would be displayed only if the
user has made the required selection.
• In other words, if the user has selected the source and destination
platforms, date of journey, and the class (i.e., sleeper, 1 st AC class,
2nd AC class) then only the availability of the seats can be
displayed.
Designing the Views Programmatically
Designing the Views Programmatically
Designing the Views Programmatically
Designing the Views Programmatically
Designing the Views Programmatically
• The LinearLayout view for the Button and DatePicker controls.
• Further, the instance of the Button and DatePicker classes have
been created as well as initialized.
• Finally, the text and layout for the button instance have been
set.
• Moreover, the addView() method has been invoked to add the
button instance to the layout.
• You can also find that the instance of the DatePicker class has
also been initialized and added to the layout on the onClick
event of the button.
Handling UI Events
• Generally, a user can have the interaction in an Android
application at either the activity level or the view level.
• In other words, the events can be raised either from the view
that has been created in the activity class or by any key of the
device.
• The key events are not handled by any of the view within the
activity.
Handling User Interaction with Activities
• The user interaction with the activities can be handled by
overriding some of the activity methods, such as onKeyDown()
and onKeyUp().
• The onKeyDown() event is triggered when the user presses a
key
• The onKeyUp() event is triggered when the user releases a key
• The onKeyPress() event is triggered when the user presses &
releases (onKeyDown() followed by onKeyUp() )
Handling User Interaction with Activities
Handling User Interaction with Activities
Handling User Interaction with Activities
Handling User Interaction with Activities
Handling User Interaction with Activities
Handling User Interaction with Activities
• The keycode variable has been created to maintain the records
for the key that has been pressed.
• According to the key that has been pressed, the appropriate
switch case would be invoked and the message would be
displayed.
Handling User Interaction with the Views
• User can fire events through various views , such as button or
RadioButton.
• For example, when a user touches the Button, the onClick()
event is fired.
Handling User Interaction with the Views
Handling User Interaction with the Views
Handling User Interaction with the Views
Handling User Interaction with the Views

You might also like