Prac14
Prac14
Android_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">
<EditText
android:id="@+id/editTextText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:hint="@string/name"
android:autofillHints="for normal value adding"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="16dp" />
<EditText
android:id="@+id/editTextText2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text"
android:hint="@string/add_value_when_you_want_change_from_list"
android:autofillHints="add text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/editTextText"
android:layout_marginTop="16dp" />
<Button
android:id="@+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/insert"
app:layout_constraintTop_toBottomOf="@id/editTextText2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="10dp" />
<Button
android:id="@+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/update"
app:layout_constraintTop_toBottomOf="@id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="10dp" />
<Button
android:id="@+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/delete"
app:layout_constraintTop_toBottomOf="@id/button2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="10dp" />
<ListView
android:id="@+id/list"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@id/button3"
app:layout_constraintBottom_toTopOf="@+id/spinner"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp" />
<Spinner
android:id="@+id/spinner"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginTop="10dp" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.myapplication;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText1 = findViewById(R.id.editTextText);
editText2 = findViewById(R.id.editTextText2);
Button insertButton = findViewById(R.id.button);
Button updateButton = findViewById(R.id.button2);
Button deleteButton = findViewById(R.id.button3);
ListView listView = findViewById(R.id.list);
Spinner spinner = findViewById(R.id.spinner); // Add spinner reference if needed
dataList = new ArrayList<>();
adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, dataList);
listView.setAdapter(adapter);
insertButton.setOnClickListener(v -> {
String newItem = editText1.getText().toString().trim();
if (!newItem.isEmpty()) {
dataList.add(newItem); // Add new item to the list
adapter.notifyDataSetChanged(); // Refresh the ListView
editText1.setText(""); // Clear input field
Toast.makeText(MainActivity.this, "Item Inserted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Please enter a valid item",
Toast.LENGTH_SHORT).show();
}
});
updateButton.setOnClickListener(v -> {
String updatedItem = editText2.getText().toString().trim();
if (selectedIndex >= 0 && selectedIndex < dataList.size() && !updatedItem.isEmpty()) {
dataList.set(selectedIndex, updatedItem); // Update the selected item
adapter.notifyDataSetChanged(); // Refresh the ListView
editText2.setText(""); // Clear input field
Toast.makeText(MainActivity.this, "Item Updated", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Please select an item and enter valid data",
Toast.LENGTH_SHORT).show();
}
});
deleteButton.setOnClickListener(v -> {
if (selectedIndex >= 0 && selectedIndex < dataList.size()) {
dataList.remove(selectedIndex); // Remove the selected item
adapter.notifyDataSetChanged(); // Refresh the ListView
editText1.setText(""); // Clear input fields
editText2.setText("");
selectedIndex = -1; // Reset selected index
Toast.makeText(MainActivity.this, "Item Deleted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Please select an item to delete",
Toast.LENGTH_SHORT).show();
}
});
listView.setOnItemClickListener((parent, view, position, id) -> {
selectedIndex = position; // Save the index of the selected item
editText1.setText(dataList.get(position)); // Show selected item in EditText1
editText2.setText(dataList.get(position)); // Also show in EditText2
});
}