按钮值未正确更新

时间:2013-01-02 18:30:10

标签: java android arraylist

我有一个整数ArrayList和几个按钮。每个按钮对应ArrayList的索引,我想显示每个按钮的相应索引的值作为其文本。我有一切工作与按钮行为等。问题是,当我去交换按钮的值(索引值)与另一个它将开始为我交换它的每个值显示0。我总是会用其他东西换掉0。

所以,如果我有按钮:b1,b2,b3分别为0,1,2,我交换b1和b2的值。结果变为0,0,2。这是交换功能:

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

这种交换方法有效,我已经使用print语句等独立测试了它,并且没有重复的0,所有其余的数字都保留在列表中。

以下是相关代码:

// 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

显示初始值的按钮是正确的。所以我知道ArrayList正在初始化。发生交换时会出现问题。不知怎的,所有的值都被0覆盖了。

即使0正在覆盖其他值,按钮行为也能正常工作,因为它知道“真实”0的位置。我也清理了这个项目。

为什么会这样?有人有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我做了一个例子,我希望它接近你的期望:

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

这是我的布局:

<?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>

这很好用,当你洗牌并尝试按顺序排列时真的很有趣......呵呵

这很简单,对于一款游戏来说是一个好主意:D(我会做一些这种类型,让我们看看会发生什么)

我认为可以改进代码,我怀疑代码的这一部分:

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

我认为你切换了值,接下来用该位置的numList值设置文本,但是,你用0改变了,不是吗?也许这是你的问题,尝试在开关之前设置文本行......

CYA, Bertan。