Note that there are some explanatory texts on larger screens.

plurals
  1. PODisable a Gridview position in a row in Android
    primarykey
    data
    text
    <p>I have tried all my best to look for solution everywhere including SO but it seems that I am not getting anywhere and I decided to ask the question to see if I can get someone that will help me out on this. OK here is what I am trying to do:</p> <p>I have a GridView of 4 columns and 4 rows. The GridView is filled with images using imageAdapter. When an image is selected in a row the image in that position changes to another image and when it is selected (Deselected) again it changes back to the previous image. That is fine. what I am trying to do now is that when a position is selected, I want to disable all other position until the selected position is deselected by clicking on it again. In essence I want to make sure that no two positions can be selected at the same time. I have tried my best but this seems a bit impossible for now. Can anyone please tell me what I am doing wrong. When the user select a position, in a row others don't get disabled. Below is my code: Any help will be much appreciated. Thank you for your anticipated help.</p> <p>private OnItemClickListener onGridItemClickListener = new OnItemClickListener() {</p> <pre><code> public void onItemClick(AdapterView&lt;?&gt; parent, View v, int position, long id) { //if (v == null) return; //Just added 03/04/2013 int rowOfSelectedItem = getRowInGrid(position); Log.i(TAG, "Selected View is : "+v.getId()+" (Id arg = "+id+")- Position is "+ position+" Row is = "+rowOfSelectedItem); switch(rowOfSelectedItem){ case 1: //iterate through first row items and check if any of the items was selected before for(int x=2; x &lt; 4; x++){ // Object tag = ((ImageView)gridView.getChildAt(x)).getTag(); Log.i(TAG, "This is : "+ x + "TAG is " + tag); if(tag ==null || !Boolean.valueOf(tag.toString())){ //disable clicking so the user cannot select another image on same row ((ImageView)gridView.getChildAt(x)).setClickable(false); ((ImageView)gridView.getChildAt(x)).setEnabled(false); Log.i(TAG, "This is : "+ x + tag); } } break; case 2: for(int x=4; x &lt; 8; x++){ // Object tag = ((ImageView)gridView.getChildAt(x)).getTag(); if(tag ==null || !Boolean.valueOf(tag.toString())){ //disable clicking so the user cannot select another image on same row ((ImageView)gridView.getChildAt(x)).setClickable(false); ((ImageView)gridView.getChildAt(x)).setEnabled(false); } } break; case 3: for(int x=8; x &lt; 12; x++){ // Object tag = ((ImageView)gridView.getChildAt(x)).getTag(); if(tag ==null || !Boolean.valueOf(tag.toString())){ //disable clicking so the user cannot select another image on same row ((ImageView)gridView.getChildAt(x)).setClickable(false); ((ImageView)gridView.getChildAt(x)).setEnabled(false); } } break; case 4: for(int x=12; x &lt; 16; x++){ // Object tag = ((ImageView)gridView.getChildAt(x)).getTag(); if(tag ==null || !Boolean.valueOf(tag.toString())){ //disable clicking so the user cannot select another image on same row ((ImageView)gridView.getChildAt(x)).setClickable(false); ((ImageView)gridView.getChildAt(x)).setEnabled(false); } } break; } </code></pre> <p>private int getRowInGrid(int position){</p> <pre><code> if(position &lt;= 3){ return 1; } else if(position &lt;=7){ return 2; } else if(position &lt;= 11){ return 3; } else if(position &lt;= 15){ return 4; } else{ return -1;} } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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