TASK Simple Add and Fetch Data
HOMEPAGE.class:
ProductPage.class
EmployeePage.class
BooksPage.class
MODEL:
Product.class, Employee.class, Books.class, University.class, and Restaurant.class .
use string builder
use your previous activityActivity: Master Java List Data Structure (hashnode.space)
Send a Github Repository only on our portal
Example:
package com.thirdygayares.androidstringbuilder;
public class Product {
private int id;
private String name;
private String category;
private int price;
public Product() {
}
public Product(int id, String name, String category, int price) {
this.id = id;
this.name = name;
this.category = category;
this.price = price;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
}
package com.thirdygayares.androidstringbuilder;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
EditText editTextId, editTextName, editTextCategory, editTextPrice;
TextView textViewResults;
Button buttonAddProduct, buttonGetProduct;
List<Product> productList = new ArrayList<>();
Product productObject;
StringBuilder stringBuilder;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
stringBuilder = new StringBuilder();
productObject = new Product();
editTextId= findViewById(R.id.editTextId);
editTextName= findViewById(R.id.editTextName);
editTextCategory= findViewById(R.id.editTextCategory);
editTextPrice= findViewById(R.id.editTextPrice);
textViewResults= findViewById(R.id.textViewResults);
buttonAddProduct= findViewById(R.id.buttonAddProduct);
buttonGetProduct= findViewById(R.id.buttonGetProducts);
//listener for buttonAddProduct
buttonAddProduct.setOnClickListener(v-> addFunction());
}
//TODO add FUNCTION
private void addFunction(){
productObject.setId(Integer.parseInt(editTextId.getText().toString()));
productObject.setName(editTextName.getText().toString());
productObject.setCategory(editTextCategory.getText().toString());
productObject.setPrice(Integer.parseInt(editTextPrice.getText().toString()));
Log.d("MAIN", "id" + productObject.getId() + " name: " + productObject.getName() + " category: " + productObject.getCategory() + "PRICE:" + productObject.getPrice()) ;
stringBuilder.append("\n\n Name:" + productObject.getName());
stringBuilder.append("\n id:" + productObject.getId());
stringBuilder.append("\n category:" + productObject.getCategory());
stringBuilder.append("\n price:" + productObject.getPrice());
textViewResults.setText(stringBuilder.toString());
}
}
<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:layout_marginTop="50dp"
android:id="@+id/editTextId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Product ID"
android:inputType="number" />
<EditText
android:id="@+id/editTextName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Product Name"
android:inputType="text" />
<EditText
android:id="@+id/editTextCategory"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Product Category"
android:inputType="text" />
<EditText
android:id="@+id/editTextPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Product Price"
android:inputType="numberDecimal" />
<Button
android:id="@+id/buttonAddProduct"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add Product" />
<Button
android:id="@+id/buttonGetProducts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Get Products" />
<TextView
android:id="@+id/textViewResults"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Results will be displayed here"
android:textSize="16sp"
android:textColor="#000000"
android:paddingTop="20dp" />
</LinearLayout>