Home >> Miscellaneous
In this tutorial we shall be covering Android's ListView API with the help
of an example with some other view elements such as ImageView, CheckBox etc.
Final screen of this Expandable listview example would look like the below image:
List of groups as initial view of this application showing two different
sections as different categories.
Following image shows screen after clicking both the items from the expandable
ListView:

This example ListView initial screen layout as follows:
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="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
|
listitemview1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/camera1" android:src="@drawable/camera1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<CheckBox android:id="@+id/chkbox1"
android:text="Add to Shopping Cart"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
|
listitemview2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/cupboard"
android:src="@drawable/cupboards1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<CheckBox android:id="@+id/chkbox1"
android:text="Add to Shopping Cart"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
|
Using Android API such as ListActivity and BaseAdapter class
files, those can be extended by example specific Java class files such as
"ExampleListView" extends ExpandableListActivity and a private inner class file
such as "ExampleListViewAdapter" extends BaseExpandableListAdapter class file.
ExampleListView.java
package com.techienjoy.example.listview;
import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
public class ExampleListView extends ListActivity {
private LayoutInflater layoutInflater = null;
private LinearLayout linearLayout = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
linearLayout = new LinearLayout(this);
ExampleListViewAdapter adapter = new ExampleListViewAdapter(this);
setListAdapter(adapter);
}
private class ExampleListViewAdapter extends BaseAdapter {
private Context context;
public ExampleListViewAdapter(Context context) {
super();
this.context = context;
}
public int getCount() {
return 2;
}
public Object getItem(int arg0) {
return null;
}
public long getItemId(int arg0) {
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
if(layoutInflater == null)
layoutInflater = (LayoutInflater) context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(position == 0) {
linearLayout = (LinearLayout) layoutInflater.
inflate(R.layout.listitemview1, null);
}
if(position == 1) {
linearLayout = (LinearLayout) layoutInflater.
inflate(R.layout.listitemview2, null);
}
return linearLayout;
}
}
}
|
Once all these files are placed in folders as appropriate within Eclipse
Workspace and Android Project, Java files compiled and *.apk file created.
Then this APK file can be published to Android Emulator for getting installed
and displaying on invocation of this application at runtime.
If you would like to share your thoughts on this, please write to us @
usingframeworks @ gmail dot com
If anything missed out , please let me know at
techienjoy at yahoo . com
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| Android ListView Example : |
Example on Android ListView and
explained with a very simple scenario
and article with appropriate screens
captured and shown.
|
|
| Android Gallery Example : |
Example on Android Gallery View
explained with a very simple scenario
and appropriate screens captured and shown.
|
|
|
|
|
|
| Android ListView Example : |
Example on Android List View
explained with a very simple scenario
and article with appropriate screens
captured and shown.
|
|
|
|
|
|
| Android Sensors Example : |
Example on Android Sensors Listed and
explained with a very simple scenario
and article with appropriate screens
captured and shown.
|
|
| Google GWT Example : |
Example using GWT and some design patterns and various
ways of implementing this example.
|
|
|
|
|
|
|
|
|
|
|
|
|
| Android Tab View Example : |
Example on Android Tab View
explained with a very simple scenario
and appropriate screens captured and shown.
|
|
|
|
|
|
|
|
|
|
| Android ListView Example : |
Example on Android ListView
explained with a very simple scenario
whereby showing folder and files with
structure and appropriate screens
captured and shown.
|
|
| Android Examples : |
List of ANDROid examples
with source code and output
screens captured and shown.
|
|