MainActivity.java 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. package com.example.zhulinaapi;
  2. import android.content.Intent;
  3. import android.os.AsyncTask;
  4. import android.os.Bundle;
  5. import android.text.Editable;
  6. import android.text.TextWatcher;
  7. import android.view.View;
  8. import android.widget.AdapterView;
  9. import android.widget.Button;
  10. import android.widget.EditText;
  11. import android.widget.ListView;
  12. import android.widget.Spinner;
  13. import androidx.appcompat.app.AppCompatActivity;
  14. import org.json.JSONArray;
  15. import org.json.JSONObject;
  16. import java.io.BufferedReader;
  17. import java.io.InputStreamReader;
  18. import java.net.HttpURLConnection;
  19. import java.net.URL;
  20. import java.util.ArrayList;
  21. import java.util.Collections;
  22. import java.util.List;
  23. import java.util.concurrent.TimeUnit;
  24. public class MainActivity extends AppCompatActivity {
  25. Spinner spinner;
  26. EditText FindTreiner;
  27. ListView listView;
  28. List<Mask> lvdays;
  29. AdapterMask pAdapter;
  30. @Override
  31. protected void onCreate(Bundle savedInstanceState) {
  32. super.onCreate(savedInstanceState);
  33. setContentView(R.layout.activity_main);
  34. Button btnAdd = findViewById(R.id.btnAdd);
  35. btnAdd.setOnClickListener((view -> startActivity(new Intent(
  36. MainActivity.this, AddData.class))));
  37. FindTreiner = findViewById(R.id.FindTreiner);
  38. FindTreiner.addTextChangedListener(new TextWatcher() {
  39. @Override
  40. public void afterTextChanged(Editable editable) {
  41. }
  42. @Override
  43. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  44. }
  45. @Override
  46. public void onTextChanged(CharSequence s, int start, int before, int count) {
  47. sort(spinner.getSelectedItemPosition());
  48. }
  49. });
  50. listView = findViewById(R.id.lvData);
  51. lvdays = new ArrayList<>();
  52. pAdapter = new AdapterMask(MainActivity.this, lvdays);
  53. listView.setAdapter(pAdapter);
  54. listView.setOnItemClickListener((arg0, arg1, position, arg3) -> {
  55. Intent intent = new Intent(MainActivity.this, Change.class);
  56. intent.putExtra("id", Integer.parseInt(String.valueOf(arg3)));
  57. intent.putExtra("day", lvdays.get(position).getday());
  58. intent.putExtra("wotkout", lvdays.get(position).getwotkout());
  59. intent.putExtra("trainer", lvdays.get(position).gettrainer());
  60. intent.putExtra("image", lvdays.get(position).getImage());
  61. startActivity(intent);
  62. });
  63. spinner = findViewById(R.id.spinner);
  64. spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
  65. @Override
  66. public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
  67. sort(position);
  68. }
  69. @Override
  70. public void onNothingSelected(AdapterView<?> parentView) {
  71. return;
  72. }
  73. });
  74. new Getdays().execute();
  75. try {
  76. TimeUnit.MILLISECONDS.sleep(200); // Без этого запускается пустой
  77. } catch (InterruptedException e) {
  78. e.printStackTrace();
  79. }
  80. }
  81. private void sort(int position) {
  82. List<Mask> list = new ArrayList<>();
  83. if (FindTreiner.getText().equals(null)) {
  84. list.addAll(lvdays);
  85. } else {
  86. for (Mask item : lvdays) {
  87. if (item.gettrainer().contains(FindTreiner.getText())) {
  88. list.add(item);
  89. }
  90. }
  91. }
  92. switch (position) {
  93. case 1:
  94. SortByday sp = new SortByday();
  95. Collections.sort(list, sp);
  96. break;
  97. case 2:
  98. SortBywotkout sq = new SortBywotkout();
  99. Collections.sort(list, sq);
  100. break;
  101. case 3:
  102. SortBytrainer sc = new SortBytrainer();
  103. Collections.sort(list, sc);
  104. break;
  105. }
  106. pAdapter = new AdapterMask(MainActivity.this, list);
  107. listView.setAdapter(pAdapter);
  108. }
  109. private class Getdays extends AsyncTask<Void, Void, String> {
  110. @Override
  111. protected String doInBackground(Void... voids) {
  112. try {
  113. URL url = new URL("https://ngknn.ru:5001/NGKNN/ЖулинаАА/api/Workouts");
  114. HttpURLConnection connection = (HttpURLConnection) url.openConnection();
  115. BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
  116. StringBuilder result = new StringBuilder();
  117. String line = "";
  118. while ((line = reader.readLine()) != null) {
  119. result.append(line);
  120. }
  121. return result.toString();
  122. } catch (Exception exception) {
  123. return null;
  124. }
  125. }
  126. @Override
  127. protected void onPostExecute(String s) {
  128. super.onPostExecute(s);
  129. try {
  130. JSONArray tempArray = new JSONArray(s);
  131. for (int i = 0; i < tempArray.length(); i++) {
  132. JSONObject productJson = tempArray.getJSONObject(i);
  133. Mask tempday = new Mask(
  134. productJson.getInt("id"),
  135. productJson.getString("day"),
  136. productJson.getString("wotkout"),
  137. productJson.getString("trainer"),
  138. productJson.getString("image")
  139. );
  140. lvdays.add(tempday);
  141. pAdapter.notifyDataSetInvalidated();
  142. }
  143. } catch (Exception ignored) {
  144. }
  145. }
  146. }
  147. }