Monday, December 18, 2023

Q:-Demonstrate Array Adapter using List View to display list of fruits.

 activity.xml

<RelativeLayout

    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"

    tools:context=".MainActivity">


    <ListView

        android:id="@+id/simpleListView"

        android:layout_width="match_parent"

        android:layout_height="wrap_content" />


</RelativeLayout>


activity.java

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.ListView;

import android.widget.ArrayAdapter;

public class MainActivity extends AppCompatActivity {

        ListView simpleListView;


        // array objects

        String fruit[] = {"Apple", "Banana", "Mango", "strawberry"};


        @Override

        protected void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_main);


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


            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,

                    R.layout.item_view, R.id.itemTextView, fruit);

            simpleListView.setAdapter(arrayAdapter);

        }

    }

fruits,xml

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical">


    <TextView

        android:id="@+id/itemTextView"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:layout_gravity="center" />


</LinearLayout>

No comments:

Post a Comment