Ejercicio3 Android
Ejercicio3 Android
'
(
Most Android data-centric classes are built on top of Adapter objects, and thus extend the
AdapterView class. An Adapter
,
Adapter object is used to create View objects for data
!
" #!
View for each of the data
$
%!!
&
!
!%
!
%%
"
"
!
robust of structures).
The most common AdapterView classes you'll encounter are: ListView, Spinner, and
GridView( $
ListView class and GridView, and explore
%
)
* +
"
"
ListView
+!
ListAdapter&
%View objects
for each of the items that should appear in the ListView.
34
%&$
!
ListView. The reason is it makes
%*
!
ListView
!&*
%
& $
5675
&
!
!
ListView is to
"!
ListView object displaying a list of
8
!String array Java object, taken from one of the default ApiDemos
7!
9;
[ 38 ]
Chapter 2
!
!*
Activity. It's quite common for a
ListView
!
*
#
*
=(
$
!
"
!+*
ListAdapter object.
&
%&
%
*
!
In the android"
+R($
!!*
!$
(
#
ListView
<string-array>
&
=
list.setAdapter(new ArrayAdapter(
this,
android.R.layout.simple_list_item_single_choice,
getResources().getStringArray(R.array.colors)));
[ 39 ]
)
* +
1
$
! ListView, but instead a
*
ListView&
%
!*
(
this case you need to tell the ListView
%
!
7!!*
addHeaderView or
addFooterView:
TextView footer = new TextView(this);
footer.setText("Footer View");
list.addFooterView(footer, null, false);
*
that you can also provide an Object
! AdapterView.
getItemAtPosition(index)!
(
%
7! %
%null.
@ !
8 7
B%
!
ListView). The third parameter tells the ListView
!
%
7!
$
[ 40 ]
Chapter 2
(
"
&
ListView
%
!&
$
!
ListView object.
1.
2.
3.
order from:
<string-array name="restaurants">
<item>The Burger Place</item>
<item>Mick's Pizza</item>
<item>Four Buckets \'o Fruit</item>
<item>Sam\'s Sushi</item>
</string-array>
4.
5.
D!
%
LinearLayout.
6.
<ListView> element.
7.
8.
[ 41 ]
)
* +
9.
10.
F
$%
! +&
%main.xml
+
""
=
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="@+id/restaurant"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:entries="@array/restaurants"/>
</LinearLayout>
[ 42 ]
Chapter 2
K
+
ListView. Since
#
?&
*
7
*
!
!
You !
*
& Where should we order
from?
*
Activity+
AndroidManifest.xml+
=
<activity
android:name=".SelectRestaurantActivity"
android:label="Where should we order from?">
In t 7!$*%
!
*
(
!
"
&
+
!
+ + ?
8
$
!
*
*!
$?
>
+
!
+
!res/
values/dimens.xml>
/0+=
[ 43 ]
)
* +
We d
!
!=item_outer_height and item_
inner_height. The item_outer_height
!&
item_inner_height is the height of any View object contained inside the list item.
The padding!
+
+!
%! +dp
!
9L(
?
the user).
In t &
$
* item_outer_height and menu_item_
height are 48sp and 52sp& !" ! ListView rather
?
%!
48sp. The height of a list
!*(
%+&
target list item if you make them too small.
5
*5
(
needs to touch it, make it big.
1.
2.
3.
[ 44 ]
Chapter 2
4.
5.
6.
7.
F
TextView to be the same as the ListView that contains
it. Since this is for our main menu, its height is menu_item_height:
android:layout_width="fill_parent"
android:layout_height="@dimen/menu_item_height"
8.
9.
6
%TextView
&
menu. Open the SelectRestaurantActivity.java+
In the onCreate!
&
setContentView&
the ListViewmain.xml:
ListView restaurants = (ListView)findViewById(R.id.restaurant);
10.
[ 45 ]
)
* +
"
=
public void onCreate(final Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
final ListView restaurants = (ListView)
findViewById(R.id.restaurant);
restaurants.setAdapter(new ArrayAdapter<String>(
this,
R.layout.menu_item,
getResources().getStringArray(R.array.restaurants)));
}
[ 46 ]
Chapter 2
"
%
!! Button list items so that they're not
too close to each other
&
!
!!
ListView!!*
& ListAdapter!!*
us to select a Cheese item, but not for us to request Cheese . In order to
!
8
$!67
$
&
!
quick glance).
"
=
class Burger {
final String name;
int count = 0;
public Burger(String name) {
this.name = name;
}
}
K
$
*
&
name and count+"#
(%
RR&!
%7
!
+
[ 47 ]
)
* +
"!
The Place is to design
!!
! !
/0
4
%& ListAdapter ourselves
*!&
TextView, but can instead build a more
complex layout.
1.
2.
3.
4.
5.
F
counter
B&
7
the same:
android:layout_width="@dimen/item_inner_height"
android:layout_height="@dimen/item_inner_height"
6.
F
7 counter:
android:gravity="center|center_vertical"
7.
We'll also need a text space to display the name of the burger:
<TextView android:id="@+id/text" />
8.
7?=
android:textSize="@dimen/item_text_size"
[ 48 ]
Chapter 2
9.
F
counter and the text label:
android:layout_marginLeft="@dimen/padding"
10.
$
+ ListView& ?
TextView
objects to be the same:
android:layout_width="fill_parent"
android:layout_height="@dimen/item_inner_height"
11.
7
%*&
!
*
counter4
%&
#=
android:gravity="left|center_vertical"
!The Place. Since the counter TextView is a separate
object from the label, it can be independently styled and managed. This makes things much
!
Z7
*
!
Your complete burger_item.xml+
=
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="@dimen/item_outer_height">
<TextView android:id="@+id/counter"
android:textSize="@dimen/item_text_size"
android:textStyle="bold"
android:layout_width="@dimen/item_inner_height"
android:layout_height="@dimen/item_inner_height"
android:gravity="center|center_vertical" />
<TextView android:id="@+id/text"
android:textSize="@dimen/item_text_size"
android:layout_marginLeft="@dimen/padding"
android:layout_width="fill_parent"
android:layout_height="@dimen/item_inner_height"
android:gravity="left|center_vertical" />
</LinearLayout>
[ 49 ]
)
* +
1.
2.
An Adapter
*
&
!
ListView. In the BurgerAdapter
Burger
assign in the constructor:
private final Burger[] burgers;
BurgerAdapter(Burger... burgers) {
this.burgers = burders;
}
3.
4.
An Adapter
7
%*+
%
!&
return their index:
public long getItemId(int index) {
return index;
}
5.
Chapter 2
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
ViewGroup item = (ViewGroup)inflater.inflate(
R.layout.burger_item, null);
return item;
}
6.
The most important method for us in the BurgerAdapter is the getView method.
ListView"
View object to represent each list item
it needs to display:
public View getView(int index, View reuse, ViewGroup parent) {
7.
8.
F$
*
TextView
! Burger
object at the requested index. The counter
!
user if the current count?
=
Burger burger = burgers[index];
counter.setVisibility(
burger.count == 0
? View.INVISIBLE
: View.VISIBLE);
counter.setText(Integer.toString(burger.count));
label.setText(burger.name);
return item;
! ListView4
%&
the ListView changes, the ListView" ListAdapter to reuse each of the View
)
* +
The getViewGroup!
Z burger_item.xml+
We do this using a LayoutInflator
& 7
Activity.
setContentView(int) method loads XML layout resources. The Context
!
parent ViewGroup ListView+
!(
$Burger&
the counter TextView using the View.setVisibility!
(F&
setVisible method takes a Boolean!&
&setVisibility
takes an int value. The reason for this is that Android treats visibility as part of the layout
(
counter
&*"
& " text#
(
% "
&
=
counter.setVisibility(burger.count == 0
? View.GONE
: View.VISIBLE);
ListView
!* *
!
+
!& "#
*
% ListView. When an item is highlighted, its background generally
&
[(
%*
4
%&ListView
!
&Button or EditText ListView
*
*
(
&
ListView from registering OnItemClick
events completely.
If you override the isEnabled(int index) method of ListAdapter,
+! ListView. A common
use of this is to turn certain items into logical separators. For example, a
*
*
&
+
! 75*
5
[ 52 ]
Chapter 2
1.
>
"
!
TheBurgerPlaceActivity, and make sure it extends ListActivity:
public class TheBurgerPlaceActivity extends ListActivity {
2.
3.
4.
5.
[ 53 ]
)
* +
""
=
public class SelectRestaurantActivity extends Activity
implements OnItemClickListener {
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
[ 54 ]
Chapter 2
ListView restaurants = (ListView)
findViewById(R.id.restaurant);
restaurants.setAdapter(new ArrayAdapter<String>(
this,
R.layout.menu_item,
getResources().getStringArray(R.array.restaurants)));
restaurants.setOnItemClickListener(this);
}
public void onItemClick(
AdapterView<?> parent,
View item,
int index,
long id) {
switch(index) {
case 0:
startActivity(new Intent(
this,
TheBurgerPlaceActivity.class));
break;
}
}
}
F
*
!
&
$
%
to
L 5]"5
!"
"
!
[ 55 ]
)
* +
Pop quiz
^ <
!
ListView object to CHOICE_MODE_SINGLE=
a. Add a RadioButton to each item.
9
c.
2. A ListAdapter+
ListView!F "
to reuse a View
!
3
a. When the data model is invalidated or changed.
b. On every item, for rubber-stamping.
c.
3. When a ListView
&
*
=
%
!
4
?
&
%
!
!
The ListView class is great for displaying small to medium amounts of data, but there
*! Z
!
!*
"
!
*
(
%!&
!&
! %% !
@%
!
+
&
"
7?
$ %
8
I"
!&
!*!=
&&
"& !
&
%
! !
ExpandableListView for this type of grouping. Each item is nested inside a group, and a
($"%&
7
%
$!
!"
#
*! %ExpandableListView
to keep the amount of data to a reasonable length. In these cases, consider
%
+
!
More! *%&ListView for the groups, and a
separate Activity for the nested items.
[ 56 ]
Chapter 2
Since the ExpandableList
%
&$
"
a normal ListAdapter
%(&
ExpandableListAdapter
!
=
%
!%F !!*
!ExpandableListAdapter,
it's generally easiest to have your ExpandableListAdapter!!*
!
the BaseExpandableListAdapter&
%!!*
%*
and triggering.
The ExpandableListAdapter
!
! "
#
z
!
7
$View object as
returned by the ExpandableListAdapter. To stop your group label from being
&
$
!View
structures. The default padding for a list item is available as the theme parameter
expandableListPreferredItemPaddingLeft&
!"
=
android:paddingLeft=
"?android:attr/expandableListPreferredItemPaddingLeft"
In order to keep your ExpandableListView looking consistent, it's a good idea to add the
!!
! !
ExpandableListView
"
7
&
<!
# &
"
7
[ 57 ]
)
* +
F
??
& %=$%,
On, and !".
Using the ListView.getAdapter()!
ExpandableListAdapter!!*
&
To fetch the original ExpandableListAdapter&
the getExpandableListAdapter()!
K
make use of the ExpandableListView. OnChildClickListener
interface to receive click events.
F
Activity
!&
%
"
!
"
=
4
%&!!
B
**
[ 58 ]
Chapter 2
F
77!&$
)*+) menu, using GridView.
The GridView %
!
!& !
!
&
!&
#*(
&$
+
z
&!"
#*%
1.
2.
9
!
%*LinearLayout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
3.
4.
[ 59 ]
)
* +
"
=
class FruitItem {
final String name;
final int image;
FruitItem(String name, int image) {
this.name = name;
this.image = image;
}
}
(
&
!
F
*
(9
&$ Q
7!$!
8
%
*
*
Bitmap object
in each FruitItem4
%&
%!
!!!
FruitItem
*
(
L"
?
&
to put them in the res/drawable directory.
-
, .
|&$
*
!!
L6|+
+
!
&!"
%G%#
+! L6|
!"GL|
& %%
8
&
!"!
!
[ 60 ]
Chapter 2
1.
2.
3.
In the ListAdapter.getView!
&
+
fruit_item.xml layout resource:
FruitItem item = items[index];
TextView text = ((TextView)view.findViewById(R.id.text));
ImageView image = ((ImageView)view.findViewById(R.id.icon));
text.setText(item.name);
image.setImageResource(item.image);
4.
5.
>
GridView
)
*+) menu, and name it res/layout/four_buckets.xml.
L
!GridView:
<GridView
xmlns:android="http://schemas.android.com/apk/res/android"
android:numColumns="3"
android:horizontalSpacing="5dip"
android:verticalSpacing="5dip"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
[ 61 ]
)
* +
1.
*
$
"=
public class FourBucketsActivity extends Activity {
2.
3.
4.
5.
6.
7.
8.
Chapter 2
"
* addContentView method. An Activity object may
have any number of View
5
5
!!*
getContentView method.
I
!*
&Z
%
getLayoutInflator() method used is simply a shortcut for LayoutInflator.
from(this). Instead of using an ID and findViewById&! View returned
directly to a GridView, since that's all that our four_buckets.xml+
!
! ArrayAdapter
" TextView
(
!"
!
&
%
AdapterView<ListAdapter>, in
%!!*
+ ListView4
%&
$ %%
7!
(
# *
&
FourBucketsActivity
!
=
Sashimi
Maki Roll
Nigiri
Oshi
[ 63 ]
)
* +
California Roll
Q
Hand Roll
]
Spinner, use a GridView
8
+
4
!*
=
Tuna
K
Snapper
Salmon
Eel
Sea Urchin
Squid
Shrimp
Summary
9
!
!!
B!
!
*
&
!8
*
% ListView is probably one of the most commonly
&
%!
&
!
!!
!*#
#
The GridView8*%%
ListView&
*
%(
%
!
%
%7&
?! !
B" (
"+&
in a GridView&
+
7
!"
!
"
for other items to be displayed.
[ 64 ]
Chapter 2
"
!&!
?View classes
% !
%
&
!+&
!?!
+
!%
[ 65 ]