Professional Documents
Culture Documents
Listview: Penir Week 5 Teknik Informatika
Listview: Penir Week 5 Teknik Informatika
Listview: Penir Week 5 Teknik Informatika
Penir Week 5
Teknik Informatika
Highlights
• ListView
• Custom ListView
• WebView
ListView
• ListView is a view group that displays a list of scrollable items.
• The list items are automatically inserted to the list using an Adapter
that pulls content from a source such as an array or database query
and converts each item result into a view that's placed into the list.
Method Creating List View
• ListActivity
• Activity with ListView Widget
ListActivity
• The following example uses
ListActivity, which is an activity
that includes a ListView as its
only layout element by default.
• ListActivity is special kind of
activity that doesn't have any
layout attached to it
Tutorial #1
ListActivity
Step 1
• Create blank empty activity Change default extends
class to ListActivity
Prepare adapter
ArrayAdapter
• ArrayAdapter is an object than can converts an ArrayList of objects
into View items loaded into the ListView container.
• ArrayAdapter can accept several form of data sources:
• Array of String
• Array of objects
• ArrayList
• List
• etc
• ArrayAdapter usually used with listview and spinner widget
Step 3
• Type onListItemClick … , use arrow up/down to select from
autocomplete suggestion and then press enter
Step 3
Perfect!
Challenge!
Make the listview searchable via edittext search!
Hints:
• String “contains” method can search char
sequence
• addTextChangedListener will detect content
changed within edit text
Step 6: Add Touch Listener
val arrayAdapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, os)
lstOs.adapter = arrayAdapter
Access array of
OperatingSystem object and
display its OS name
Tutorial #3:
Custom ListView
Custom List View
• The ListView instance calls the
getView() method on the
adapter for each data element.
• In this method the adapter
creates the row layout and
maps the data to the views in
the layout.
Custom List View
• An adapter manages the data model and adapts it to the individual
entries in the widget.
• Every line in the widget displaying the data consists of a layout which
can be as complex as you want.
Step 1
• Create new layout. Right click on res > layout and then pick new >
layout resource file
• Name it “listview_layout”
Step 2 ConstraintLayout
layout_width = match_parent
layout_height = wrap_content
• Create Layout similar like this:
Image ID = imgLogo
Image width & height = 70dp
JudulOS ID = txtJudul
ApiLevel ID = txtApi
Steps 3
• Download set of OS images from Classroom
• Copy and paste all images to drawable
• Choose “drawable” and then press OK
• drawable folder used by
android API < 24
• drawable-v24 used by android
API >= 24
Steps 4
class OperatingSystem(OS:String, apilevel:Int, imageid:Int ){
var OS = OS
var apilevel = apilevel
var imageid = imageid add new imageid
filed. This field is used
to store resource
override fun toString(): String { image id
return "$OS ($apilevel)"
}
}
Steps 5
Adjust your array of objects:
var os = arrayOf(OperatingSystem("Gingerbread", 9, R.drawable.gingerbread),
OperatingSystem("Honeycomb", 11, R.drawable.honeycomb),
OperatingSystem("Ice Cream Sandwich", 14, R.drawable.icecream),
OperatingSystem("JellyBean", 16, R.drawable.jellybean),
OperatingSystem("KitKat", 19, R.drawable.kitkat),
OperatingSystem("Lollipop", 21, R.drawable.lollipop)
)
Steps 6
• Create new class, name it “CustomAdapter”
• Set extend to “ArrayAdapter” class. In Kotlin we use ‘:’ to extend a class
class CustomAdapter(val thiscontext: Context,
val os: ArrayList<OperatingSystem>)
: ArrayAdapter<OperatingSystem> (thiscontext,R.layout.listview_layout, os ) {
}
ArrayAdapter is flexible. It This is the custom layout
can handle simple data that have been created
types or object. previously
Next Steps
• Override getView
• Inflate listview_layout
• Update image and text view
Steps 7: Override getView
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val inflater = thiscontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)
as LayoutInflater
}
function getView always get called everytime
data rendered on screen, and its called as
much as the number of data inside array.
<uses-permission
android:name="android.permission.INTERNET" />
Exercise
• Use webview to load Ubaya
website
• Make it fullscreen! (hide action
bar and status bar)
Any QUESTIONS?