Only for Android developers

How to create a ListView.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
        android:id="@+id/scroll"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteX="100dp"
        tools:layout_editor_absoluteY="88dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.moonlight.quiz;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;

import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ListView scroll=findViewById(R.id.scroll);

        ArrayList<String> arr=new ArrayList<>();
        arr.add("11806769");arr.add("11569846");arr.add("11624593");
        arr.add("19056324");arr.add("11956349");
ArrayList<String> arrname=new ArrayList<>();
        arrname.add("Vijaykanth");arrname.add("Prashanth");arrname.add("Srikanth");
        arrname.add("Vijaya");arrname.add("Parul");
        scroll.setAdapter(new ArrayAdapter<String>(this,R.layout.support_simple_spinner_dropdown_item,arr));


        scroll.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                Intent iv= new Intent(MainActivity.this,main2.class);
                iv.putExtra("reg",arr.get(i));
                iv.putExtra("name",arr.get(i));
                startActivity(iv);

            }
        });

    }

}

Leave a comment

Design a site like this with WordPress.com
Get started