1. ListViews, GridViews, Adapters, Dialogs and Toasts
2. ListViews, GridViews, Adapters, Dialogs and Toasts A
ListView or a GridView shows data in an array, a Collection or a
Cursor resulting from a DB query. Contrary to what happens with the
other View, the data entry is done at runtime using an Adapter that
tells the system how to insert the data specifying the graphical
interface of each element. To simplify this management it provides
lists of specialized classes like ListActivity and ListFragment
that add methods to handle the ListView without recovering its
reference from the layout. ListViews and GridViews
3. The main class that manages the View that make a list or a
grid is BaseAdapter. The platform provides specializations of this
class to simplify the creation of the view that compose a list or
grid, but restrict them customization. ArrayAdapter SimpleAdapter
CursorAdapter SimpleCursorAdapter Adapters ListViews, GridViews,
Adapters, Dialogs and Toasts
4. public class ExampleAdapter extends BaseAdapter { @Override
public int getCount() { return 0; } @Override public Object
getItem(int position) { return null; } @Override public long
getItemId(int position) { return 0; } @Override public View
getView(int position, View convertView, ViewGroup parent) { return
null; } } BaseAdapter ListViews, GridViews, Adapters, Dialogs and
Toasts
5. public class ExampleAdapter extends BaseAdapter { private
List titles; @Override public int getCount() { if (titles != null)
return titles.size(); return 0; } @Override public String
getItem(int position) { return titles.get(position); } @Override
public long getItemId(int position) { return position; } @Override
public View getView(int position, View convertView, ViewGroup
parent) { return convertView; } } BaseAdapter ListViews, GridViews,
Adapters, Dialogs and Toasts
6. public class ExampleAdapter extends BaseAdapter { private
List titles; private LayoutInflater inflater; public
ExampleAdapter(Context context) { inflater =
LayoutInflater.from(context); } ... @Override public View
getView(int position, View convertView, ViewGroup parent) { String
title = getItem(position); convertView =
inflater.inflate(R.layout.example_item, null); TextView titleView =
(TextView) convertView .findViewById(R.id.example_title);
titleView.setText(title); return convertView; } } BaseAdapter
ListViews, GridViews, Adapters, Dialogs and Toasts
7. All the times that you have to make a visible element of the
list, a call is made to Adapter.getView () on the Main Thread
because only this can change the graphical interface. Whether to
complex View or simple View, this may affect the fluidity of the
ListView and in the worst cases the entire application. The cause
of this is the call to View.findViewById () that search for
reference of the instance of a View. In most cases the View loaded
for each element of the list is the same, then all subsequent calls
to the first to retrieve the View can be eliminated by maintaining
a reference to all of the Layout View. To do this you must use the
View Holder Pattern. BaseAdapter - Limits ListViews, GridViews,
Adapters, Dialogs and Toasts
8. A ViewHolder object stores a reference of each of the Layout
View in order to reuse it for all elements. public class
ExampleAdapter extends BaseAdapter { private List titles; private
LayoutInflater inflater; ... @Override public View getView(int
position, View convertView, ViewGroup parent) { String title =
getItem(position); ViewHolder holder; if (convertView == null) {
convertView = inflater.inflate(R.layout.example_item, null); holder
= new ViewHolder(); holder.titleView = (TextView) convertView
.findViewById(R.id.example_title); convertView.setTag(holder); }
else { holder = (ViewHolder) convertView.getTag(); }
holder.titleView.setText(title); return convertView; } static class
ViewHolder { TextView titleView; } } View Holder Pattern ListViews,
GridViews, Adapters, Dialogs and Toasts
9. ArrayAdapter is a concrete class that allows the insertion
of a simple list in a ListView. ArrayAdapter itemsAdapter = new
ArrayAdapter(this, android.R.layout.simple_list_item_1, items); By
default, the text entered in the one TextView in the list is the
string returned by toString(). ArrayAdapter ListViews, GridViews,
Adapters, Dialogs and Toasts
10. SimpleAdapter (as the ArrayAdapter) is a concrete class
that allows the insertion of a simple list in a ListView, but
allows a greater level of customization. List> data = new
ArrayList>(); data.add(new HashMap() { { put("description",
"10100"); put("title", "Torino"); } }); ... ListAdapter adapter =
new SimpleAdapter(this, data, android.R.layout.simple_list_item_2,
new String[] { "title", "description" }, new int[] {
android.R.id.text1, android.R.id.text2 }); SimpleAdapter ListViews,
GridViews, Adapters, Dialogs and Toasts
11. CursorAdapter is an abstract class that simplifies the
management of data from a DataBase to show them in a ListView. It
is not necessary to implement the ViewHolderPattern, because the
class manages the reuse of View. public class ExampleCursorAdapter
extends CursorAdapter { private LayoutInflater inflater; public
ExampleCursorAdapter(Context context, Cursor c, boolean
autoRequery) { super(context, c, autoRequery); inflater =
LayoutInflater.from(context); } @Override public void bindView(View
view, Context context, Cursor cursor) { TextView titleText =
(TextView) view.findViewById(R.id.example_title); String title =
cursor.getString(cursor.getColumnIndex("title"));
titleText.setText(title); } @Override public View newView(Context
context, Cursor cursor, ViewGroup parent) { return
inflater.inflate(R.layout.example_item, null); } } CursorAdapter
ListViews, GridViews, Adapters, Dialogs and Toasts
12. Once you have created the adapter, it should be assigned to
the ListView or GridView with the method:
listView.setAdapter(adapter); After you've assigned
all'AdapterView, you no longer need to instantiate a new one: if
and when the data passed to the adapter undergo a change, it is
necessary and sufficient to invoke the method
Adapter.notifyDataSetChanged () to apply your changes and force the
update of the list. adapter.notifyDataSetChanged(); N.B.: If you
change the data, but the method Adapter.notifyDataSetChanged() is
not called, the system will throw an IllegalStateException. Data
management ListViews, GridViews, Adapters, Dialogs and Toasts
13. A Dialog is a small window that asks the user to make a
selection or enter additional information. Dialog ListViews,
GridViews, Adapters, Dialogs and Toasts
14. The Dialog class is the base for the management of this
type of objects, but the platform makes available to the
specialization for certain purposes: AlertDialog: Dialog that shows
a title, a message and up to 3 Button DatePickerDialog: Dialog with
default layout that allows the user to select a date
TimePickerDialog: Dialog with default layout that allows the user
to select a time ProgressDialog: Dialog with title, message and a
ProgressBar to notify the user waiting for an operation of long
duration. Use of this Dialog is not recommended because it prevents
the user from interacting with the application. Dialog ListViews,
GridViews, Adapters, Dialogs and Toasts
15. To create a Dialog you can create an instance and define
the graphical interface: Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.example_dialog); TextView title =
(TextView) dialog.findViewById(R.id.example_title); Button close =
dialog.findViewById(R.id.example_close);
close.setOnClickListener(new OnClickListener() { @Override public
void onClick(View arg0) { dialog.cancel(); } });
title.setText("Title"); dialog.show(); Dialog ListViews, GridViews,
Adapters, Dialogs and Toasts
16. With the introduction of the Fragment, it was added the
possibility of extending DialogFragment to create Dialog. This
allows you to add it to an Activity or show it like a Dialog. The
DialogFragment class has pne more method than the Fragment: it's
used to create the instance of Dialog in which to display the
Fragment to the invocation of the method DialogFragment.show()
public class ExampleDialogFragment extends DialogFragment {
@Override public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.example_dialog_fragment); return
dialog; } } DialogFragment ListViews, GridViews, Adapters, Dialogs
and Toasts
17. An AlertDialog is composed by 3 main components: Title:
optional Content: a message, a list or a custom layout Actions: up
to 3 buttons AlertDialog ListViews, GridViews, Adapters, Dialogs
and Toasts
18. In order to create an AlertDialog, use an
AlertDialog.Builder: AlertDialog.Builder builder = new
AlertDialog.Builder(getActivity());
builder.setMessage(R.string.dialog_message).setTitle(
R.string.dialog_title); // Add the buttons
builder.setPositiveButton(R.string.ok, new
DialogInterface.OnClickListener() { public void
onClick(DialogInterface dialog, int id) { // User clicked OK button
} }); builder.setNegativeButton(R.string.cancel, new
DialogInterface.OnClickListener() { public void
onClick(DialogInterface dialog, int id) { // User cancelled the
dialog } }); AlertDialog dialog = builder.create(); AlertDialog -
Button ListViews, GridViews, Adapters, Dialogs and Toasts
19. In order to create an AlertDialog, use an
AlertDialog.Builder: AlertDialog.Builder builder = new
AlertDialog.Builder(getActivity());
builder.setTitle(R.string.pick_color);
builder.setItems(R.array.colors_array, new
DialogInterface.OnClickListener() { public void
onClick(DialogInterface dialog, int which) { // The 'which'
argument contains the index position // of the selected item } });
AlertDialog dialog = builder.create(); AlertDialog - List
ListViews, GridViews, Adapters, Dialogs and Toasts
20. In order to create an AlertDialog, use an
AlertDialog.Builder: AlertDialog.Builder builder = new
AlertDialog.Builder(getActivity()); mSelectedItems = new
ArrayList();
builder.setTitle(R.string.pick_color).setMultiChoiceItems(
R.array.toppings, null, new
DialogInterface.OnMultiChoiceClickListener() { @Override public
void onClick(DialogInterface dialog, int which, boolean isChecked)
{ if (isChecked) { // If the user checked the item, add it to the
// selected items mSelectedItems.add(which); } else if
(mSelectedItems.contains(which)) { // Else, if the item is already
in the array, // remove it
mSelectedItems.remove(Integer.valueOf(which)); } } }); AlertDialog
dialog = builder.create(); AlertDialog - MultiChoiceList ListViews,
GridViews, Adapters, Dialogs and Toasts
21. In order to create an AlertDialog, use an
AlertDialog.Builder: AlertDialog.Builder builder = new
AlertDialog.Builder(getActivity());
builder.setView(inflater.inflate(R.layout.dialog_signin, null)) //
Add action buttons .setPositiveButton(R.string.signin, new
DialogInterface.OnClickListener() { @Override public void
onClick(DialogInterface dialog,int id){ // sign in the user ... }
}) .setNegativeButton(R.string.cancel, new
DialogInterface.OnClickListener() { public void
onClick(DialogInterface dialog,int id){
LoginDialogFragment.this.getDialog().cancel(); } }); AlertDialog
dialog = builder.create(); AlertDialog Custom Layout ListViews,
GridViews, Adapters, Dialogs and Toasts
22. If you do not want to use the Dialog API to show windows,
you can assign a Dialog style to the Activity declaring it in the
Manifest file: Activity as a Dialog ListViews, GridViews, Adapters,
Dialogs and Toasts
23. A Toast shows the user a message for a short period of time
without interaction. Toast Context context =
getApplicationContext(); CharSequence text = "Hello toast!"; int
duration = Toast.LENGTH_SHORT; Toast toast =
Toast.makeText(context, text, duration); toast.show(); ListViews,
GridViews, Adapters, Dialogs and Toasts