0

我有一个GridView设置,我已经实现了setOnItemClickListener接口。首先我做了一点Toast反馈,然后我继续调用我的方法。第一种方法很有效,我将它放入一个 while 循环中,该循环将调用它直到它返回true。第二种方法,当插入到代码中时,会阻止第一种方法工作,但我没有得到任何编译器错误。

这是我用我要插入的第二种方法onCreate包含setOnItemClickListener() 注释掉的内容:

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this));

    final GameplayController gc = new GameplayController(this);

    // Create board model, add units to player's list.
    final UnitArray gameBoardModel = new UnitArray();

    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v,
                int position, long id) {
            Toast.makeText(MainActivity.this, "" + position,
                    Toast.LENGTH_SHORT).show();
            boolean next = false;
            while (next == false) {
                next = gc.choosePiece(position, gameBoardModel,
                        (ImageView) v);
            }
            /*next = false;
            while (next == false) {
                next = gc.chooseEmpty(position, gameBoardModel,
                        (ImageView) v);
            }*/
        }
    });
}

我说第一种方法“有点”有效,因为它是一种检查GridView所选位置是否为“允许”选择的方法,并将更新该ImageView选择的位置,然后return true,否则它return false的想法是它将循环直到用户做出“有效”的选择。现在,如果做出了有效的选择,它确实会ImageView相应地更新 at 选择,但是当做出“无效”选择时,它只会冻结。

所以它“有点”有效,但是当我插入我的第二种方法时,即使做出了“有效”选择,它也会冻结并且它永远不会进入第二种方法。

我认为我对onItemClickListener().

--------------------------编辑-------附录--- ------------

如果有助于查看我调用的第一个方法,

public boolean choosePiece(int position, UnitArray ua, ImageView iv) {
    if (ua.checkPiece(pNum, position) == true) {
        if (ua.checkType(position, "rock")) {
            ImageView v = iv;
            v.setImageResource(R.drawable.rock1select);
            return true;
        }
        if (ua.checkType(position, "paper")) {
            iv.setImageResource(R.drawable.paper1select);
            return true;
        }
        if (ua.checkType(position, "scissors")) {
            iv.setImageResource(R.drawable.scissors1select);
            return true;
        }
    }
    /*
     * With just the IF statements above catching the correct selections, my
     * program crashes. I thought simply adding the RETURN FALSE; to catch
     * everything else would work since it should just keep looping from the
     * mAINaCTIVITY until it hits one of them and RETURNS TRUE;. So I will
     * add this ELSE so that it will just redraw what is already there,
     * maybe that will fix it? Like, maybe it really really wants to use the
     * freaking listener for something.
     * 
     * Nope, that didn't help at all.
     */
    else {
        System.out.println("Ohhhh, NO YOU DIDN'T!!!");
        if (ua.checkType(position, "rock")) {
            System.out.println("Bad rock!, BAD!");
            ImageView v = iv;
            v.setImageResource(R.drawable.rock2);
            return false;
        }
        if (ua.checkType(position, "paper")) {
            ImageView v = iv;
            v.setImageResource(R.drawable.paper2);
            return false;
        }
        if (ua.checkType(position, "scissors")) {
            ImageView v = iv;
            v.setImageResource(R.drawable.scissors2);
            return false;
        }
        if (ua.checkType(position, "empty")) {
            ImageView v = iv;
            v.setImageResource(R.drawable.blank);
            return false;
        }
    }
    return false;
}

也许我必须在这里添加一些东西来告诉onItemClick..继续前进并停止在这里发送触摸事件?

4

3 回答 3

2

假设您知道 GameplayController 将查看的元素数量:

boolean found = false;
int size = numberOfElements;
for(int i=0; i<size;i++) {
    if(gc.choosePiece(position, gameBoardModel,(ImageView) v)==true) {
        found = true;
        break;
    }
}
if(found) {
// do what u expect
}
于 2013-04-26T08:58:16.403 回答
0

您说当您向方法添加一些带有 while 循环的代码时,该方法似乎永远不会成功返回。您是否考虑过第二个 while 循环执行的条件永远不会评估为假?(在这种情况下,当值“true”分配给变量“next”时?)

如果你不确定,也许你应该在每个循环中添加一个 System.out.println() 来输出正在执行的循环以及“next”的值,很可能它永远保持错误,否则不会按照你的想法做.

于 2013-04-26T08:34:55.890 回答
0

好吧,我的代码在提出这个问题时太糟糕了,但不能忽视的直接问题是我正在创建boolean nextINSIDE onClick,所以每次 onClick 发生时它都会被重置。将创建移动boolean next到 onClick 之前可以解决问题。

有时我看一个小问题太久了,除了问题之外什么都看不到,即使解决方案非常简单。

于 2013-05-01T20:13:12.810 回答