diff --git a/app/.gitignore b/app/.gitignore new file mode 100644 index 0000000..42afabf --- /dev/null +++ b/app/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/app/build.gradle.kts b/app/build.gradle.kts new file mode 100644 index 0000000..6b0a8db --- /dev/null +++ b/app/build.gradle.kts @@ -0,0 +1,43 @@ +plugins { + alias(libs.plugins.android.application) +} + +android { + namespace = "com.adeptofstroggus.laba2_2" + compileSdk = 36 + + defaultConfig { + applicationId = "com.adeptofstroggus.laba2_2" + minSdk = 31 + targetSdk = 36 + versionCode = 1 + versionName = "1.0" + + testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" + } + + buildTypes { + release { + isMinifyEnabled = false + proguardFiles( + getDefaultProguardFile("proguard-android-optimize.txt"), + "proguard-rules.pro" + ) + } + } + compileOptions { + sourceCompatibility = JavaVersion.VERSION_11 + targetCompatibility = JavaVersion.VERSION_11 + } +} + +dependencies { + + implementation(libs.appcompat) + implementation(libs.material) + implementation(libs.activity) + implementation(libs.constraintlayout) + testImplementation(libs.junit) + androidTestImplementation(libs.ext.junit) + androidTestImplementation(libs.espresso.core) +} \ No newline at end of file diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro new file mode 100644 index 0000000..481bb43 --- /dev/null +++ b/app/proguard-rules.pro @@ -0,0 +1,21 @@ +# Add project specific ProGuard rules here. +# You can control the set of applied configuration files using the +# proguardFiles setting in build.gradle. +# +# For more details, see +# http://developer.android.com/guide/developing/tools/proguard.html + +# If your project uses WebView with JS, uncomment the following +# and specify the fully qualified class name to the JavaScript interface +# class: +#-keepclassmembers class fqcn.of.javascript.interface.for.webview { +# public *; +#} + +# Uncomment this to preserve the line number information for +# debugging stack traces. +#-keepattributes SourceFile,LineNumberTable + +# If you keep the line number information, uncomment this to +# hide the original source file name. +#-renamesourcefileattribute SourceFile \ No newline at end of file diff --git a/app/src/androidTest/java/com/adeptofstroggus/laba2_2/ExampleInstrumentedTest.java b/app/src/androidTest/java/com/adeptofstroggus/laba2_2/ExampleInstrumentedTest.java new file mode 100644 index 0000000..2a35905 --- /dev/null +++ b/app/src/androidTest/java/com/adeptofstroggus/laba2_2/ExampleInstrumentedTest.java @@ -0,0 +1,26 @@ +package com.adeptofstroggus.laba2_2; + +import android.content.Context; + +import androidx.test.platform.app.InstrumentationRegistry; +import androidx.test.ext.junit.runners.AndroidJUnit4; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.*; + +/** + * Instrumented test, which will execute on an Android device. + * + * @see Testing documentation + */ +@RunWith(AndroidJUnit4.class) +public class ExampleInstrumentedTest { + @Test + public void useAppContext() { + // Context of the app under test. + Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); + assertEquals("com.adeptofstroggus.laba2_2", appContext.getPackageName()); + } +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml new file mode 100644 index 0000000..e2d8658 --- /dev/null +++ b/app/src/main/AndroidManifest.xml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/adeptofstroggus/laba2_2/Adapter.java b/app/src/main/java/com/adeptofstroggus/laba2_2/Adapter.java new file mode 100644 index 0000000..f719a66 --- /dev/null +++ b/app/src/main/java/com/adeptofstroggus/laba2_2/Adapter.java @@ -0,0 +1,45 @@ +package com.adeptofstroggus.laba2_2; + +import android.os.Debug; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +import androidx.annotation.NonNull; +import androidx.recyclerview.widget.RecyclerView; + +import java.util.ArrayList; +import java.util.List; + +public class Adapter extends RecyclerView.Adapter { + public List itemsList; + + public Adapter() { + this.itemsList = new ArrayList<>(); + } + + public void addItem(Item item){ + itemsList.add(item); + } + + + @NonNull + @Override + public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { + View itemView = LayoutInflater.from(parent.getContext()) + .inflate(R.layout.item_layout, parent, false); + return new ViewHolder(itemView); + } + + @Override + public void onBindViewHolder(ViewHolder holder, int position) { + Item currentItem = itemsList.get(position); + holder.bind(currentItem); + } + + @Override + public int getItemCount() { + return itemsList.size(); + } +} + diff --git a/app/src/main/java/com/adeptofstroggus/laba2_2/Item.java b/app/src/main/java/com/adeptofstroggus/laba2_2/Item.java new file mode 100644 index 0000000..69f50f2 --- /dev/null +++ b/app/src/main/java/com/adeptofstroggus/laba2_2/Item.java @@ -0,0 +1,20 @@ +package com.adeptofstroggus.laba2_2; + +public class Item { + public String name; + private Integer value; + + public void incValue(){ + value += 1; + } + public void decValue(){ + value -= 1; + } + public Integer getValue(){ + return value; + } + Item(String name, Integer value){ + this.name = name; + this.value = value; + } +} diff --git a/app/src/main/java/com/adeptofstroggus/laba2_2/MainActivity.java b/app/src/main/java/com/adeptofstroggus/laba2_2/MainActivity.java new file mode 100644 index 0000000..1991ca5 --- /dev/null +++ b/app/src/main/java/com/adeptofstroggus/laba2_2/MainActivity.java @@ -0,0 +1,95 @@ +package com.adeptofstroggus.laba2_2; + +import android.app.AlertDialog; +import android.os.Bundle; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; + +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 androidx.recyclerview.widget.LinearLayoutManager; +import androidx.recyclerview.widget.RecyclerView; + +import java.util.ArrayList; +import java.util.List; + +public class MainActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + EdgeToEdge.enable(this); + setContentView(R.layout.activity_main); + ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> { + Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars()); + v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom); + return insets; + }); + + Item item1_debug = new Item("Пакета травы", 2); + Item item2_debug = new Item("Ампулы мескалина", 75); + Item item3_debug = new Item("Упаковка кислоты", 5); + + Adapter adapter = new Adapter(); + adapter.addItem(item1_debug); + adapter.addItem(item2_debug); + adapter.addItem(item3_debug); + adapter.notifyDataSetChanged(); + RecyclerView recyclerView = findViewById(R.id.recyclerView); + + recyclerView.setAdapter(adapter); + recyclerView.setLayoutManager(new LinearLayoutManager(this)); + + Button addButton = findViewById(R.id.addButton); + addButton.setOnClickListener(v -> showAddDialog(adapter.itemsList, adapter)); + } + + private void showAddDialog(List itemsList, Adapter adapter) { + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setTitle("Добавить элемент"); + + // Создаем layout для ввода данных + View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_add_item, null); + EditText nameEditText = dialogView.findViewById(R.id.nameInput); + EditText numberEditText = dialogView.findViewById(R.id.numberInput); + + builder.setView(dialogView); + builder.setPositiveButton("Добавить", (dialog, which) -> { + String name; + int number; + + try { + name = nameEditText.getText().toString(); + if(name == "") { + name = "Безымянный элемент"; + } + } + catch (Exception e) { + name = "Безымянный"; + } + + try { + number = Integer.parseInt(numberEditText.getText().toString()); + } + catch (Exception e) { + number = 0; + } + + + + Item newItem = new Item(name, number); + itemsList.add(newItem); + adapter.notifyItemInserted(itemsList.size() - 1); + }); + + + + builder.setNegativeButton("Отмена", (dialog, which) -> dialog.dismiss()); + builder.show(); + } +} \ No newline at end of file diff --git a/app/src/main/java/com/adeptofstroggus/laba2_2/ViewHolder.java b/app/src/main/java/com/adeptofstroggus/laba2_2/ViewHolder.java new file mode 100644 index 0000000..b0442fc --- /dev/null +++ b/app/src/main/java/com/adeptofstroggus/laba2_2/ViewHolder.java @@ -0,0 +1,46 @@ +package com.adeptofstroggus.laba2_2; + +import android.view.View; +import android.widget.Button; +import android.widget.TextView; + +import androidx.recyclerview.widget.RecyclerView; + +public class ViewHolder extends RecyclerView.ViewHolder { + TextView nameTextView; + TextView numberTextView; + Button plusButton; + Button minusButton; + + private void UpdateText(Item item){ + numberTextView.setText(item.getValue().toString()); + } + public ViewHolder(View itemView) { + super(itemView); + nameTextView = itemView.findViewById(R.id.name); + numberTextView = itemView.findViewById(R.id.number); + plusButton = itemView.findViewById(R.id.plusButton); + minusButton = itemView.findViewById(R.id.minusButton); + } + + public void bind(Item item) { + nameTextView.setText(item.name); + numberTextView.setText(String.valueOf(item.getValue())); + // Обработка событий кнопок + plusButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + item.incValue(); + UpdateText(item); + } + }); + + minusButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + item.decValue(); + UpdateText(item); + } + }); + } +} diff --git a/app/src/main/res/drawable/ic_launcher_background.xml b/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 0000000..07d5da9 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/drawable/ic_launcher_foreground.xml b/app/src/main/res/drawable/ic_launcher_foreground.xml new file mode 100644 index 0000000..2b068d1 --- /dev/null +++ b/app/src/main/res/drawable/ic_launcher_foreground.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml new file mode 100644 index 0000000..1e4ba92 --- /dev/null +++ b/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,32 @@ + + + + + +