我有一个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..
继续前进并停止在这里发送触摸事件?