0% found this document useful (0 votes)
3 views

ListView

This document presents a simple Android Java program that demonstrates the use of a ListView. It includes an XML layout file for the ListView and a Java class that initializes the ListView with a list of items using an ArrayAdapter. The items are displayed using the built-in layout android.R.layout.simple_list_item_1.

Uploaded by

riteshguest
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

ListView

This document presents a simple Android Java program that demonstrates the use of a ListView. It includes an XML layout file for the ListView and a Java class that initializes the ListView with a list of items using an ArrayAdapter. The items are displayed using the built-in layout android.R.layout.simple_list_item_1.

Uploaded by

riteshguest
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Here's a simple Android Java program demonstrating the usage of ListView:

1. *Layout XML (activity_main.xml):*

xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout 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:orientation="vertical"

android:padding="16dp"

tools:context=".MainActivity">

<!-- ListView to display items -->

<ListView

android:id="@+id/listView"

android:layout_width="match_parent"

android:layout_height="match_parent" />

</LinearLayout>

2. *Java Code (MainActivity.java):*

java

import android.os.Bundle;
import android.widget.ArrayAdapter;

import android.widget.ListView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.ArrayList;

import java.util.Arrays;

public class MainActivity extends AppCompatActivity {

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

// Initialize ListView

ListView listView = findViewById(R.id.listView);

// Create an ArrayList of items to display in the ListView

ArrayList<String> itemsList = new ArrayList<>(Arrays.asList(

"Item 1", "Item 2", "Item 3", "Item 4", "Item 5",

"Item 6", "Item 7", "Item 8", "Item 9", "Item 10"

));

// Create an ArrayAdapter to populate the ListView with items

ArrayAdapter<String> adapter = new ArrayAdapter<>(

this,
android.R.layout.simple_list_item_1,

itemsList

);

// Set the adapter for the ListView

listView.setAdapter(adapter);

This program creates a simple layout with a ListView that displays a list of items. The ListView is
populated using an ArrayAdapter with a list of items. Each item in the ListView is displayed using the
built-in layout android.R.layout.simple_list_item_1.

You might also like