Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9109125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:58:05+00:00 2026-06-17T02:58:05+00:00

I have an integer ArrayList and several buttons. Each button corresponds to an index

  • 0

I have an integer ArrayList and several buttons. Each button corresponds to an index of the ArrayList and I want to display the value of each button’s corresponding index as its text. I have everything working with button behavior, etc. The problem is when I go to swap a button’s value (index value) with another it will start to display 0s for every value I swap it with. I am always going to be swapping the 0 with something else.

So if I have buttons: b1, b2, b3 with values 0, 1, 2 respectively and I swap the values of b1 and b2. The result becomes 0, 0, 2. This is the swapping function:

public static void swap(ArrayList<Integer> list, int firstInd, int secondInd) {
    int temp = list.get(firstInd);
    list.set(firstInd, list.get(secondInd));
    list.set(secondInd, temp);
}

This swap method works, I have tested it independently using print statements, etc. and there are no duplicate 0s and all of the rest of the numbers remain in the list.

Here is the relevant code:

// class declaration
static ArrayList<Integer> numList = new ArrayList<Integer>();

// onCreate
// initialize ArrayList
// displays initial values on buttons

@Override
public void onClick(View v) {
    switch(v.getId()) {
        case R.id.first: {
            switchButtonValues(R.id.first);
            b1.setText(String.valueOf(numList.get(0)));
            updateButtonStates();
            break;
        }
        case R.id.second: {
            switchButtonValues(R.id.second);
            b2.setText(String.valueOf(numList.get(1)));
            updateButtonStates();
            break;
        }
// etc for all buttons

public static void switchButtonValues(int buttonNum) {
    switch(buttonNum) {
        case R.id.first: {
            if(numList.get(1) == 0) {
                swap(numList, 0, 1);
            } else if (numList.get(3) == 0) {
                swap(numList, 0, 3);
            } else {

            }

            break;
        }
        case R.id.second: {
// etc. for the buttons

The buttons displaying the initial values is correct. So I know the ArrayList is getting initialized properly. The problem occurs when the swapping occurs. All of the values are getting overwritten with 0 somehow.

Even though 0s are overwriting the other values, the button behavior is working correctly because it knows where the “real” 0 is. I have cleaned the project too.

Why is this happening? Anyone have any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T02:58:07+00:00Added an answer on June 17, 2026 at 2:58 am

    I did an example, I hope it’s near what you expect:

    package br.com.bertan.swap.buttons;
    
    import java.util.ArrayList;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    
    public class SwapButtonsActivity extends Activity {
    
        // class declaration
        static ArrayList<Integer> numList = new ArrayList<Integer>();
    
        static ArrayList<Button> btnList = new ArrayList<Button>();
    
        public static void swap(ArrayList<Integer> list, int firstInd, int secondInd) {
            int temp = list.get(firstInd);
            list.set(firstInd, list.get(secondInd));
            list.set(secondInd, temp);
        }
    
        private void updateButtonStates(){
    
            int index_0 = -1;
    
            int index_up = -1;
            int index_down = -1;
            int index_left = -1;
            int index_right = -1;
    
            for(int i = 0; i < 16; i++){
                if(index_0 >= 0){   
                    if(i != index_up
                       && i != index_down
                       && i != index_left
                       && i != index_right
    //                 && i != index_0
                       ){
                        btnList.get(i).setEnabled(false);
                    } else {
                        btnList.get(i).setEnabled(true);
                    }
                } else {
                    if(numList.get(i) == 0){
                        index_0 = i;
    
                        if(index_0 > 3){
                            index_up = index_0 - 4;
                        }
    
                        if(index_0 < 12){
                            index_down = index_0 + 4;
                        }
    
                        if(index_0 != 0
                           && index_0 != 4
                           && index_0 != 8
                           && index_0 != 12){
                            index_left = index_0 - 1;
                        }
    
                        if(index_0 != 3
                           && index_0 != 7
                           && index_0 != 11
                           && index_0 != 15){
                            index_right = index_0 + 1;
                        }
    
                        i = -1;
                    }
                }
            }
        }
    
        public static void switchButtonValues(int buttonNum) {
            int index_0 = -1;
            int index_btn = -1;
    
            for(int i = 0; i < 16; i++){
                if(index_0 < 0){
                    if(numList.get(i) == 0){
                        index_0 = i;
                    }
                }
    
                if(index_btn < 0){
                    if(buttonNum == btnList.get(i).getId()){
                        index_btn = i;
                    }
                }
    
                if(index_0 >= 0 && index_btn >=0){
                    break;
                }
            }
    
            btnList.get(index_0).setText(btnList.get(index_btn).getText().toString());
            btnList.get(index_btn).setText("0");
    
            numList.set(index_0, numList.get(index_btn));
            numList.set(index_btn, 0);
        }
    
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
    
            // initialize ArrayList
            for(int i = 0; i < 16; i++){
                numList.add(i);
            }
    
            final Button btn_01 = (Button) findViewById(R.id.btn_01);
            final Button btn_02 = (Button) findViewById(R.id.btn_02);
            final Button btn_03 = (Button) findViewById(R.id.btn_03);
            final Button btn_04 = (Button) findViewById(R.id.btn_04);
            final Button btn_05 = (Button) findViewById(R.id.btn_05);
            final Button btn_06 = (Button) findViewById(R.id.btn_06);
            final Button btn_07 = (Button) findViewById(R.id.btn_07);
            final Button btn_08 = (Button) findViewById(R.id.btn_08);
            final Button btn_09 = (Button) findViewById(R.id.btn_09);
            final Button btn_10 = (Button) findViewById(R.id.btn_10);
            final Button btn_11 = (Button) findViewById(R.id.btn_11);
            final Button btn_12 = (Button) findViewById(R.id.btn_12);
            final Button btn_13 = (Button) findViewById(R.id.btn_13);
            final Button btn_14 = (Button) findViewById(R.id.btn_14);
            final Button btn_15 = (Button) findViewById(R.id.btn_15);
            final Button btn_16 = (Button) findViewById(R.id.btn_16);
    
            btnList.add(btn_01);
            btnList.add(btn_02);
            btnList.add(btn_03);
            btnList.add(btn_04);
            btnList.add(btn_05);
            btnList.add(btn_06);
            btnList.add(btn_07);
            btnList.add(btn_08);
            btnList.add(btn_09);
            btnList.add(btn_10);
            btnList.add(btn_11);
            btnList.add(btn_12);
            btnList.add(btn_13);
            btnList.add(btn_14);
            btnList.add(btn_15);
            btnList.add(btn_16);
    
            OnClickListener click_listener = new OnClickListener() {            
                @Override
                public void onClick(View v) {
                    switch(v.getId()) {
                        case R.id.btn_01: {
                            switchButtonValues(R.id.btn_01);
                            btn_01.setText(String.valueOf(numList.get(0)));
                            updateButtonStates();
                            break;
                        }
                        case R.id.btn_02: {
                            switchButtonValues(R.id.btn_02);
                            btn_02.setText(String.valueOf(numList.get(1)));
                            updateButtonStates();
                            break;
                        }
                        case R.id.btn_03: {
                            switchButtonValues(R.id.btn_03);
                            btn_03.setText(String.valueOf(numList.get(2)));
                            updateButtonStates();
                            break;
                        }
                        case R.id.btn_04: {
                            switchButtonValues(R.id.btn_04);
                            btn_04.setText(String.valueOf(numList.get(3)));
                            updateButtonStates();
                            break;
                        }
                        case R.id.btn_05: {
                            switchButtonValues(R.id.btn_05);
                            btn_05.setText(String.valueOf(numList.get(4)));
                            updateButtonStates();
                            break;
                        }
                        case R.id.btn_06: {
                            switchButtonValues(R.id.btn_06);
                            btn_06.setText(String.valueOf(numList.get(5)));
                            updateButtonStates();
                            break;
                        }
                        case R.id.btn_07: {
                            switchButtonValues(R.id.btn_07);
                            btn_07.setText(String.valueOf(numList.get(6)));
                            updateButtonStates();
                            break;
                        }
                        case R.id.btn_08: {
                            switchButtonValues(R.id.btn_08);
                            btn_08.setText(String.valueOf(numList.get(7)));
                            updateButtonStates();
                            break;
                        }
                        case R.id.btn_09: {
                            switchButtonValues(R.id.btn_09);
                            btn_09.setText(String.valueOf(numList.get(8)));
                            updateButtonStates();
                            break;
                        }
                        case R.id.btn_10: {
                            switchButtonValues(R.id.btn_10);
                            btn_10.setText(String.valueOf(numList.get(9)));
                            updateButtonStates();
                            break;
                        }
                        case R.id.btn_11: {
                            switchButtonValues(R.id.btn_11);
                            btn_11.setText(String.valueOf(numList.get(10)));
                            updateButtonStates();
                            break;
                        }
                        case R.id.btn_12: {
                            switchButtonValues(R.id.btn_12);
                            btn_12.setText(String.valueOf(numList.get(11)));
                            updateButtonStates();
                            break;
                        }
                        case R.id.btn_13: {
                            switchButtonValues(R.id.btn_13);
                            btn_13.setText(String.valueOf(numList.get(12)));
                            updateButtonStates();
                            break;
                        }
                        case R.id.btn_14: {
                            switchButtonValues(R.id.btn_14);
                            btn_14.setText(String.valueOf(numList.get(13)));
                            updateButtonStates();
                            break;
                        }
                        case R.id.btn_15: {
                            switchButtonValues(R.id.btn_15);
                            btn_15.setText(String.valueOf(numList.get(14)));
                            updateButtonStates();
                            break;
                        }
                        case R.id.btn_16: {
                            switchButtonValues(R.id.btn_16);
                            btn_16.setText(String.valueOf(numList.get(15)));
                            updateButtonStates();
                            break;
                        }
                        default:
                            break;
                    }
                }
            };
    
            // displays initial values on buttons
            for(int i = 0; i < 16; i++){
                btnList.get(i).setText(String.valueOf(numList.get(i)));
                btnList.get(i).setOnClickListener(click_listener);
            }
    
            updateButtonStates();
        }
    }
    

    And here is my layout:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:orientation="vertical">
        <TableLayout 
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">
            <TableRow 
                android:layout_width="fill_parent"
                android:layout_height="0dip"
                android:layout_weight="1"
                android:orientation="horizontal">
                <Button 
                    android:id="@+id/btn_01"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="0"
                    android:textSize="50dip"
                    android:textStyle="bold"
                />
                <Button 
                    android:id="@+id/btn_02"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="1"
                    android:textSize="50dip"
                    android:textStyle="bold"
                />
                <Button 
                    android:id="@+id/btn_03"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="2"
                    android:textSize="50dip"
                    android:textStyle="bold"
                />
                <Button 
                    android:id="@+id/btn_04"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="3"
                    android:textSize="50dip"
                    android:textStyle="bold"
                />
            </TableRow>
            <TableRow 
                android:layout_width="fill_parent"
                android:layout_height="0dip"
                android:layout_weight="1"
                android:orientation="horizontal">
                <Button 
                    android:id="@+id/btn_05"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="4"
                    android:textSize="50dip"
                    android:textStyle="bold"
                />
                <Button 
                    android:id="@+id/btn_06"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="5"
                    android:textSize="50dip"
                    android:textStyle="bold"
                />
                <Button 
                    android:id="@+id/btn_07"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="6"
                    android:textSize="50dip"
                    android:textStyle="bold"
                />
                <Button 
                    android:id="@+id/btn_08"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="7"
                    android:textSize="50dip"
                    android:textStyle="bold"
                />
            </TableRow>
            <TableRow 
                android:layout_width="fill_parent"
                android:layout_height="0dip"
                android:layout_weight="1"
                android:orientation="horizontal">
                <Button 
                    android:id="@+id/btn_09"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="8"
                    android:textSize="50dip"
                    android:textStyle="bold"
                />
                <Button 
                    android:id="@+id/btn_10"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="9"
                    android:textSize="50dip"
                    android:textStyle="bold"
                />
                <Button 
                    android:id="@+id/btn_11"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="10"
                    android:textSize="50dip"
                    android:textStyle="bold"
                />
                <Button 
                    android:id="@+id/btn_12"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="11"
                    android:textSize="50dip"
                    android:textStyle="bold"
                />
            </TableRow>
            <TableRow 
                android:layout_width="fill_parent"
                android:layout_height="0dip"
                android:layout_weight="1"
                android:orientation="horizontal">
                <Button 
                    android:id="@+id/btn_13"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="12"
                    android:textSize="50dip"
                    android:textStyle="bold"
                />
                <Button 
                    android:id="@+id/btn_14"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="13"
                    android:textSize="50dip"
                    android:textStyle="bold"
                />
                <Button 
                    android:id="@+id/btn_15"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="14"
                    android:textSize="50dip"
                    android:textStyle="bold"
                />
                <Button 
                    android:id="@+id/btn_16"
                    android:layout_width="0dip"
                    android:layout_height="fill_parent"
                    android:layout_weight="1"
                    android:text="15"
                    android:textSize="50dip"
                    android:textStyle="bold"
                />
            </TableRow>
        </TableLayout>
    </LinearLayout>
    

    that’s worked just fine, and really quite a funny when you shuffle the places and try to put then in order… hehehe

    It’s simple and was one good idea for a game 😀 (I will do some sort of this genre and let see what happens)

    I think that’s possible to better the code, I had suspect of this part of your code:

    case R.id.btn_01: {
        switchButtonValues(R.id.btn_01);
        btn_01.setText(String.valueOf(numList.get(0)));
        updateButtonStates();
        break;
    }
    

    I thinked you switch the values, and next set the text with the numList value of that position, but, you had changed with the 0, isn’t? Maybe it’s your problem, try put the line that set the text before the switch…

    cya,
    Bertan.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple Integer ArrayList , which contains some duplicate elements. I want to
Possible Duplicate: list.clear() vs list = new ArrayList<int>(); I have a list: List<Integer> l1
Possible Duplicate: How to convert List<Integer> to int[] in Java? I have an ArrayList
I have an integer value that spans a range so large it is impractical
I have an integer column and I want to find numbers that start with
I have an ArrayList<Integer> that is full of years that it pulls from my
I have an ArrayList<String> A that contains n elements, and an ArrayList<Integer> B that
I have; List<String> stringList = new ArrayList<String>(); List<Integer> integerList = new ArrayList<Integer>(); Is there
I have a 2D List of type Integer ( ArrayList< List < Integer >
I have a problem sorting the contents of a 2D integer array (not ArrayList)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.