How to pass activity values to util?

-1

I would like to work with the item object but I need to pass values from my activity to the util one can you explain?

    item[0] = new ColorClothes();
    item[0].record = 0;
    item[0].color = "blue";
    item[0].clothes = "shoes";

For example where "blue" would like to receive a value from my activity how could it be?

import java.util.*;


    public class ColorClothes
    {
        public int record;
        public String color;
        public String clothes;

        public static void main(String[] args)
        {
            Initialize();
        }

        public static void Initialize()
        {
            ColorClothes item[] = new ColorClothes[4];

            item[0] = new ColorClothes();
            item[0].record = 0;
            item[0].color = "blue";
            item[0].clothes = "shoes";

            item[1] = new ColorClothes();
            item[1].record = 1;
            item[1].color = "yellow";
            item[1].clothes = "pants";

            item[2] = new ColorClothes();
            item[2].record = 2;
            item[2].color = "red";
            item[2].clothes = "boots";

            item[3] = new ColorClothes();
            item[3].record = 3;
            item[3].color = "black";
            item[3].clothes = "coat";

            System.out.println("Unsorted");

            for(int i = 0; i < item.length; i++)
            {
                System.out.println(item[i].record + "     " + item[i].color + "     " + item[i].clothes);
            }

            System.out.println("\nSorted By Color\n");

            Arrays.sort(item, new ColorComparator());

            for(int i = 0; i < item.length; i++)
            {
                System.out.println(item[i].record + "     " + item[i].color + "     " + item[i].clothes);
            }

            System.out.println("\nSorted By Clothes\n");

            Arrays.sort(item, new ClothesComparator());

            for(int i = 0; i < item.length; i++)
            {
                System.out.println(item[i].record + "     " + item[i].color + "     " + item[i].clothes);
            }

        }

    }
    
asked by anonymous 07.07.2016 / 19:02

1 answer

1

Good Tiago, I do not see a good reason to try to instantiate a class within itself, I believe that it would be ideal to divide this class into two, one with only the Object that will have the information, and one to do this initialization, arrangement, and so on ..

In this way, each "item" will have a separate instance of data.

So, I would do it like this:

ColorClothes:

public class ColorClothes
{

    public ColorClothes() // <------ método construtor
    {
       // O ideal é usar esse método somente para trazer parâmetros iniciais para a instância
    }

    public Clothes[] Initialize(Clothes[] item)
    {
        Clothes item[] = new Clothes[4]; // <----------- utilize o objeto com os dados


        System.out.println("Unsorted");

        for(int i = 0; i < item.length; i++)
        {
            System.out.println(item[i].record + "     " + item[i].color + "     " + item[i].clothes);
        }

        System.out.println("\nSorted By Color\n");

        Arrays.sort(item, new ColorComparator());

        for(int i = 0; i < item.length; i++)
        {
            System.out.println(item[i].record + "     " + item[i].color + "     " + item[i].clothes);
        }

        System.out.println("\nSorted By Clothes\n");

        Arrays.sort(item, new ClothesComparator());

        for(int i = 0; i < item.length; i++)
        {
            System.out.println(item[i].record + "     " + item[i].color + "     " + item[i].clothes);
        }

 return item;

    }


    public class Clothes{
        public int record;
        public String color;
        public String clothes;
   }

    }

In your Activity:

Clothes[] listaClothes = new Clothes[4];
            listaClothes [0] = new Clothes();
            listaClothes [0].record = 0;
            listaClothes [0].color = "blue";
            listaClothes [0].clothes = "shoes";
        listaClothes [1] = new Clothes();
        listaClothes [1].record = 1;
        listaClothes [1].color = "yellow";
        listaClothes [1].clothes = "pants";

        listaClothes [2] = new Clothes();
        listaClothes [2].record = 2;
        listaClothes [2].color = "red";
        listaClothes [2].clothes = "boots";

        listaClothes [3] = new Clothes();
        listaClothes [3].record = 3;
        listaClothes [3].color = "black";
        listaClothes [3].clothes = "coat";



ColorClothes colorClothes = new ColorClothes();    
Clothes[] listaOrganizada= colorClothes.Initialize(listaCloth);

You will have, in your activity, the object that was the "item" containing the 4 objects.

    
07.07.2016 / 21:57