Hello Friends, Today we will teach you how to add List ( ListView ) in android.
Before adding a list to your project you need to know something about the list and what is listview and why we use listview in our project.
ListView is ViewGroup which contains the group of items and displays it in a scrollable list.
The ListView and GridView are subclasses of AdapterView and they can be populated by binding them to an Adapter, which retrieves data from an external source and creates a View that represents each data entry.
First, we need to add the ListView component to our project.
activity_main.xml :-
We will bind data to our ListView using ArrayAdapter.
Now we will create a ListView object with one String array ( String[] ) and we need ArrayAdapter.
To fill the data in a ListView we simply use adapters. List items are automatically inserted into a list using an Adapter
MainActivity.java :-
Now Run the app and see the output:-
The ListView and GridView are subclasses of AdapterView and they can be populated by binding them to an Adapter, which retrieves data from an external source and creates a View that represents each data entry.
First, we need to add the ListView component to our project.
activity_main.xml :-
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
We will bind data to our ListView using ArrayAdapter.
Now we will create a ListView object with one String array ( String[] ) and we need ArrayAdapter.
To fill the data in a ListView we simply use adapters. List items are automatically inserted into a list using an Adapter
MainActivity.java :-
package com.akshayarora.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView listView;
String[] country = new String[]{"India", "USA", "UK", "Japan"};
ArrayAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView)findViewById(R.id.listView);
adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,country);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView adapterView, View view, int position, long l) {
Toast.makeText(MainActivity.this, adapter.getItem(position), Toast.LENGTH_SHORT).show();
}
});
}
}
Enjoy!




No comments:
Post a Comment
Note: Only a member of this blog may post a comment.