Android has ListView and ExpandableListView classes capable of displaying scrollable list of items. The ExpandableListView supports a scrollable list of items.
An Adapter manages the data model and adapts to the individual rows in the list view. An Adapter extends the BaseAdapter class.
Every line in the list view consists of a layout and application can choose the complexity of it.
Adapters
An Adapter manages the data model and adapts to the individual rows in the list view. An adapter extends the BaseAdapter class. The Adapter would inflate the layout for each row in its getView method and assign data to the individual views in the row.
The Adapter is assigned to the ListView via the setAdapter method in the ListView object.
the default normal adapters provided by the system are ArrayAdapters and CursorAdapter
A sample of ListView with ArrayAdapter
- First of all make the layout in the xml like below
android:id="+id/listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
By default if Application provides a simple array adapter, each element n the array adapter array will be taken as the row item and display it in the list view
Applications can create a list view row with own layout. A sample given below which shows an image view and a text view in a row
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
…
/>
android:id="+id/textview"
…
…
/>
After this, in the code, the below can be done
String[] values = new String[]{"Android" , "iPhone", "WindowsMobile", "Blackberrty" , "WebOS" };
ArrayAdapter adapter = new ArrayAdapter (this, R.layout.rowlayout, R.id.label, values);
setLlistAdapter (adapter)
Application can create an Own adapter as well. Below sample code shows the
extends from the Simple Adapter such as array adapter overrider the getView method
public class MySimpleCustomAdapter extends ArrayAdapter
{
private final Context context;
private final String[] values;
public MySimpleCustomAdapter (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)
{
LayoutInflator inflator = (LayoutInflator) context.getSystemService(Context.LAYOUT_INFLATOR_SERVICE);
View rowView = inflator.inflate(R.layout.rowLayout,parent,false);
TextView tview = (TExtView)rowView.findViewById(R.id.label);
ImageView view = (ImageView) rowView.findViewById(R.id.imageview);
tview.setText(values[position]);
view.setImageResource(R.drawble.mycustomimage);
}
}
references:
No comments:
Post a Comment