ListActivity
is to simplify the handling of ListView
. It extends Activity
and provides simplified handling of lists.
Difference with Activity
- Override
onListItemClick()
method to add item click listener. WithActivity
, we need to create a separate listener withOnClickListener
. - No need to use
findViewById()
to find listview in layout resource. - Could show a special view to replace list view automatically when there is no data in list.
setListAdapter()
to set adapter instead of callingListView.setAdaper()
.
Please inform me if any expert know more about it.
Example
Points to pay attention:
ListView
must set id@android:id/list
. SoListActivity
could find it automatically.- The view to show when there is no data in the list must set id
android:id/empty
.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#00FF00"
android:layout_weight="1"
android:drawSelectorOnTop="false"/>
<TextView android:id="@android:id/empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:text="No data"/>
</LinearLayout>
Then extend ListActivity
and override onListItemClick()
to add item listener
public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] data = {"a", "b", "c"};
// Set data null to test no data textview
// String[] data = {};
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, data);
// Add adapter
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Toast.makeText(this, "Position:" + position + " id:" + id, Toast.LENGTH_SHORT)
.show();
}
}