Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -----MainActivity.java
- public class MainActivity extends AppCompatActivity implements NotifyListener {
- private String[] arrayOfIceCream;
- private ListView listToShow;
- Button button;
- private int min = 40;
- private int max = 100;
- private int t = 1;
- int rand;
- int rand2 = rand - 1;
- //List Adapter with Ice Creams
- //SUM is added randomly
- //Thread
- //Button click = -1 sum
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- listToShow = findViewById(R.id.list_view);
- button = findViewById(R.id.button);
- arrayOfIceCream = new String [] {"Strawberry", "Chocolate", "Banana", "Cookies", "Watermelon"};
- final ArrayList<IceCream> listOfIceCream = new ArrayList<>();
- for(int index = 0; index < arrayOfIceCream.length; index++){
- Random r = new Random();
- rand = r.nextInt((max - min) + 1) + min;
- final IceCream nCream = new IceCream(arrayOfIceCream[index], rand);
- listOfIceCream.add(nCream);
- }
- Collections.sort(listOfIceCream, new Comparator<IceCream>() {
- @Override
- public int compare(IceCream o1, IceCream o2) {
- return Integer.valueOf(o2.getSum()).compareTo(o1.getSum());
- }
- });
- CustomAdapter myAdapter = new CustomAdapter(this, R.layout.custom_list_layout, listOfIceCream);
- listToShow.setAdapter(myAdapter);
- final Task task = new Task (MainActivity.this);
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- task.start();
- }
- });
- }
- @Override
- public void notifyEventSuccess() {
- runOnUiThread(new Runnable() {
- ArrayList<IceCream> listOfIceCream = new ArrayList<>();
- @Override
- public void run() {
- int rand;
- for(int index = 0; index < arrayOfIceCream.length; index++){
- Random r = new Random();
- rand = r.nextInt((max - min) + 1) + min;
- IceCream nCream = new IceCream(arrayOfIceCream[index], rand);
- listOfIceCream.add(nCream);
- }
- Collections.sort(listOfIceCream, new Comparator<IceCream>() {
- @Override
- public int compare(IceCream o1, IceCream o2) {
- return Integer.valueOf(o2.getSum()).compareTo(o1.getSum());
- }
- });
- CustomAdapter myAdapter = new CustomAdapter(MainActivity.this, R.layout.custom_list_layout, listOfIceCream);
- listToShow.setAdapter(myAdapter);
- }
- });
- }
- }
- ----activity_mail.xml
- <ListView
- android:id="@+id/list_view"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- />
- <TextView
- android:id="@+id/total_points"
- android:layout_width="match_parent"
- android:layout_height="60dp"
- app:layout_constraintBottom_toTopOf="@+id/list_view"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintHorizontal_bias="0.5"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/list_view" />
- <Button
- android:id="@+id/button"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginStart="161dp"
- android:layout_marginLeft="161dp"
- android:layout_marginTop="16dp"
- android:layout_marginEnd="162dp"
- android:layout_marginRight="162dp"
- android:text="Button"
- app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintStart_toStartOf="parent"
- app:layout_constraintTop_toTopOf="parent" />
- ----Task.java
- package com.example.eurynomus.zadsladoledlikvidaciq;
- public class Task extends Thread {
- private NotifyListener listener;
- public Task(NotifyListener l)
- {
- listener = l;
- }
- public void run(){
- Thread task1 = new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- Thread.sleep(3000);
- } catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- }
- });
- task1.start();
- try {
- task1.join();
- } catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- listener.notifyEventSuccess();
- }
- }
- ----NotifyListener.java
- public interface NotifyListener {
- void notifyEventSuccess();
- }
- ----CustomAdapter.java
- package com.example.eurynomus.zadsladoledlikvidaciq;
- 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 java.util.ArrayList;
- public class CustomAdapter extends ArrayAdapter<IceCream> {
- private static final String TAG = "CustomAdapter";
- private Context mContext;
- private int mResource;
- public CustomAdapter(Context context, int resource, ArrayList<IceCream> objects) {
- super(context, resource, objects);
- mContext = context;
- mResource = resource;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- String name = getItem(position).getName();
- int sum = getItem(position).getSum();
- IceCream iceCream = new IceCream(name, sum);
- LayoutInflater inflater = LayoutInflater.from(mContext);
- convertView = inflater.inflate(mResource, parent, false);
- TextView teamName = (TextView) convertView.findViewById(R.id.name);
- TextView teamScore = (TextView) convertView.findViewById(R.id.sum);
- teamName.setText(name);
- teamScore.setText("" + sum);
- return convertView;
- }
- }
- ----Custom_list_layout.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <TextView
- android:id="@+id/name"
- android:layout_width="match_parent"
- android:layout_height="60dp"
- android:layout_weight="30"
- android:gravity="center"
- android:textAlignment="center" />
- <TextView
- android:id="@+id/sum"
- android:layout_width="match_parent"
- android:layout_height="60dp"
- android:layout_weight="70"
- android:gravity="center"
- android:textAlignment="center" />
- </LinearLayout>
- ----IceCream.java
- package com.example.eurynomus.zadsladoledlikvidaciq;
- public class IceCream {
- private String name;
- private int sum;
- public IceCream(String name, Integer sum)
- {
- this.name = name;
- this.sum = sum;
- }
- public String getName()
- {
- return name;
- }
- public Integer getSum()
- {
- return sum;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement