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

lecture 5

This document is a lecture on Mobile Application Development in Java, focusing on LinearLayout and ListView components in Android. It explains the features, attributes, and usage of LinearLayout, as well as the implementation and benefits of ListView, including its recycling mechanism and limitations compared to RecyclerView. Additionally, it covers the role of adapters, particularly ArrayAdapter and CursorAdapter, in managing data for UI components.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

lecture 5

This document is a lecture on Mobile Application Development in Java, focusing on LinearLayout and ListView components in Android. It explains the features, attributes, and usage of LinearLayout, as well as the implementation and benefits of ListView, including its recycling mechanism and limitations compared to RecyclerView. Additionally, it covers the role of adapters, particularly ArrayAdapter and CursorAdapter, in managing data for UI components.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Faculty Of Engineering and Technology

Department of Computer Science


Course: Mobile Application In java

(Lecture-5)

Sayed Mortaza Kazemi


Department of Computer Science
Email: [email protected]
Mobile: +93(0) 795474969
Content

• Linear Layout
• ListViews and Adapters
• Working with Linear Layout
• Understanding Built-in Adapter

2
What is LinearLayout?
• LinearLayout is a ViewGroup that aligns all its child views in a single direction,
either vertically or horizontally.
• It is one of the most basic and commonly used layouts in Android.
• Why Use LinearLayout?
• Simplicity:
• It is easy to implement and is useful for simple UI designs.
• Alignment:
• Ensures a clear, ordered arrangement of views.
• Flexibility:
• Can nest other layouts, allowing complex UIs.
• Weight Distribution:
• Efficient in distributing space among child views using layout weights.

3
Attributes of LinearLayout

android:orientation
Defines the direction of the child views.
 vertical: Aligns child views vertically (stacked on top of each other).
 horizontal: Aligns child views horizontally (side by side).
android:gravity
Aligns the child views inside the LinearLayout (e.g., center, left, right, etc.).
android:layout_weight
Specifies how much space a child view should occupy relative to others.

4
ListView
• is a ViewGroup that displays a vertically scrollable list of items, which can
be populated from an array, database, or other data sources.
• It is one of the most commonly used UI components for displaying large
sets of data efficiently in a mobile app
• Scrolling: Items in a ListView are displayed in a scrollable list format,
which makes it suitable for displaying long lists of items.
• Item Layouts: You can define custom layouts for each item in the list. The
list adapts to the number of items dynamically.
• Recycling Mechanism: ListView uses an internal recycling mechanism to
reuse the views that are out of sight, which helps in optimizing memory
and performance.

5
Introduction to ListView

• A ListView is a UI component used to display a vertically


scrollable list of items.
• Each item in the list is an instance of the view, typically a
TextView.
• Commonly used in scenarios where data needs to be
displayed in a structured list format.

6
ListView Basic Usage
• Heading: ListView Implementation (Java)
• Code Example:
• ListView listView = findViewById(R.id.myListView);
• String[] items = {"Apple", "Banana", "Cherry", "Date", "Elderberry"};
• // Using ArrayAdapter for simple lists
• ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, items);
• listView.setAdapter(adapter);

7
Explanation:

•ArrayAdapter connects the data source (items array) with the ListView.
•simple_list_item_1 is a built-in layout for list items.

8
Basic Usage of ListView
• Step 1: Define the ListView in XML
• Step 2: Prepare the Data
• Step 3: Set Up the Adapter To bind data to the ListView, you need an
adapter (usually an ArrayAdapter or a CustomAdapter for more complex
layouts). An adapter converts the data into views for the ListView
• ArrayAdapter: This is one of the simplest types of adapters that takes data
from a source (e.g., an array or a list) and uses it to populate views in the
ListView.

9
Benefits of ListView
• Step 4: Handling Item Clicks You can handle clicks on individual items
using an OnItemClickListener.
• Efficient Handling of Large Datasets: Thanks to its recycling mechanism,
ListView is efficient in handling large amounts of data without consuming
too much memory.
• Versatility: ListView can be populated from different data sources,
including arrays, lists, and databases.
• Flexibility: You can fully customize the appearance and behavior of the list
items by providing custom layouts and adapters.

10
Limitations of ListView
• Performance: While ListView is efficient, for highly dynamic or complex
datasets.
• RecyclerView (a more advanced component) is often recommended as it
provides more flexibility and better performance.
• RecyclerView is a more modern and flexible replacement for ListView. It
supports more complex layouts, animations, and can handle large
datasets more efficiently.
• If you are working with static lists, ListView is simpler to implement. For
dynamic and performance-intensive lists, you may want to consider using
RecyclerView.

11
Understanding Adapters

What are Adapters?


Content:
Adapters act as a bridge between UI components and data
sources.
They manage data and bind it to UI components like ListView,
GridView, etc.

12
Built-in Adapters
Using Built-in Adapters
Content
• Android provides built-in adapters like ArrayAdapter, CursorAdapter, and
SimpleCursorAdapter.
• These adapters help manage simple data structures (arrays, database
queries, etc.) without requiring custom implementations.

13
ArrayAdapter Overview
What is ArrayAdapter?
Content:
 ArrayAdapter is a built-in adapter that connects an array or list of
data to a ListView.
 It can handle simple arrays and automatically creates views for each
item.
 Ideal for basic, small datasets like arrays of strings or integers.

14
CursorAdapter Overview

What is CursorAdapter?
Content
• CursorAdapter is used to bind data from a database (Cursor) to a
ListView.
• It is efficient for working with large datasets retrieved via SQL
queries.
• Data is provided by a Cursor, typically from a database like SQLite.

15
CursorAdapter Example

16
Thank You…!

You might also like