我有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
,但是当选择“无效”时,它就会冻结。
因此它“有点”有效,但是当我插入第二个方法时,即使进行了“有效”选择,它也会冻结,并且它永远不会进入第二种方法。
我认为onItemClickListener()
。
-------------------------- EDIT -------------------附录--------------
如果它有助于查看我正在调用的第一个方法,
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..
继续前进并停止发送触摸事件?
答案 0 :(得分: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
}
答案 1 :(得分:0)
您说当您使用while循环向方法添加一些代码时,该方法似乎永远不会成功返回。您是否认为第二次while循环执行的条件永远不会计算为false? (在这种情况下,当值“true”被赋值给变量“next”?)
如果你不确定,也许你应该在每个循环中添加一个System.out.println(),输出哪个循环和“next”的值一起执行,很可能它永远都是假的,否则就不会你的想法。
答案 2 :(得分:0)
我的代码在提出这个问题时非常糟糕,但是直接的问题是我在onClick中创建了boolean next
INSIDE,因此每次onClick都会被重置。将创建boolean next
移至onClick之前将解决问题。
有时候我会看一个小问题太长时间,除了解决方案很简单之外什么都看不到问题。