0% found this document useful (0 votes)
42 views5 pages

LAB 07 Adapters: TASK 01: Code: Activity Mainxml

This document contains code and output for two tasks in Lab 07 on Adapters. Task 1 creates a simple list view with hardcoded string data. Task 2 adds click handling to the list items to display a toast with the clicked item name.

Uploaded by

Maryam Khalil
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)
42 views5 pages

LAB 07 Adapters: TASK 01: Code: Activity Mainxml

This document contains code and output for two tasks in Lab 07 on Adapters. Task 1 creates a simple list view with hardcoded string data. Task 2 adds click handling to the list items to display a toast with the clicked item name.

Uploaded by

Maryam Khalil
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/ 5

MARYAM KHALIL LAB : 07 02-134181-148

LAB 07
ADAPTERS

TASK 01:

CODE:

Activity mainxml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="center"
android:textColor="#111112"
/>

<ListView
android:id="@+id/simpleListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#8A8F8F"
android:listSelector="#2DC6C6"

/>

/>
</LinearLayout>

Main Activity Java:

package com.example.listexample;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
MARYAM KHALIL LAB : 07 02-134181-148

ListView simpleList;
String weekdays[]={"Monday", "Tuesday", "Wednesday", "Thursday", "Friday",
"Saturday", "Sunday"};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
simpleList= (ListView)findViewById(R.id.simpleListView);
ArrayAdapter<String> arrayAdapter = new
ArrayAdapter<>(this,R.layout.activity_main,R.id.textView, weekdays);
simpleList.setAdapter(arrayAdapter);

}
}

OUTPUT:
MARYAM KHALIL LAB : 07 02-134181-148

TASK 02:

CODE:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="50dp"
MARYAM KHALIL LAB : 07 02-134181-148

android:gravity="center"
android:textColor="#111112"
/>

<ListView
android:id="@+id/simpleListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#8A8F8F"
android:listSelector="#2DC6C6"

/>

/>
</LinearLayout>

package com.example.listexample;

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.Button;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String studentList[] = {"Maira","Wajiha","Maryam","Hunaina"};
ListView list = findViewById(R.id.simpleListView);
ArrayAdapter<String> adept = new
ArrayAdapter(this,R.layout.activity_main,R.id.textView,studentList);
list.setAdapter(adept);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
int index =position;
Toast.makeText(getApplicationContext(),"clicked "+studentList
[index],Toast.LENGTH_LONG).show();

}
});

}
MARYAM KHALIL LAB : 07 02-134181-148

OUTPUT:

You might also like