Blog Details

img
Design

What is a list view in android

Spoke Right / 20 Nov, 2023

What is a list view in android

Android ListView is a ViewGroup that is used to display the list of items in multiple rows and contains an adapter that automatically inserts the items into the list

To include a group of items to be displayed in a scrollable list, the Android ListView is used. It is a view. By importing the android.widget.ListView class, the ListView is implemented. It does not use other scroll views as it is a default scrollable. The Adapter classes are used by the ListView to add the content from the data source to the ListView. The data source can be a string array, array, database, etc. The data between the AdapterViews and other Views like ListView, ScrollView, etc are bridged by the Adapter.

Example of ListView:

activity_main.xml:

In the activity_main.xml file, we will drag and drop the ListView component from the palette.



 
   

mylist.xml:

The view components displayed in the ListView are added in the mylist.xml file which is an additional file created in the layout folder.


strings.xml:

   radioapp
   
       A
       B
       C
       D
       E
       F
       G
       H
       I
       J
       K
       L
       M
       N
       O
       P
       Q
       R
       S
       T
       U
       V
       W
       X
       Y
       Z
   

Activity class:(File: MainActivity.java)

The setAdapter() method of the ListView is used in the MainActivity.java file to add the adapter to the Listview.

package com.example.radioapp;
 
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
 
public class MainActivity extends AppCompatActivity {
   ListView listView;
   TextView textView;
   String[] listItem;
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
 
       listView=(ListView)findViewById(R.id.listView);
       textView=(TextView)findViewById(R.id.textView);
       listItem = getResources().getStringArray(R.array.alphabets);
       final ArrayAdapter adapter = new ArrayAdapter(this,
               android.R.layout.simple_list_item_1, android.R.id.text1, listItem);
       listView.setAdapter(adapter);
 
       listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
           @Override
           public void onItemClick(AdapterView adapterView, View view, int position, long l) {
               // TODO Auto-generated method stub
               String value=adapter.getItem(position);
               Toast.makeText(getApplicationContext(),value,Toast.LENGTH_SHORT).show();
 
           }
       });
   }
}

0 comments

Warning: PHP Startup: Unable to load dynamic library 'imagick.so' (tried: /usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so (/usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so: cannot open shared object file: No such file or directory), /usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so.so (/usr/local/lib/php/extensions/no-debug-non-zts-20210902/imagick.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0