diff --git a/.idea/androidTestResultsUserPreferences.xml b/.idea/androidTestResultsUserPreferences.xml new file mode 100644 index 00000000..e85ced4e --- /dev/null +++ b/.idea/androidTestResultsUserPreferences.xml @@ -0,0 +1,65 @@ + + + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml index fb7f4a8a..b589d56e 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -1,6 +1,6 @@ - + \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml index 4e3844e8..993336a6 100644 --- a/.idea/gradle.xml +++ b/.idea/gradle.xml @@ -6,13 +6,7 @@ - diff --git a/.idea/misc.xml b/.idea/misc.xml index e10eeb4c..026a88f5 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,4 +1,10 @@ +<<<<<<< HEAD + + + + +======= - + \ No newline at end of file diff --git a/CityList b/CityList new file mode 160000 index 00000000..88d855dc --- /dev/null +++ b/CityList @@ -0,0 +1 @@ +Subproject commit 88d855dc67688a00ac925f63f52c9f06524a4802 diff --git a/app/build.gradle b/app/build.gradle index f6c5007c..8904b57c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -25,6 +25,7 @@ android { sourceCompatibility JavaVersion.VERSION_1_8 targetCompatibility JavaVersion.VERSION_1_8 } + namespace 'com.example.citylist' } dependencies { @@ -35,4 +36,6 @@ dependencies { testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' + androidTestImplementation 'androidx.test:runner:1.4.0' + androidTestImplementation 'androidx.test:rules:1.4.0' } \ No newline at end of file diff --git a/app/src/androidTest/java/com/example/citylist/MainActivityTest.java b/app/src/androidTest/java/com/example/citylist/MainActivityTest.java new file mode 100644 index 00000000..53876461 --- /dev/null +++ b/app/src/androidTest/java/com/example/citylist/MainActivityTest.java @@ -0,0 +1,113 @@ +package com.example.citylist; + +import static androidx.test.espresso.Espresso.onData; +import static androidx.test.espresso.action.ViewActions.click; +import static androidx.test.espresso.assertion.ViewAssertions.doesNotExist; +import static androidx.test.espresso.assertion.ViewAssertions.matches; +import static androidx.test.espresso.matcher.ViewMatchers.isDisplayed; +import static androidx.test.espresso.matcher.ViewMatchers.withId; +import static androidx.test.espresso.matcher.ViewMatchers.withText; +import static androidx.test.espresso.Espresso.onView; + +import static org.hamcrest.CoreMatchers.anything; + +import androidx.test.espresso.Espresso; +import androidx.test.espresso.action.ViewActions; +import androidx.test.ext.junit.rules.ActivityScenarioRule; +import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.filters.LargeTest; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.runner.RunWith; + +@RunWith(AndroidJUnit4.class) +@LargeTest +public class MainActivityTest { + + @Rule + public ActivityScenarioRule activityRule = + new ActivityScenarioRule<>(MainActivity.class); + + @Test + public void testAppName() { + onView(withText("CityList")).check(matches(isDisplayed())); //Check the name on the screen + } + + @Test + public void testAddCity(){ + onView(withId(R.id.button_add)).perform(click()); //Click add button to add a city to the list + onView(withId(R.id.editText_name)).perform(ViewActions.typeText("Edmonton")); //Type a city name + onView(withId(R.id.button_confirm)).perform(click()); //Confirm the city name and add to the list + onView(withText("Edmonton")).check(matches(isDisplayed())); //Check the name on the screen + } + + @Test + public void testClearCity(){ + onView(withId(R.id.button_add)).perform(click()); //Click add button to add a city to the list + onView(withId(R.id.editText_name)).perform(ViewActions.typeText("Edmonton")); //Type a city name + onView(withId(R.id.button_confirm)).perform(click()); //Confirm the city name and add to the list + //Add another city to the list + onView(withId(R.id.button_add)).perform(click()); //Click add button to add a city to the list + onView(withId(R.id.editText_name)).perform(ViewActions.typeText("Toronto")); //Type a city name + onView(withId(R.id.button_confirm)).perform(click()); //Confirm the city name and add to the list + //Clear the list + onView(withId(R.id.button_clear)).perform(click()); + onView(withText("Edmonton")).check(doesNotExist()); + + } + @Test + public void testListView(){ + onView(withId(R.id.button_add)).perform(click()); //Click add button to add a city to the list + onView(withId(R.id.editText_name)).perform(ViewActions.typeText("Edmonton")); //Type a city name + onView(withId(R.id.button_confirm)).perform(click()); //Confirm the city name and add to the list + + onData(anything()).inAdapterView(withId(R.id.city_list)).atPosition(0). + check(matches((withText("Edmonton")))); //Check the content on the list - no content in this case + } + + @Test + public void testListViewClickAndBack(){ + onView(withId(R.id.button_add)).perform(click()); //Click add button to add a city to the list + onView(withId(R.id.editText_name)).perform(ViewActions.typeText("Edmonton")); //Type a city name + onView(withId(R.id.button_confirm)).perform(click()); //Confirm the city name and add to the list + + onData(anything()).inAdapterView(withId(R.id.city_list)).atPosition(0).perform(click()); //Check the content on the list - no content in this case + Espresso.pressBack(); //Back button + } + + @Test + public void lab2test() + { + onView(withId(R.id.button_add)).perform(click()); //Click add button to add a city to the list + onView(withId(R.id.editText_name)).perform(ViewActions.typeText("Admonton")); //Type a city name + Espresso.pressBack(); + onView(withId(R.id.button_confirm)).perform(click()); //Confirm the city name and add to the list + + onView(withId(R.id.button_add)).perform(click()); //Click add button to add a city to the list + onView(withId(R.id.editText_name)).perform(ViewActions.typeText("Bdmonton")); //Type a city name + Espresso.pressBack(); + onView(withId(R.id.button_confirm)).perform(click()); //Confirm the city name and add to the list + + onView(withId(R.id.button_add)).perform(click()); //Click add button to add a city to the list + onView(withId(R.id.editText_name)).perform(ViewActions.typeText("Cdmonton")); //Type a city name + Espresso.pressBack(); + onView(withId(R.id.button_confirm)).perform(click()); //Confirm the city name and add to the list + + onView(withId(R.id.button_add)).perform(click()); //Click add button to add a city to the list + onView(withId(R.id.editText_name)).perform(ViewActions.typeText("Ddmonton")); //Type a city name + Espresso.pressBack(); + onView(withId(R.id.button_confirm)).perform(click()); //Confirm the city name and add to the list + + onData(anything()).inAdapterView(withId(R.id.city_list)).atPosition(3).perform(click()); + //Second activity display hoise kina check koro + onView(withId(R.id.second)).check(matches(isDisplayed())); + //main activity er click kora city secondactivity er text view te gese kina + onView(withText("Ddmonton")).check(matches(isDisplayed())); + //back button e click krlam + onView(withId(R.id.button)).perform(click()); + //main activity te fire ashlam kina + onView(withId(R.id.main)).check(matches(isDisplayed())); + } + +} diff --git a/app/src/androidTest/java/com/example/citylist/ShowActivityTest.java b/app/src/androidTest/java/com/example/citylist/ShowActivityTest.java new file mode 100644 index 00000000..f3aa57a6 --- /dev/null +++ b/app/src/androidTest/java/com/example/citylist/ShowActivityTest.java @@ -0,0 +1,12 @@ +package com.example.citylist; + +import static org.junit.Assert.*; + +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) + +public class ShowActivityTest { + +} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index b15e6b3c..97a0eeca 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,6 +1,5 @@ - + + android:theme="@style/Theme.CityList"> + + android:exported="true"> diff --git a/app/src/main/java/com/example/citylist/City.java b/app/src/main/java/com/example/citylist/City.java index 4bec61ac..8615fe3b 100644 --- a/app/src/main/java/com/example/citylist/City.java +++ b/app/src/main/java/com/example/citylist/City.java @@ -1,6 +1,6 @@ package com.example.citylist; -public class City { +public class City implements Comparable{ private String city; private String province; @@ -16,4 +16,9 @@ String getCityName(){ String getProvinceName(){ return this.province; } -} + + @Override + public int compareTo(City city) { + return this.city.compareTo(city.getCityName()); + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/citylist/CityList.java b/app/src/main/java/com/example/citylist/CityList.java new file mode 100644 index 00000000..994e78e3 --- /dev/null +++ b/app/src/main/java/com/example/citylist/CityList.java @@ -0,0 +1,63 @@ +package com.example.citylist; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +/** + * This is a class that keeps track of a list of city objects + */ +public class CityList { + private List cities = new ArrayList<>(); + + /** + * This adds a city to the list if that city does not exist + * @param city + * This is the city to add + */ + public void add(City city) { + if (cities.contains(city)) { + throw new IllegalArgumentException(); + } + cities.add(city); + } + + /** + * This returns a sorted list of cities + * @return + * Return the sorted list of cities + */ + public List getCities(int parameter) { + List cityList = cities; + if(parameter == 0) + { + Collections.sort(cityList); + } + else + { + Collections.sort(cityList, new Comparator() { + @Override + public int compare(City city, City t1) { + return city.getProvinceName().compareTo(t1.getProvinceName()); + } + }); + } + return cityList; + } + + public void delete(City city) + { + if(!cities.contains(city)) + { + throw new IllegalArgumentException(); + } + cities.remove(city); + } + public int count() + { + return cities.size(); + } +} + + diff --git a/app/src/main/java/com/example/citylist/CustomList.java b/app/src/main/java/com/example/citylist/CustomList.java deleted file mode 100644 index 915c1bf8..00000000 --- a/app/src/main/java/com/example/citylist/CustomList.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.example.citylist; - -import android.content.Context; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ArrayAdapter; -import android.widget.TextView; - -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; - -import java.util.ArrayList; - -public class CustomList extends ArrayAdapter { - - private ArrayList cities; - private Context context; - - public CustomList(Context context, ArrayList cities){ - super(context, 0, cities); - this.cities = cities; - this.context = context; - } - @NonNull - @Override - public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent){ - View view = convertView; - - if(view == null){ - view = LayoutInflater.from(context).inflate(R.layout.content, parent, false); - } - - City city = cities.get(position); - - TextView cityName = view.findViewById(R.id.city_text); - TextView provinceName = view.findViewById(R.id.province_text); - - cityName.setText(city.getCityName()); - provinceName.setText(city.getProvinceName()); - - return view; - } -} diff --git a/app/src/main/java/com/example/citylist/MainActivity.java b/app/src/main/java/com/example/citylist/MainActivity.java index 898aa6ec..1d4f3ac5 100644 --- a/app/src/main/java/com/example/citylist/MainActivity.java +++ b/app/src/main/java/com/example/citylist/MainActivity.java @@ -1,37 +1,75 @@ package com.example.citylist; import androidx.appcompat.app.AppCompatActivity; + +import android.content.Intent; 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.LinearLayout; import android.widget.ListView; - import java.util.ArrayList; -import java.util.Arrays; public class MainActivity extends AppCompatActivity { // Declare the variables so that you will be able to reference it later. ListView cityList; - ArrayAdapter cityAdapter; - ArrayList cityDataList; + EditText newName; + LinearLayout nameField; + ArrayAdapter cityAdapter; + ArrayList dataList; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + nameField = findViewById(R.id.field_nameEntry); + newName = findViewById(R.id.editText_name); + cityList = findViewById(R.id.city_list); + dataList = new ArrayList<>(); + cityAdapter = new ArrayAdapter<>(this, R.layout.content, dataList); + cityList.setAdapter(cityAdapter); - String cities[] = {"Edmonton", "Vancouver", "Toronto", "Calgary", "Hamilton", "Waterloo" }; - String provinces[] = {"AB", "BC", "ON", "AB", "ON", "ON"}; + final Button addButton = findViewById(R.id.button_add); + addButton.setOnClickListener(new View.OnClickListener() { + public void onClick(View v) { + nameField.setVisibility(View.VISIBLE); + } + }); - cityDataList = new ArrayList<>(); + final Button confirmButton = findViewById(R.id.button_confirm); + confirmButton.setOnClickListener(new View.OnClickListener() { + public void onClick(View v) { + String cityName = newName.getText().toString(); + cityAdapter.add(cityName); + newName.getText().clear(); + nameField.setVisibility(View.INVISIBLE); + } + }); - for(int i=0; i adapterView, View view, int i, long l) { + Intent intent = new Intent(MainActivity.this, ShowActivity.class); + intent.putExtra("cityname", dataList.get(i)); + startActivity(intent); + } + }); - cityList.setAdapter(cityAdapter); } + + } \ No newline at end of file diff --git a/app/src/main/java/com/example/citylist/ShowActivity.java b/app/src/main/java/com/example/citylist/ShowActivity.java new file mode 100644 index 00000000..d236cb15 --- /dev/null +++ b/app/src/main/java/com/example/citylist/ShowActivity.java @@ -0,0 +1,33 @@ +package com.example.citylist; + +import androidx.appcompat.app.AppCompatActivity; + +import android.content.Intent; +import android.os.Bundle; +import android.view.View; +import android.widget.Button; +import android.widget.TextView; + +public class ShowActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_show); + Intent intent= getIntent(); + String s = intent.getStringExtra("cityname"); + TextView textView = findViewById(R.id.textView); + textView.setText(s); + + Button button=findViewById(R.id.button); + + button.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + Intent intent1 = new Intent(ShowActivity.this, MainActivity.class); + startActivity(intent1); + finish(); + } + }); + } +} \ 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 index 2af50fa9..0be43b84 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -2,16 +2,65 @@ + android:orientation="vertical" + tools:context=".MainActivity" + android:id="@+id/main"> + + + +