Mobile Application and Development-Lec7
Mobile Application and Development-Lec7
– 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
<array name=“integer_array“>
Adapters
– 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
– 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,
<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
@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