This is a simplest Android Example for Android ListView
Android ListView is a view group that displays a list of scrollable items.
Android provides ListView and ExpandableListView classes for displaying scrollable list of items.
Note 1: When creating your main activity , you can extend ListActivity to use the default ListView but make sure you declare it as,
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/mainText"
android:id="@android:id/list"
Also, you can assign the list adapter as
setListAdapter(myAdapter);
Thats the only difference.
Note 2: Important: Though you have a ArrayList of Strings as source for listview, whenever you use the ArrayAdapter it should be declared as ArrayAdapter<String>
Note 2: Important: Though you have a ArrayList of Strings as source for listview, whenever you use the ArrayAdapter it should be declared as ArrayAdapter<String>
Adapter:
An adapter manages the data model and adapts it to the individual entries in the widget. An adapter extends the
BaseAdapter
class.Default Adapters:
Android provides default adapter implementations; the most important are
ArrayAdapter
and CursorAdapter
.
ArrayAdapter
can handle data based on Arrays
or java.util.List
. SimpleCursorAdapter
can handle database related data.Step 1 : Create a layout file with ListView
Step 2 : In your Activity, access the ListView created in Step 1 and perform the possible desired actions
- Get ListView object from xml
- Defined Array values to show in ListView
- Define a new Adapter
- Assign adapter to ListView
- Implement ListView Item Click Listener
Step 1 : Create a layout file with ListView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MyListView">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listview1"></ListView>
</RelativeLayout>
Step 2 : In your Activity, access the ListView created in Step 1 and perform the possible desired actions.
public class MyListView extends Activity {
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_list_view);
lv = (ListView)findViewById(R.id.listview1); // Get ListView object from xml
String values[] = {"Jellybean","Kitkat","Lollipop"}; // Defined Array values to show in ListView
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, android.R.id.text1,values); // Define a new Adapter
lv.setAdapter(adapter); // Assign adapter to ListView
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { //Implement ListView Item Click Listener
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
int position = i;
String x = (String)lv.getItemAtPosition(i);
Toast.makeText(getApplicationContext(),"the selected item is " +x+" and at postion "+position, Toast.LENGTH_LONG).show();;
}
});
}
OutPut :
Excellent Tutorial on Customized Adapter is available in
http://www.codelearn.org/android-tutorial/android-listview
No comments:
Post a Comment