Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to call more that one method inside of an `setOnItemClickListener(...`
    text
    copied!<p>I have a <code>GridView</code> setup and I have implemented the <code>setOnItemClickListener</code> interface. Firstly I have made a little <code>Toast</code> just for feedback and then I moved on to calling my methods. The first method sort of works, I put it into a while loop that will call it until it returns <code>true</code>. The second method, when inserted into the code, prevents the first method from working at all, but I don't get any compiler errors.</p> <p>Here is my <code>onCreate</code> containing the <code>setOnItemClickListener()</code> <em>with my second method I want to insert commented out</em>:</p> <pre><code>@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&lt;?&gt; 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); }*/ } }); } </code></pre> <p>I say the first method "kinda" of works because it is a method that will check if the position in the <code>GridView</code> selected is an "allowable" selection and will update the <code>ImageView</code> at that selection and then <code>return true</code>, otherwise it will <code>return false</code> with the idea being that it will loop until the user makes a "valid" selection. Now, if a valid selection is made, it indeed does update the <code>ImageView</code> at the selection accordingly, but when an "invalid" selection is made it simply freezes.</p> <p>So it "kinda" works, but when I insert my second method, it freezes even if a "valid" selection is made and it never makes it to the second method.</p> <p>I think there is something I'm missing about the <code>onItemClickListener()</code>.</p> <p>--------------------------EDIT-------------------ADDENDUM--------------</p> <p>In case it helps to see the first method I'm calling, </p> <pre><code>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; } </code></pre> <p>Maybe there is something here that I have to add to tell the <code>onItemClick..</code> to move on and stop sending touch events here??</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload