XML
<?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"
android:padding="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Spinner
android:id="@+id/spCountry"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Spinner
android:id="@+id/spFruits"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="16dp">
<EditText
android:id="@+id/edtFruit"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Enter fruit name" />
<Button
android:id="@+id/btnSubmit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
<Button
android:id="@+id/btnInsert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Insert" />
</LinearLayout>
<ListView
android:id="@+id/lvFruits"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="16dp" />
</LinearLayout>
JAVA
package com.example.bai5;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
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;
public class MainActivity extends AppCompatActivity {
private Spinner spCountry, spFruits;
private ListView lvFruits;
private EditText edtFruit;
private Button btnSubmit, btnInsert;
private ArrayAdapter<String> countryAdapter;
private ArrayAdapter<String> fruitsAdapter;
private ArrayAdapter<String> listViewAdapter;
private ArrayList<String> countryList;
private ArrayList<String> fruitsList;
private ArrayList<String> listViewItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initialize views
spCountry = findViewById(R.id.spCountry);
spFruits = findViewById(R.id.spFruits);
lvFruits = findViewById(R.id.lvFruits);
edtFruit = findViewById(R.id.edtFruit);
btnSubmit = findViewById(R.id.btnSubmit);
btnInsert = findViewById(R.id.btnInsert);
// Initialize data
initializeData();
// Setup adapters
setupAdapters();
// Set listeners
setListeners();
}
private void initializeData() {
countryList = new ArrayList<>();
countryList.add("Malaysia");
countryList.add("United States");
countryList.add("Indonesia");
countryList.add("France");
fruitsList = new ArrayList<>();
fruitsList.add("Coconut");
fruitsList.add("Durian");
fruitsList.add("Guava");
fruitsList.add("Kiwifruit");
fruitsList.add("Jackfruit");
fruitsList.add("Mango");
listViewItems = new ArrayList<>();
}
private void setupAdapters() {
countryAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item, countryList);
countryAdapter.setDropDownViewResource(android.R.layout.simple_spinn
er_dropdown_item);
spCountry.setAdapter(countryAdapter);
fruitsAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item, fruitsList);
fruitsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_
dropdown_item);
spFruits.setAdapter(fruitsAdapter);
listViewAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, listViewItems);
lvFruits.setAdapter(listViewAdapter);
}
private void setListeners() {
// Spinner item selection listeners
spCountry.setOnItemSelectedListener(new
AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int
position, long id) {
String selectedItem =
parent.getItemAtPosition(position).toString();
Toast.makeText(MainActivity.this, "Selected country: " +
selectedItem, Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spFruits.setOnItemSelectedListener(new
AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int
position, long id) {
String selectedItem =
parent.getItemAtPosition(position).toString();
Toast.makeText(MainActivity.this, "Selected fruit: " +
selectedItem, Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
// ListView item click listener
lvFruits.setOnItemClickListener(new
AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int
position, long id) {
String selectedItem =
parent.getItemAtPosition(position).toString();
Toast.makeText(MainActivity.this, "Selected: " + selectedItem,
Toast.LENGTH_SHORT).show();
}
});
// ListView long click listener (for deletion)
lvFruits.setOnItemLongClickListener(new
AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View
view, int position, long id) {
listViewItems.remove(position);
listViewAdapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "Item removed",
Toast.LENGTH_SHORT).show();
return true;
}
});
// Submit button click listener
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String country = spCountry.getSelectedItem().toString();
String fruit = edtFruit.getText().toString().trim();
if (!fruit.isEmpty()) {
String newItem = country + " - " + fruit;
fruitsList.add(newItem);
fruitsAdapter.notifyDataSetChanged();
edtFruit.setText("");
Toast.makeText(MainActivity.this, "Added to fruits list: " +
newItem, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "Please enter a fruit name",
Toast.LENGTH_SHORT).show();
}
}
});
// Insert button click listener
btnInsert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String country = spCountry.getSelectedItem().toString();
String fruit = spFruits.getSelectedItem().toString();
String newItem = country + " - " + fruit;
listViewItems.add(newItem);
listViewAdapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "Added to list: " + newItem,
Toast.LENGTH_SHORT).show();
}
});
}
}