0% found this document useful (0 votes)
12 views

Assignment 01

The document contains XML code for activity_main.xml and list_items.xml layout files and Java code for the MainActivity class. The activity_main.xml file contains a RecyclerView and a button. The list_items.xml file contains a TextView and Button for each recycler view item. The MainActivity code manages the recycler view adapter and list, allows adding, deleting, and updating items, and sets click listeners on each item.

Uploaded by

sher afzal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Assignment 01

The document contains XML code for activity_main.xml and list_items.xml layout files and Java code for the MainActivity class. The activity_main.xml file contains a RecyclerView and a button. The list_items.xml file contains a TextView and Button for each recycler view item. The MainActivity code manages the recycler view adapter and list, allows adding, deleting, and updating items, and sets click listeners on each item.

Uploaded by

sher afzal
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

ASSIGNMENT

UNIVERSITY OF MIANWALI

Submitted By : Misbah Tahir


Roll no : BSCSF20M024
Submitted To : Sir Faiz Ur Rehman
Submission Date : 18 June, 2023
Department of Computer Science,
UMW

XML code for activity_main.xml file:

<?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">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycleView"
android:layout_width="match_parent"
android:layout_height="500dp"
android:layout_weight="1" />
<Button
android:id="@+id/addButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="135dp"
android:text="Add Item"
android:textStyle="bold"
android:textSize="20dp"/>

</LinearLayout>

XML code for list_items.xml file:

<?xml version="1.0" encoding="utf-8"?>


<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_marginLeft="100dp"
android:layout_marginRight="100dp"
android:textSize="20dp"
android:padding="18dp"/>

<Button
android:id="@+id/deleteButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:text="Delete"
android:textSize="20dp"
android:textStyle="bold"/>

</LinearLayout>

JAVA Code for MainActivity.java file:

package com.firstapp.managerecycleview;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

private RecyclerView recyclerView;


private ItemAdapter itemAdapter;
private List<String> itemList;
private int itemCounter = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

recyclerView = findViewById(R.id.recycleView);
recyclerView.setLayoutManager(new LinearLayoutManager(this));

itemList = new ArrayList<>();


itemAdapter = new ItemAdapter(itemList);
recyclerView.setAdapter(itemAdapter);

Button addButton = findViewById(R.id.addButton);


addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{ addItem();
}
});
}

private void addItem() {


String newItem = "item" + itemCounter;
itemList.add(newItem);
itemAdapter.notifyItemInserted(itemList.size() - 1);
itemCounter++;
}

private void deleteItem(int position) {


itemList.remove(position);
itemAdapter.notifyItemRemoved(position);
}

private void updateItem(int position, String newItem) {


itemList.set(position, newItem);
itemAdapter.notifyItemChanged(position);
}

private class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ItemViewHolder> {


private List<String> itemList;

public ItemAdapter(List<String> itemList) {


this.itemList = itemList;
}

@Override
public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_items, parent,
false);
return new ItemViewHolder(view);
}

@Override
public void onBindViewHolder(ItemViewHolder holder, final int position) {
final String item = itemList.get(position);
holder.itemTextView.setText(item);

holder.deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
deleteItem(position);
}
});

holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String updatedItem = "Updated Item " + itemCounter;
updateItem(position, updatedItem);
Toast.makeText(MainActivity.this, "Item updated",
Toast.LENGTH_SHORT).show();
}
});
}

@Override
public int getItemCount() {
return itemList.size();
}

public class ItemViewHolder extends RecyclerView.ViewHolder {


TextView itemTextView;
Button deleteButton;

public ItemViewHolder(View itemView) {


super(itemView);
itemTextView = itemView.findViewById(R.id.itemTextView);
deleteButton = itemView.findViewById(R.id.deleteButton);
}
}
}
}

OUTPUT:

You might also like