Advertisement
praymes

IceCream

Sep 11th, 2019
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.33 KB | None | 0 0
  1. -----MainActivity.java
  2.  
  3. public class MainActivity extends AppCompatActivity implements NotifyListener {
  4.  
  5. private String[] arrayOfIceCream;
  6. private ListView listToShow;
  7. Button button;
  8. private int min = 40;
  9. private int max = 100;
  10. private int t = 1;
  11. int rand;
  12. int rand2 = rand - 1;
  13.  
  14.  
  15.  
  16. //List Adapter with Ice Creams
  17. //SUM is added randomly
  18. //Thread
  19. //Button click = -1 sum
  20.  
  21. @Override
  22. protected void onCreate(Bundle savedInstanceState) {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.activity_main);
  25.  
  26. listToShow = findViewById(R.id.list_view);
  27. button = findViewById(R.id.button);
  28.  
  29. arrayOfIceCream = new String [] {"Strawberry", "Chocolate", "Banana", "Cookies", "Watermelon"};
  30. final ArrayList<IceCream> listOfIceCream = new ArrayList<>();
  31.  
  32. for(int index = 0; index < arrayOfIceCream.length; index++){
  33. Random r = new Random();
  34. rand = r.nextInt((max - min) + 1) + min;
  35. final IceCream nCream = new IceCream(arrayOfIceCream[index], rand);
  36. listOfIceCream.add(nCream);
  37. }
  38.  
  39. Collections.sort(listOfIceCream, new Comparator<IceCream>() {
  40. @Override
  41. public int compare(IceCream o1, IceCream o2) {
  42. return Integer.valueOf(o2.getSum()).compareTo(o1.getSum());
  43. }
  44. });
  45.  
  46. CustomAdapter myAdapter = new CustomAdapter(this, R.layout.custom_list_layout, listOfIceCream);
  47. listToShow.setAdapter(myAdapter);
  48.  
  49.  
  50. final Task task = new Task (MainActivity.this);
  51.  
  52. button.setOnClickListener(new View.OnClickListener() {
  53. @Override
  54. public void onClick(View view) {
  55. task.start();
  56. }
  57. });
  58. }
  59.  
  60. @Override
  61. public void notifyEventSuccess() {
  62. runOnUiThread(new Runnable() {
  63. ArrayList<IceCream> listOfIceCream = new ArrayList<>();
  64.  
  65. @Override
  66. public void run() {
  67. int rand;
  68. for(int index = 0; index < arrayOfIceCream.length; index++){
  69. Random r = new Random();
  70. rand = r.nextInt((max - min) + 1) + min;
  71. IceCream nCream = new IceCream(arrayOfIceCream[index], rand);
  72. listOfIceCream.add(nCream);
  73. }
  74.  
  75. Collections.sort(listOfIceCream, new Comparator<IceCream>() {
  76. @Override
  77. public int compare(IceCream o1, IceCream o2) {
  78. return Integer.valueOf(o2.getSum()).compareTo(o1.getSum());
  79. }
  80. });
  81.  
  82. CustomAdapter myAdapter = new CustomAdapter(MainActivity.this, R.layout.custom_list_layout, listOfIceCream);
  83. listToShow.setAdapter(myAdapter);
  84. }
  85. });
  86. }
  87. }
  88.  
  89.  
  90. ----activity_mail.xml
  91.  
  92. <ListView
  93. android:id="@+id/list_view"
  94. android:layout_width="match_parent"
  95. android:layout_height="match_parent"
  96. />
  97. <TextView
  98. android:id="@+id/total_points"
  99. android:layout_width="match_parent"
  100. android:layout_height="60dp"
  101. app:layout_constraintBottom_toTopOf="@+id/list_view"
  102. app:layout_constraintEnd_toEndOf="parent"
  103. app:layout_constraintHorizontal_bias="0.5"
  104. app:layout_constraintStart_toStartOf="parent"
  105. app:layout_constraintTop_toBottomOf="@+id/list_view" />
  106. <Button
  107. android:id="@+id/button"
  108. android:layout_width="wrap_content"
  109. android:layout_height="wrap_content"
  110. android:layout_marginStart="161dp"
  111. android:layout_marginLeft="161dp"
  112. android:layout_marginTop="16dp"
  113. android:layout_marginEnd="162dp"
  114. android:layout_marginRight="162dp"
  115. android:text="Button"
  116. app:layout_constraintEnd_toEndOf="parent"
  117. app:layout_constraintStart_toStartOf="parent"
  118. app:layout_constraintTop_toTopOf="parent" />
  119.  
  120. ----Task.java
  121.  
  122. package com.example.eurynomus.zadsladoledlikvidaciq;
  123.  
  124. public class Task extends Thread {
  125. private NotifyListener listener;
  126.  
  127. public Task(NotifyListener l)
  128. {
  129. listener = l;
  130. }
  131. public void run(){
  132.  
  133. Thread task1 = new Thread(new Runnable() {
  134. @Override
  135. public void run() {
  136. try {
  137. Thread.sleep(3000);
  138. } catch (InterruptedException e)
  139. {
  140. e.printStackTrace();
  141. }
  142. }
  143. });
  144.  
  145. task1.start();
  146.  
  147. try {
  148. task1.join();
  149. } catch (InterruptedException e)
  150. {
  151. e.printStackTrace();
  152. }
  153.  
  154. listener.notifyEventSuccess();
  155. }
  156. }
  157.  
  158. ----NotifyListener.java
  159. public interface NotifyListener {
  160. void notifyEventSuccess();
  161. }
  162.  
  163.  
  164. ----CustomAdapter.java
  165.  
  166. package com.example.eurynomus.zadsladoledlikvidaciq;
  167.  
  168. import android.content.Context;
  169. import android.view.LayoutInflater;
  170. import android.view.View;
  171. import android.view.ViewGroup;
  172. import android.widget.ArrayAdapter;
  173. import android.widget.TextView;
  174.  
  175. import java.util.ArrayList;
  176.  
  177. public class CustomAdapter extends ArrayAdapter<IceCream> {
  178. private static final String TAG = "CustomAdapter";
  179. private Context mContext;
  180. private int mResource;
  181.  
  182. public CustomAdapter(Context context, int resource, ArrayList<IceCream> objects) {
  183. super(context, resource, objects);
  184. mContext = context;
  185. mResource = resource;
  186. }
  187.  
  188.  
  189. @Override
  190. public View getView(int position, View convertView, ViewGroup parent) {
  191. String name = getItem(position).getName();
  192. int sum = getItem(position).getSum();
  193.  
  194. IceCream iceCream = new IceCream(name, sum);
  195.  
  196. LayoutInflater inflater = LayoutInflater.from(mContext);
  197. convertView = inflater.inflate(mResource, parent, false);
  198.  
  199. TextView teamName = (TextView) convertView.findViewById(R.id.name);
  200. TextView teamScore = (TextView) convertView.findViewById(R.id.sum);
  201.  
  202. teamName.setText(name);
  203. teamScore.setText("" + sum);
  204.  
  205. return convertView;
  206. }
  207. }
  208.  
  209. ----Custom_list_layout.xml
  210.  
  211. <?xml version="1.0" encoding="utf-8"?>
  212. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  213. android:layout_width="match_parent"
  214. android:layout_height="match_parent">
  215.  
  216. <TextView
  217. android:id="@+id/name"
  218. android:layout_width="match_parent"
  219. android:layout_height="60dp"
  220. android:layout_weight="30"
  221. android:gravity="center"
  222. android:textAlignment="center" />
  223.  
  224. <TextView
  225. android:id="@+id/sum"
  226. android:layout_width="match_parent"
  227. android:layout_height="60dp"
  228. android:layout_weight="70"
  229. android:gravity="center"
  230. android:textAlignment="center" />
  231.  
  232. </LinearLayout>
  233.  
  234. ----IceCream.java
  235.  
  236. package com.example.eurynomus.zadsladoledlikvidaciq;
  237.  
  238. public class IceCream {
  239.  
  240. private String name;
  241. private int sum;
  242.  
  243. public IceCream(String name, Integer sum)
  244. {
  245. this.name = name;
  246. this.sum = sum;
  247.  
  248. }
  249.  
  250. public String getName()
  251. {
  252. return name;
  253. }
  254. public Integer getSum()
  255. {
  256. return sum;
  257. }
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement