The android.widget.ListView is a core component of the Android SDK, which belongs to the package library android.widget. This widget is used to display a list of scrollable items in your app's user interface.
Here are some code examples for using the ListView widget:
1) Basic ListView: The following code snippet shows how to create a simple ListView with an array of strings as the data source and a default layout for list items:
String[] items = {"apple", "banana", "orange", "pear"}; ListView listView = new ListView(context); ArrayAdapter adapter = new ArrayAdapter(context, android.R.layout.simple_list_item_1, items); listView.setAdapter(adapter);
2) Custom ListView: You can also create a custom ListView by defining your own XML layout for list items and a custom adapter to bind data to the layout. Here's a code example:
public class MyListAdapter extends BaseAdapter { private List items; private LayoutInflater inflater;
public MyListAdapter(Context context, List items) { this.items = items; this.inflater = LayoutInflater.from(context); }
@Override public int getCount() { return items.size(); }
@Override public String getItem(int position) { return items.get(position); }
@Override public long getItemId(int position) { return position; }
ListView listView = new ListView(context); MyListAdapter adapter = new MyListAdapter(context, items); listView.setAdapter(adapter);
In this example, MyListAdapter is a custom adapter that extends the BaseAdapter class and overrides its methods to bind data to a custom layout defined in list_item_layout.xml.
Overall, the android.widget.ListView is a versatile widget that can be used to display a wide variety of data in a scrollable list view.
Java ListView - 30 examples found. These are the top rated real world Java examples of android.widget.ListView extracted from open source projects. You can rate examples to help us improve the quality of examples.