Download adapter

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
PENGEMBANGAN APLIKASI
PERANGKAT BERGERAK
(MOBILE)
“
ListView, Collection, Adapter
K Candra Brata
[email protected]
Mobille App Lab 2015-2016
ListView
http://developer.android.com/guide/topics/ui/layout/listview.html
ListView
 A specific view that shows items in a vertically scrolling list.
 An ordered collection of selectable choices.
 Uses adapter to populate listView items with data.
ListView
 key attributes in XML:
 2 kind of ListView :
 Static List.
 Dynamic List.
Static ListView
static list: Content is fixed and known before the app runs.
– Declare the list elements in the strings.xml resource file.
<!-- res/values/strings.xml -->
<resources>
<string-array name="oses">
<item>Android</item>
<item>iPhone</item>
...
<item>Max OS X</item>
</string-array>
</resources>
<!-- res/layout/activity_main.xml -->
<ListView ... android:id="@+id/mylist"
android:entries="@array/oses" />
Dynamic ListView
Dynamic list: Content is read or generated as the program runs.
 Content comes from a data file, or from the internet (WS), etc.
 Must be set in the Java code.
Uses adapter to populate listView items with data.
Dynamic ListView (adapter)
adapter: Helps turn list data into list view items.
– common adapters: ArrayAdapter, ListAdapter, BaseAdapter.
Syntax for creating an adapter:
ArrayAdapter<String> name =
new ArrayAdapter<String>(activity, layout, array);
● the activity is usually (this).
● the default layout for lists is (android.R.layout.simple_list_item_1)
● get the array by reading your file or data source of choice
(it can be an array like String[], or a list like ArrayList<String>)
– Once you have an adapter, you can attach it to your list by calling the
setAdapter method of the ListView object in the Java code.
listactivity.XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/selection"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#F44333"
android:gravity="center_horizontal"
android:text="List Sitem Operasi"
android:textSize="20dp"/>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawSelectorOnTop="false"
/>
</LinearLayout>
MainList.Java
public class MainList extends Activity {
private String[] menu =
{"Android","iPhone","windowsMobile","BlackBerry","WebOS","Ubuntu","windows7","Mac OS X"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listactivity);
ListView listsisop = (ListView) findViewById(R.id.listView);
ArrayAdapter<String> myadapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,menu);
listsisop.setAdapter(myadapter);
}
........
}
Dynamic ListView (Event)
How to handle List event??
 Unfortunately lists don't use a simple onClick event.
 Several fancier GUI widgets use other kinds of events.
 The event listeners must be attached in the Java code, not in the XML.
 Understanding how to attach these event listeners requires the use of
Java anonymous inner classes.
anonymous inner class: A shorthand syntax for declaring a small class
without giving it an explicit name.
 The class can be made to extend a given super class or implement a
given interface.
 Typically the class is declared and a single object of it is constructed
and used all at once.
Dynamic ListView (Event)
List views respond to the following events:
 setOnItemClickListener(AdapterView.OnItemClickListener)
Listener for when an item in the list has been clicked.
 setOnItemLongClickListener(AdapterView.OnItemLongClickListener)
Listener for when an item in the list has been clicked and held.
setOnItemSelectedListener(AdapterView.OnItemSelectedListener)
Listener for when an item in the list has been selected.
Others:
– onDrag, onFocusChanged, onHover,
onKey, onScroll, onTouch, ...
Dynamic ListView (Event)
ListView list = (ListView) findViewById(R.id.id);
list.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> list,View row,int index,long rowID) {
// code to run when user clicks that item
...
}
}
);
MainList.Java
public class MainList extends Activity implements AdapterView.OnItemClickListener {
private String[] menu =
{"Android","iPhone","windowsMobile","BlackBerry","WebOS","Ubuntu","windows7","Mac OS X"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listactivity);
ListView listsisop = (ListView) findViewById(R.id.listView);
ArrayAdapter<String> myadapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,menu);
listsisop.setAdapter(myadapter);
listsisop.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(this,menu[position], Toast.LENGTH_SHORT).show();
}
Custom List Layout
Fancy List
If you want your list to look different than the default appearance (of
just a text string for each line), you must:
 Write a short layout XML file describing the layout for each
row.
 Write a custom subclass of ArrayAdapter that overrides the
getView() method to describe what view must be returned for
each row.
Custom Adapter
The true power of the ListView and Adapter combination is the ability to define your own
Adapter to best suit you application.
Adapter is simply an interface that needs to be implemented.
Object
listView widget
in Activity's UI
rowView.xml
ListView
Item View
XML file
Adapter
Layout
Inflater
Collection
(Data Set)
Context
ArrayList<Object>
rowview.XML
<?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="wrap_content"
android:background="#FFFFFF"
android:orientation="horizontal">
<ImageView
android:id="@+id/logo"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:src="@mipmap/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical|left"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="@+id/namaos"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="8dp"
android:text="dummy text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="16dp" />
</LinearLayout>
</LinearLayout>
Sisop.Java
public class Sisop {
public String nama;
public Sisop (String nama) {
this.nama = nama;
}
}
CustomAdapter.Java
public class CustomAdapter extends ArrayAdapter<Sisop> {
public Context context;
public ArrayList<Sisop> listos = null;
public CustomAdapter(Context context, ArrayList<Sisop> listos) {
super(context, R.layout.rowview, listos);
this.context = context;
this.listos = listos;
}
// Constructor adapter
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.rowview, parent, false);
TextView nama = (TextView) rowView.findViewById(R.id.namaos);
nama.setText(listos.get(position).nama);
return (rowView);
}
// modify getView()
MainList.Java
public class MainList extends Activity implements AdapterView.OnItemClickListener{
private ArrayList<Sisop> listOs = new ArrayList<Sisop>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listactivity);
ListView listsisop = (ListView) findViewById(R.id.listView);
listOs.add(new Sisop("Android”));
listOs.add(new Sisop("iPhone"));
listOs.add(new Sisop("windowsMobile"));
listOs.add(new Sisop("BlackBerry"));
listOs.add(new Sisop("WebOS"));
listOs.add(new Sisop("Ubuntu"));
listOs.add(new Sisop("Windows7"));
listOs.add(new Sisop("Mac OS x"));
CustomAdapter myadapter = new CustomAdapter(this,listOs);
listsisop.setAdapter(myadapter);
listsisop.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(this, listOs.get(position).nama, Toast.LENGTH_SHORT).show();
}
Thanks!
QUESTIONS?
JOIN !!
https://groups.google.com/d/forum/papb_tif_C