0

I need to compare 9 Integers to find out which is bigger, and i need the bigger to be in the first textview the second bigger integer to be in the second textview and so on.

The integers are collected by another activity

 //All the integers are not constant the numbers is just for the example

 integer1 = 10
 integer2 = 4
 integer3 = 13
 integer4 = 21 
 integer5 = 20
 integer6 = 2
 integer7 = 32
 integer8 = 100
 integer9 = 23

 //Heres the textviews
    WinnerTextView = findViewById(R.id.WinnerTextView);
    SecondTextView = findViewById(R.id.SecondTextView);
    ThirdTextView = findViewById(R.id.ThirdTextView);
    ForthTextView = findViewById(R.id.ForthTextView);
    FifthTextView = findViewById(R.id.FifthTextView);
    SixthTextView = findViewById(R.id.SixthTextView);
    SeventhTextView = findViewById(R.id.SeventhTextView);
    EigthTextView = findViewById(R.id.EighthTextView);
    NinthTextView =  findViewById(R.id.NinthTextView);

I tried with if, but its gonna take like 1000 lines of code.

coroutineDispatcher
  • 7,718
  • 6
  • 30
  • 58
triple50
  • 33
  • 6
  • 10
    1) Put the numbers into an array. 2) Sort the array 3) Loop the array to set text view values. Takes like 3 lines of code, you just have to try ;) – SME_Dev Sep 03 '19 at 15:11
  • 1
    Add the numbers in arraylist. Use Collections.sort(arraylist); Access the number based on index. – Rajnish suryavanshi Sep 03 '19 at 15:13
  • Possible duplicate of [Java: Finding the highest value in an array](https://stackoverflow.com/questions/1806816/java-finding-the-highest-value-in-an-array) – J03L Sep 03 '19 at 15:20

4 Answers4

1

At first, I suggest you to use a RecyclerView instead of that.

But if you don't want to use RecyclerView: I don't really suggest using a list at all. You wont need it. Just use a java int[] which is an array of integers.

Example:

int[] numbers = {10, 4, 13 , 21, 20, 2, 32, 100 , 23};

Arrays.sort(numbers);
//now numbers are sorted in ascending order

textViewNumber1.setText(String.valueOf(numbers[8]) //your first element is at index 0
textViewNumber2.setText(String.valueOf(numbers[7])) 
.... and so on so forth
coroutineDispatcher
  • 7,718
  • 6
  • 30
  • 58
1
List<Integer> items=new ArrayList<>();
// Add your integers in an Arraylist. 
items.add(integer1);
items.add(integer2);
....
....
items.add(integer9);

// Sort them in reverse order, so that bigger number comes first.
Arrays.sort(items, Collections.reverseOrder());

// Assign them to your Textview with index of the arraylist.
WinnerTextView.setText(items.get(0));
SecondTextView.setText(items.get(1));
...
...
NinthTextView.setText(items.get(9));

Hope it helps:)

0

Can Do something like this

ArrayList<Integer> al = new ArrayList<Integer>(); 
    al.add("1"); 
    al.add("2"); 
    al.add("3"); 
    al.add("4"); 
    al.add("5"); 

    /* Collections.sort method is sorting the 
    elements of ArrayList in descendingorder. */
    Collections.sort(al, Collections.reverseOrder());
Rajnish suryavanshi
  • 3,168
  • 2
  • 17
  • 23
0

So you need to have a list of integers from that Activity. As previously mentioned. Simple code would be:

List<Integer> numList = new ArrayList<>();
numList.add(10);
...
numList.add(23);
Collections.sort(numList);
//Reverse so the first one will be the biggest
Collections.reverse(numList);

Now you have numbers in correct order so you should somehow bind them to views. For this purpose create another list but it will consist of TextViews in desired order

List<TextView> txList = new ArrayList<>();
txList.add((TextView)findViewById(R.id.WinnerTextView);
...
txList.add((TextView)findViewById(R.id.NinthTextView);

Using this list you could bind results to text view like so:

for (int i = 0; i < txList.size() && i < numList.size(); i++) {
    txList.get(i).setText((numList.get(i).toString()));
}

I've added additional check so code will work even if you change your mind and increase or decrease range.

The same could be done using iterators like so:

Iterator<Integer> nums = numList.iterator();
Iterator<TextView> textViews = txList.iterator();

while(nums.hasNext() && textViews.hasNext()) {
    textViews.next().setText(nums.next().toString());
}

And in the end this will solve your current problem but as mentioned in other comments you just need a dedicated view to display a list of items. It could be simple ListView with ArrayAdapter or it could be RecyclerView a bit more code to write for this task but is recommended.

j2ko
  • 2,479
  • 1
  • 16
  • 29