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

Mobile Application and Development-Lec7

The document discusses mobile application development focusing on arrays and adapters in Android. It explains how to declare and use arrays, specifically String and Integer arrays, and introduces ArrayAdapter and SimpleCursorAdapter for binding data to UI components. Several examples illustrate the implementation of these concepts in Android applications, including the use of ListView and Spinner components.

Uploaded by

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

Mobile Application and Development-Lec7

The document discusses mobile application development focusing on arrays and adapters in Android. It explains how to declare and use arrays, specifically String and Integer arrays, and introduces ArrayAdapter and SimpleCursorAdapter for binding data to UI components. Several examples illustrate the implementation of these concepts in Android applications, including the use of ListView and Spinner components.

Uploaded by

h.ejaz0444
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Sir Syed University of Engineering & Technology, Karachi

Mobile Application Development


Lecture 7:Arrays and Adapter

Batch - 2019 Department of Computer Science / Information Technology


Arrays

– We can declare list of items in values folder which contains strings.xml file
– Set of collection of similar data
– Declaration of array in java
For Example
String[] stringArray;
stringArray = myResources.getStringArray(R.array.string_array);
int[] intArray = myResources.getIntArray(R.array.integer_array);
Arrays

<string-array name=“string_array“> <item>3</item>


<item>Item 1</item> <item>2</item>
<item>Item 2</item> <item>1</item>
<item>Item 3</item> </array>
</string-array>

<array name=“integer_array“>
Adapters

– Adapters provide a common interface to the data model behind a selectionstyle


widget, such as a listbox.
– In most cases you won’t have to create your own Adapters from scratch.
Android supplies a set of Adapters that can pump data from common data
sources (including arrays and Cursors) into the native controls that extend
Adapter View
– Two of the most useful and versatile native Adapters:
1) ArrayAdapter
2) SimpleCursorAdapter
ArrayAdapter

– The Array Adapter uses generics to bind an Adapter View to an array of objects
of the specified class. By default, the Array Adapter uses the toString value of
each object in the array to create and populate Text Views.
– The ArrayAdapter constructor takes three parameters:
1) The Context to use (typically this will be your activity instance)
2) The resource ID of a view to use (such as a built-in system resource ID, as
previously shown)
3) The actual array or list of items to show
Example

String[] items={“red", “blue", “green", “yellow"};


ListView listView = findViewById(R.id.listView);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, items);
listView.setAdapter(adapter);
Another Example

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp">
<Spinner android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Another Example

String[] colors = {"Red", "Blue", "Green", "Yellow", "Purple", "Orange"};


spinner = findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<>( this,
android.R.layout.simple_spinner_item, colors);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_i
tem);
spinner.setAdapter(adapter);
SimpleCursorAdapter

– Used to bind data from a Cursor (retrieved from a database) to a user interface.
– Acts as a bridge between the cursor object (which contains data) and layout
defined in XML for displaying data.
– Commonly used when working with SQLite.
public SimpleCursorAdapter( Context context, int layout, Cursor cursor,

String[] from, int[] to, int flags)


Example

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent“
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="@+id/name“
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
Example

<TextView
android:id="@+id/phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Example

ListView listView = findViewById(R.id.listView);


String[] from = {"name", "phone"}; // Columns in the Cursor
int[] to = {R.id.name, R.id.phone}; // Views in list_item.xml
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
R.layout.list_item, // Layout for each row
cursor, // Cursor with data
from, // Columns in the database
to, // Views to bind data
0 // Flags (usually 0));
listView.setAdapter(adapter);
Another Example

– <?xml version="1.0" encoding="utf- android:layout_width="fill_parent"


8"?> android:layout_height="wrap_content"
<LinearLayout />
xmlns:android="http://schemas.android. <Spinner android:id="@+id/spinner"
com/apk/res/android" android:layout_width="fill_parent"
android:orientation="vertical" android:layout_height="wrap_content"
android:layout_width="fill_parent" android:drawSelectorOnTop="true" />
android:layout_height="fill_parent" > </LinearLayout>
<TextView
android:id="@+id/selection"
Another Example

public class SpinnerDemo extends Activity


implements
AdapterView.OnItemSelectedListener
{ TextView selection;
String[] items={"lorem", "ipsum", "dolor", "sit",
"amet", "consectetuer", "adipiscing", "elit",
"morbi", "vel", "ligula", "vitae", "arcu",
"aliquet", "mollis", "etiam", "vel", "erat",
"placerat", "ante", "porttitor", "sodales",
"pellentesque“};
Another Example

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
selection=(TextView)findViewById(R.id.selection);
Spinner spin=(Spinner)findViewById(R.id.spinner);
spin.setOnItemSelectedListener(this);
ArrayAdapter<String> aa=new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,items);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(aa); }
Another Example

public void onItemSelected(AdapterView<?> parent, View v, int position, long id)


{ selection.setText(items[position]); }
public void onNothingSelected(AdapterView<?> parent)
{ selection.setText(""); } }

You might also like