Pages

mardi 14 mai 2013

Android - Adapter implementations


1.1. Developing a custom Adapter

To control the data assignment and to support this assignment to several Views, you create your own Adapterimplementation. For this you would extend an existing adapter implementations or by sub-classing the BaseAdapterclass directly.
ListView calls the getView() method on the adapter for each data element. In this method the adapter determines the layout of the row and how the data is mapped to the Views in this layout.
This root of the layout is typically a ViewGroup (LayoutManager) and contains several other Views, e.g. anImageView and a TextView.

Within the getView() method you would inflate an XML based layout and then set the values of the individual Views in the layout. For inflating an XML layout you can use the system service LayoutInflator. This service can get accessed via the activity or via the context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)method call.
The individual elements in the layout can be found via the findViewById() method call.

1.2. Example Adapter

The following shows an implementation of an own adapter. This adapter assumes that you have two png files (no.png and yes.png) in one of your res/drawable folders. The coding inflates an XML layout file, finds the relevant Views in the layout and sets their content based on the input data.
package de.vogella.android.listactivity;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MySimpleArrayAdapter extends ArrayAdapter<String> {
  private final Context context;
  private final String[] values;

  public MySimpleArrayAdapter(Context context, String[] values) {
    super(context, R.layout.rowlayout, values);
    this.context = context;
    this.values = values;
  }

  @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.rowlayout, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.label);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
    textView.setText(values[position]);
    // Change the icon for Windows and iPhone
    String s = values[position];
    if (s.startsWith("iPhone")) {
      imageView.setImageResource(R.drawable.no);
    } else {
      imageView.setImageResource(R.drawable.ok);
    }

    return rowView;
  }
} 

1.3. Possible input to the adapter and interaction

An adapter can receive any Java object as input. In its getView() method it extracts the correct data from the data object and assigns this data to the views in the row which is representing the data.
The row can also contain views which interact with the underlying data model via the adapter. For example you can have a Checkbox in your row layout and if the Checkbox is selected, the underlying data is changed.

1.4. Data updates in the Adapter

The notifyDataSetChanged() method on the adapter is called if the data has changed or if new data is available.
The notifyDataSetInvalidated() method is called if the data is not available anymore.


0 commentaires:

Enregistrer un commentaire