-
Notifications
You must be signed in to change notification settings - Fork 0
Homework7 #7
base: master
Are you sure you want to change the base?
Homework7 #7
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| /build |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| apply plugin: 'com.android.application' | ||
|
|
||
| android { | ||
| compileSdkVersion 30 | ||
| buildToolsVersion "30.0.2" | ||
|
|
||
| defaultConfig { | ||
| applicationId "com.example.homework7" | ||
| minSdkVersion 21 | ||
| targetSdkVersion 30 | ||
| versionCode 1 | ||
| versionName "1.0" | ||
|
|
||
| testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" | ||
| } | ||
|
|
||
| buildTypes { | ||
| release { | ||
| minifyEnabled false | ||
| proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' | ||
| } | ||
| } | ||
| } | ||
|
|
||
| dependencies { | ||
| implementation fileTree(dir: "libs", include: ["*.jar"]) | ||
| implementation 'androidx.appcompat:appcompat:1.2.0' | ||
| implementation 'androidx.constraintlayout:constraintlayout:2.0.4' | ||
| implementation 'androidx.recyclerview:recyclerview:1.1.0' | ||
| testImplementation 'junit:junit:4.12' | ||
| androidTestImplementation 'androidx.test.ext:junit:1.1.2' | ||
| androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' | ||
| implementation "android.arch.persistence.room:runtime:1.1.1" | ||
| annotationProcessor "android.arch.persistence.room:compiler:1.1.1" | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package com.example.homework7; | ||
|
|
||
| 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 <a href="http://d.android.com/tools/testing">Testing documentation</a> | ||
| */ | ||
| @RunWith(AndroidJUnit4.class) | ||
| public class ExampleInstrumentedTest { | ||
| @Test | ||
| public void useAppContext() { | ||
| // Context of the app under test. | ||
| Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); | ||
| assertEquals("com.example.homework7", appContext.getPackageName()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
| package="com.example.homework7"> | ||
|
|
||
| <application | ||
| android:allowBackup="true" | ||
| android:icon="@mipmap/ic_launcher" | ||
| android:label="@string/app_name" | ||
| android:roundIcon="@mipmap/ic_launcher_round" | ||
| android:supportsRtl="true" | ||
| android:theme="@style/AppTheme"> | ||
| <activity android:name=".AddContact"/> | ||
| <activity android:name=".EditContact"/> | ||
| <activity android:name=".MainActivity"> | ||
| <intent-filter> | ||
| <action android:name="android.intent.action.MAIN" /> | ||
|
|
||
| <category android:name="android.intent.category.LAUNCHER" /> | ||
| </intent-filter> | ||
| </activity> | ||
| </application> | ||
|
|
||
| </manifest> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package com.example.homework7; | ||
|
|
||
| import android.content.Intent; | ||
| import android.content.SharedPreferences; | ||
| import android.os.Bundle; | ||
| import android.view.View; | ||
| import android.widget.CompoundButton; | ||
| import android.widget.EditText; | ||
| import android.widget.Switch; | ||
| import android.widget.Toast; | ||
|
|
||
| import androidx.appcompat.app.AppCompatActivity; | ||
|
|
||
| public class AddContact extends AppCompatActivity { | ||
| private String name; | ||
| private String phone; | ||
| private Switch switch1; | ||
| private boolean ifChecked; | ||
| private AppDatabase db; | ||
| private int counter; | ||
| private SharedPreferences sPref; | ||
|
|
||
|
|
||
| @Override | ||
| protected void onCreate(Bundle savedInstanceState) { | ||
| super.onCreate(savedInstanceState); | ||
| setContentView(R.layout.contact_add); | ||
|
|
||
| final EditText editText1 = findViewById(R.id.addName); | ||
| final EditText editText2 = findViewById(R.id.addPhone); | ||
| db = AppDatabase.getInstance(this); | ||
|
|
||
| switch1=findViewById(R.id.switcher1); | ||
| switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { | ||
|
|
||
| @Override | ||
| public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { | ||
| if (isChecked == true) { | ||
| editText2.setHint(R.string.email); | ||
| Toast.makeText(AddContact.this, R.string.modeInputEmail, Toast.LENGTH_SHORT).show(); | ||
| } else if (isChecked== false){ | ||
| editText2.setHint(R.string.phoneNumber); | ||
| Toast.makeText(AddContact.this, R.string.modeInputPhone, Toast.LENGTH_SHORT).show(); | ||
| } | ||
| ifChecked = isChecked; | ||
| } | ||
| }); | ||
|
|
||
| findViewById(R.id.buttonAdd).setOnClickListener(new View.OnClickListener() { | ||
| @Override | ||
| public void onClick(View v) { | ||
| Intent intentSave = new Intent(AddContact.this, MainActivity.class); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. по сути интент тут вообще не нужен |
||
| name = editText1.getText().toString(); | ||
| phone = editText2.getText().toString(); | ||
| loadCounter(); | ||
| db.contactDao().insert(new Item(counter, name, phone)); | ||
| Toast.makeText(AddContact.this, R.string.addNewContact, Toast.LENGTH_SHORT).show(); | ||
| counter ++; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Если использовать автоинкремент, то эти операции сами собой отпадают |
||
| saveCounter(counter); | ||
| startActivity(intentSave); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Так а вот это тут лишнее. Не вижу смысла запускать новую активити. тебе по сути надо просто финишировать текущую |
||
| finish(); | ||
| } | ||
| }); | ||
| } | ||
| void saveCounter(int counter) { | ||
| sPref = getPreferences(MODE_PRIVATE); | ||
| SharedPreferences.Editor ed = sPref.edit(); | ||
| ed.putInt("COUNTER", counter); | ||
| ed.apply(); | ||
| } | ||
|
|
||
| public int loadCounter() { | ||
| sPref = getPreferences(MODE_PRIVATE); | ||
| this.counter = sPref.getInt("COUNTER", 0); | ||
| return counter; | ||
| } | ||
|
Comment on lines
+65
to
+76
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Если использовать автоинкремент, то эти операции сами собой отпадают |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package com.example.homework7; | ||
|
|
||
| import android.content.Context; | ||
|
|
||
| import androidx.room.Database; | ||
| import androidx.room.Room; | ||
| import androidx.room.RoomDatabase; | ||
|
|
||
| @Database(entities = {Item.class}, version = 1, exportSchema = false) | ||
| public abstract class AppDatabase extends RoomDatabase { | ||
| private static AppDatabase database; | ||
|
|
||
| public abstract ContactDao contactDao(); | ||
|
|
||
| public synchronized static AppDatabase getInstance(Context context) { | ||
| if (database == null) { | ||
| database = Room.databaseBuilder(context.getApplicationContext(), | ||
| AppDatabase.class, "database") | ||
| .allowMainThreadQueries() | ||
| .fallbackToDestructiveMigration() | ||
| .build(); | ||
| } | ||
| return database; | ||
|
|
||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package com.example.homework7; | ||
|
|
||
| import androidx.room.Dao; | ||
| import androidx.room.Delete; | ||
| import androidx.room.Insert; | ||
| import androidx.room.Query; | ||
| import androidx.room.Update; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| @Dao | ||
| public interface ContactDao { | ||
|
|
||
| @Query("SELECT * FROM item") | ||
| List<Item> getAll(); | ||
|
|
||
| @Query("SELECT * FROM item WHERE id = :id") | ||
| Item getById(long id); | ||
|
|
||
| @Insert | ||
| void insert(Item item); | ||
|
|
||
| @Update | ||
| void update(Item item); | ||
|
|
||
| @Delete | ||
| void delete(Item item); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package com.example.homework7; | ||
|
|
||
| import android.content.Intent; | ||
| import android.os.Bundle; | ||
| import android.view.View; | ||
| import android.widget.CompoundButton; | ||
| import android.widget.EditText; | ||
| import android.widget.Switch; | ||
| import android.widget.Toast; | ||
|
|
||
| import androidx.appcompat.app.AppCompatActivity; | ||
|
|
||
| public class EditContact extends AppCompatActivity { | ||
| private Switch switch2; | ||
| private boolean ifChecked; | ||
| private String name; | ||
| private String data; | ||
| private int position; | ||
| private AppDatabase db; | ||
|
|
||
| @Override | ||
| protected void onCreate(Bundle savedInstanceState) { | ||
| super.onCreate(savedInstanceState); | ||
| setContentView(R.layout.activity_edit_contact); | ||
|
|
||
| final EditText editName = findViewById(R.id.editName); | ||
| final EditText editData = findViewById(R.id.editPhone); | ||
| Intent intentPosition = getIntent(); | ||
| position = intentPosition.getIntExtra("POSITION", 0); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. лучше передавать сюда не позицию, а id твоего контакта. Так более правильно, потому что ты по нему потом сможешь получить контакт из базы |
||
| name = intentPosition.getStringExtra("NAME"); | ||
| data = intentPosition.getStringExtra("DATA"); | ||
| editName.setHint(name); | ||
| editData.setHint(data); | ||
| db = AppDatabase.getInstance(this); | ||
|
|
||
| switch2 = findViewById(R.id.switcher2); | ||
| switch2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { | ||
|
|
||
| @Override | ||
| public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { | ||
| if (isChecked == true) { | ||
| editData.setHint(R.string.email); | ||
| Toast.makeText(EditContact.this, R.string.modeInputEmail, Toast.LENGTH_SHORT).show(); | ||
| } else if (isChecked == false) { | ||
| editData.setHint(R.string.phoneNumber); | ||
| Toast.makeText(EditContact.this, R.string.modeInputPhone, Toast.LENGTH_SHORT).show(); | ||
| } | ||
| ifChecked = isChecked; | ||
| } | ||
| }); | ||
|
|
||
| findViewById(R.id.buttonSave).setOnClickListener(new View.OnClickListener() { | ||
| @Override | ||
| public void onClick(View v) { | ||
| Intent intentSaved = new Intent(EditContact.this, MainActivity.class); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. по сути интент тут вообще не нужен |
||
| name = editName.getText().toString(); | ||
| data = editData.getText().toString(); | ||
| db.contactDao().update(new Item(position, name, data)); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Лично я бы загружал бы контакт из базы в onCreate по id, потом тут обновлял бы его, и передавал бы в базу |
||
| Toast.makeText(EditContact.this, R.string.editContact, Toast.LENGTH_SHORT).show(); | ||
| startActivity(intentSaved); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Так а вот это тут лишнее. Не вижу смысла запускать новую активити. тебе по сути надо просто финишировать текущую |
||
| finish(); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package com.example.homework7; | ||
|
|
||
| import androidx.room.Entity; | ||
| import androidx.room.PrimaryKey; | ||
|
|
||
| @Entity | ||
| public class Item { | ||
| @PrimaryKey | ||
| public int id; | ||
| private String name; | ||
| private String data; | ||
|
|
||
| public String getName() { | ||
| return name; | ||
| } | ||
|
|
||
| public String getData() { | ||
| return data; | ||
| } | ||
|
|
||
| public int getId() { | ||
| return id; | ||
| } | ||
|
|
||
| public Item(int id, String name, String data) { | ||
| this.id = id; | ||
| this.name = name; | ||
| this.data = data; | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ты можешь отказаться от каунтера и просто сделать автоинкрементное поле для id или генерить слачайную строку
https://www.baeldung.com/kotlin-random-alphanumeric-string