#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2016
    Posts
    3
    Rep Power
    0

    Trying to access imageViews that reside in a TableLayout - Android


    I have a TableLayout which has 3 TableRows and within the rows there are 3 ImageViews.
    What I am trying to do is iterate through the table and setImageResource for each ImageView
    in the table. I have tried using this code, but I am getting stuck with this problem. I am a beginner
    at Android, any help is greatly appreciated, thank you. The code i used is as follows...
    Code:
    TableLayout tableLayout = (TableLayout) findViewById(R.id.tableLayout);
    TableRow tableRow = (TableRow)tableLayout.getChildAt(0);
    ImageView imageView = (ImageView)tableRow.getChildAt(0);
    
    
    int x = tableLayout.getChildCount();
    for(int i = 0;  i < x; i++){
    tableRow= (TableRow)tableLayout.getChildAt(i);
    for( int j = 0; j < 3; j++){
    imageView = (ImageView)tableRow.getChildAt(j);
    imageView.setImageResource(0);
    }
    }
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2016
    Posts
    3
    Rep Power
    0
    Addon, this code does not produce an error, all it does is setImageResource for the 3 ImageViews for the first row only. I need it to setImageResource for all imageViews in the table , 9 total. 3x3 table
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2016
    Posts
    3
    Rep Power
    0
    Problem solved...
    Code:
    TableLayout tableLayout = (TableLayout)findViewById(R.id.tableLayout);
    
            for(int i = 0; i < 3; i++){
              TableRow  tableRow = (TableRow)tableLayout.getChildAt(i);
                for(int j = 0; j < 3; j++){
                    ImageView imageView = (ImageView)tableRow.getChildAt(j);
                    imageView.setImageResource(0);
                }
            }

IMN logo majestic logo threadwatch logo seochat tools logo