How to pass Activity values to useful?

Asked

Viewed 47 times

-1

I would like to work with the item object but I need to pass values of my activity to the usel someone can explain me?

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

For example where there is "blue" I would like to be receiving a value of my activity as it would be possible?

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);
            }

        }

    }
  • I didn’t quite understand what you want, you have an Object, with 3 attributes, then you create an array of that same object and use it within that object.

  • For example if I have another activity I can use that object ?

  • Well, this class is a little bit poorly formed for me, its intention with Initialize()` is to return an array with 4 pre-initialized objects ?

  • yes that’s right

1 answer

1


Bom Tiago, I do not see a good reason to try to instantiate a class within itself, I believe that the ideal would be for you to divide this class in two, being one only with the Object that will have the information, and one to do this initialization, arrangement and etc..

This way, each "item" will have a separate data instance.

So I’d do it this way:

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 his 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);

Ready, you will have in your Activity the object that was the "item", containing the 4 objects.

  • The aim was the reverse was to put Vex objects in there to remove I do not know how to manipulate the code for it

  • You want to put 4 objects and return them organized that’s it ?

  • But the values are fixed in Initialize

  • Yes thanks in advance

  • It is my first time using useful if I find that initialize is not necessary for the intended can withdraw I am trying to learn how to draw multiple arrays of an activity that are sent there to the useful

  • Util is the library that organizes, it has nothing to do with how to build the class, I can change to a method where you receive an array and return it in the organized end, the data is usually mutable, so you have to assign the values in Activity, which is the most usual

  • That’s exactly what I’m looking for and thank you for explaining

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.