0% found this document useful (0 votes)
4 views6 pages

Android Product Management App

The document contains the implementation of a product management application in Android, featuring a main activity that allows users to add, edit, delete, and sort products. It includes a RecyclerView for displaying products, buttons for sorting by name and price, and a SearchView for filtering products. The ProductAdapter class manages the display of product items and handles user interactions through click listeners.

Uploaded by

huynhvunhunguyen
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)
4 views6 pages

Android Product Management App

The document contains the implementation of a product management application in Android, featuring a main activity that allows users to add, edit, delete, and sort products. It includes a RecyclerView for displaying products, buttons for sorting by name and price, and a SearchView for filtering products. The ProductAdapter class manages the display of product items and handles user interactions through click listeners.

Uploaded by

huynhvunhunguyen
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

MainActivity.

java

package [Link];

import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];

public class MainActivity extends AppCompatActivity {


private static final int EDIT_PRODUCT_REQUEST = 2;
private static final int ADD_PRODUCT_REQUEST = 1;
private RecyclerView recyclerView;
private ProductAdapter adapter;
private List<Product> productList;
private ImageButton addProductButton;
private Button sortByNameButton, sortByPriceButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].activity_main);
Window window = getWindow();

// Initialize views
sortByNameButton = findViewById([Link]);
sortByPriceButton = findViewById([Link]);
recyclerView = findViewById([Link]);
addProductButton = findViewById([Link]);
// Set navigation bar color
[Link](getResources().getColor([Link]));

// Initialize product list and adapter


productList = new ArrayList<>();
adapter = new ProductAdapter(productList, new
[Link]() {
@Override
public void onEditClick(int position) {
Intent intent = new Intent([Link], [Link]);
[Link]("product", [Link](position));
[Link]("position", position);
startActivityForResult(intent, EDIT_PRODUCT_REQUEST);
}

@Override
public void onDeleteClick(int position) {
new [Link]([Link])
.setTitle("Delete Product")
.setMessage("Are you sure you want to delete this product?")
.setPositiveButton([Link], (dialog, which) -> {
[Link](position);
[Link](position);
[Link](findViewById([Link]), "Product deleted
successfully", Snackbar.LENGTH_SHORT).show();
[Link]([Link], "Product deleted successfully",
Toast.LENGTH_SHORT).show();
})
.setNegativeButton([Link], null)
.show();
}
});

[Link](adapter);
[Link](new LinearLayoutManager(this));

// Add product button click


[Link](v -> {
Intent intent = new Intent([Link], [Link]);
startActivityForResult(intent, ADD_PRODUCT_REQUEST);
});
// Load products (implement this as needed)
loadProducts();

// Set up SearchView filter


SearchView searchView = findViewById([Link]);
[Link](new [Link]() {
@Override
public boolean onQueryTextSubmit(String query) {
filter(query);
return false;
}

@Override
public boolean onQueryTextChange(String newText) {
filter(newText);
return false;
}
});

// Sort by name button click


[Link](v -> {
[Link](productList, new Comparator<Product>() {
@Override
public int compare(Product p1, Product p2) {
return [Link]().compareToIgnoreCase([Link]());
}
});
[Link](productList); // Refresh list in adapter
});

// Sort by price button click


[Link](v -> {
[Link](productList, new Comparator<Product>() {
@Override
public int compare(Product p1, Product p2) {
return [Link]([Link](), [Link]());
}
});
[Link](productList); // Refresh list in adapter
});
}

private void filter(String text) {


List<Product> filteredList = new ArrayList<>();
for (Product product : productList) {
if ([Link]().toLowerCase().contains([Link]())) {
[Link](product);
}
}
[Link](filteredList); // Refresh list in adapter
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
[Link](requestCode, resultCode, data);
if (requestCode == ADD_PRODUCT_REQUEST && resultCode == RESULT_OK) {
Product newProduct = (Product) [Link]("product");
if (newProduct != null) {
[Link](newProduct);
[Link](productList); // Refresh list in adapter
}
} else if (requestCode == EDIT_PRODUCT_REQUEST && resultCode == RESULT_OK) {
Product updatedProduct = (Product) [Link]("updatedProduct");
int position = [Link]("position", -1);
if (position != -1 && updatedProduct != null) {
[Link](position, updatedProduct);
[Link](position);
}
}
}

private void loadProducts() {


// Load products from database or repository and update productList
// For example: productList = [Link]();
[Link](productList); // Refresh list in adapter
}
}

[Link]

public class ProductAdapter extends


[Link]<[Link]> {
private List<Product> productList;
private OnItemClickListener listener;
public ProductAdapter(List<Product> productList, OnItemClickListener listener) {
[Link] = productList;
[Link] = listener;
}

// Method to update the list and notify the adapter


public void updateList(List<Product> newList) {
[Link]();
[Link](newList);
notifyDataSetChanged();
}

// Other adapter methods (onCreateViewHolder, onBindViewHolder, getItemCount)

public interface OnItemClickListener {


void onEditClick(int position);
void onDeleteClick(int position);
}
}

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_below="@id/searchView"

android:orientation="horizontal"

android:gravity="center">

<Button

android:id="@+id/sortByNameButton"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Sort by Name" />


<Button

android:id="@+id/sortByPriceButton"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Sort by Price"

android:layout_marginStart="16dp" />

</LinearLayout>

You might also like