In this android example creating a simple ListView with ListActivity to display a array values.
activity_list_view_android_example.xml File
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView android:id="@+id/output"
android:background="#758AA7"
android:layout_height="wrap_content"
android:padding="1px"
android:textColor="#ffffff"
android:text="Click : " android:layout_width="fill_parent" />
<ListView
android:id="@android:id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
</LinearLayout>
Explanation :
Define ListView in xml file
<ListView
android:id="@android:id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
NOTE :
When creating Listview with ListActivity then two things are MUST contain a ListView with the android:id attribute set to @android:id/list ( NOT android:id="@+id/list" ).
if you will defined ListView with the android:id attribute set to @+id/list then it will give error.
=========================================================================================
ListViewAndroidExample.java File
public class ListViewAndroidExample extends ListActivity {
TextView content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view_android_example);
content = (TextView)findViewById(R.id.output);
//listView = (ListView) findViewById(R.id.list);
String[] values = new String[] { "Android Example ListActivity", "Adapter implementation", "Simple List View With ListActivity",
"ListActivity Android", "Android Example", "ListActivity Source Code", "ListView ListActivity Array Adapter", "Android Example ListActivity" };
// Define a new Adapter
// First parameter - Context
// Second parameter - Layout for the row
// Third - the Array of data
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, values);
// Assign adapter to List
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) l.getItemAtPosition(position);
content.setText("Click : \n Position :"+itemPosition+" \n ListItem : " +itemValue);
}
}
Explanation:
Adapters are used to provide the data to the ListView
Parameters:
simple_list_item_1 : Android internal layout view
values : User defined data array.
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, values);
// Assign adapter to List
setListAdapter(adapter);
When Click on list item then onItemClick method in ListView Item Click Listener called
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// ListView Clicked item index
int itemPosition = position;
// ListView Clicked item value
String itemValue = (String) l.getItemAtPosition(position);
content.setText("Click : \n Position :"+itemPosition+" \n ListItem : " +itemValue);
}