mac_systems Senior-Moderator

Anmeldungsdatum: 24.11.2003 Beiträge: 6094 Wohnort: Düsseldorf
|
Verfasst am: Do Jul 09, 2009 12:29 pm Titel: [TIP] Eigende Adapter schreiben |
|
|
Heute mal ein Bsp. wie eine ListActivity dazu überredet werden kann mit euren Objekten umzugehen.
Zur Darstellung von Listen habe Ich eine einfache ListActivity geschrieben in der momentan dummy Objekte erzeugt werden um diese in einer List darzustellen.
| Java: |
/**
* @author mac
*
*/
public class SpotOverview extends ListActivity
{
/*
* (non-Javadoc)
*
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.spotoverview);
final List<SpotConfigurationVO> spotConfigurations = new ArrayList<SpotConfigurationVO>();
for (int i = 0; i < 10; i++)
{
final SpotConfigurationVO vo = new SpotConfigurationVO();
vo.setFromDirection(WindDirection.E);
vo.setToDirection(WindDirection.W);
vo.setStation(new Station("Test Station", "id", "keyword", true, true));
vo.setPreferredWindUnit(WindUnit.KNOTS);
spotConfigurations.add(vo);
}
final SpotOverviewAdapter adapter = new SpotOverviewAdapter(this, R.layout.custom_listview_spotoverview,
spotConfigurations);
setListAdapter(adapter);
}
} |
Das eigentliche Layout dieser Activity definieren wir hier:
| 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/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id="@+id/android:empty"
android:text="@string/no_entrys"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
|
Der Eintrag mit folgender ID ist wichtig: android:id="@+id/android:empty"
Da er intern genutzt wird falls die Liste leer sein sollte. Würden wir dem TextView nun einen Text im Layout mitgeben würde dieser nur gezeigt falls die List leer ist.
Nun erzeugen wir ein Layout für jeden Eintrag in unserer Liste:
| 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">
<TextView
android:id="@+id/custom_spotoverview_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/custom_spotoverview_tralala"
android:text="@string/no_entrys"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/custom_spotoverview_wind_from"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/custom_spotoverview_wind_to"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
|
Wir haben gerade eine Zeile Text der größe 20sp (Scaled Pixel) definiert, darunter einen TextView neben dem zwei ImageViews sich befinden.
Um nun auf einzelne Einträge der Liste zuzugreifen zu können und gleichzeitig mit dem SpotConfigurationVO Objekt arbeiten zu können brauchen wir einen Adapter:
| Java: |
public class SpotOverviewAdapter extends BaseAdapter |
Adapter haben die Aufgabe Inkompatible Schnittstellen kompatibel zu bekommen (Das Adapter Pattern). Damit eine ListActivity ihren Adapter auch kennt haben wir ihn weiter oben gesetzt:
| Java: |
final SpotOverviewAdapter adapter = new SpotOverviewAdapter(this, R.layout.custom_listview_spotoverview,
spotConfigurations);
|
Un nun schauen wir auf den Adapter:
| Java: |
/**
* @author mac
*
*/
public class SpotOverviewAdapter extends BaseAdapter
{
private final Context context;
private final List<SpotConfigurationVO> spotList;
private final int rowResID;
private final LayoutInflater layoutInflater;
/**
*
* @param _context
* @param _rowResID
* @param _spotList
*/
public SpotOverviewAdapter (final Context _context, final int _rowResID, final List<SpotConfigurationVO> _spotList )
{
context = _context;
rowResID = _rowResID;
spotList = _spotList;
layoutInflater = (LayoutInflater ) context. getSystemService(Context. LAYOUT_INFLATER_SERVICE);
}
/*
* (non-Javadoc)
*
* @see android.widget.Adapter#getCount()
*/
public int getCount ()
{
return spotList. size();
}
/*
* (non-Javadoc)
*
* @see android.widget.Adapter#getItem(int)
*/
public Object getItem (int position )
{
return spotList. get(position );
}
/*
* (non-Javadoc)
*
* @see android.widget.Adapter#getItemId(int)
*/
public long getItemId (int position )
{
return position;
}
/*
* (non-Javadoc)
*
* @see android.widget.Adapter#getView(int, android.view.View,
* android.view.ViewGroup)
*/
public View getView (int position, View convertView, ViewGroup parent )
{
final SpotConfigurationVO weather = spotList. get(position );
final View view = layoutInflater. inflate(rowResID, null);
final TextView spotnameTextView = (TextView ) view. findViewById(R. id. custom_spotoverview_name);
spotnameTextView. setText(weather. getStation(). getName());
final ImageView windFromImageView = (ImageView ) view. findViewById(R. id. custom_spotoverview_wind_from);
windFromImageView. setImageResource(weather. getFromDirection(). getImage());
final ImageView windToImageView = (ImageView ) view. findViewById(R. id. custom_spotoverview_wind_to);
windToImageView. setImageResource(weather. getToDirection(). getImage());
return view;
}
}
|
Die Methode public View getView(int position, View convertView, ViewGroup parent) tut den ganzen Trick indem sie den LayoutInflater nutzt um auf das Root View der Listeneinträge zugreift. Von dort aus suchen wir die einzelnen Views (TextView, ImageView) über ihre IDs und rufen geeignete Methoden auf ihnen auf um geben das Root View zurück.
- Mac
|
|