Search code examples
javaandroidobjectandroid-linearlayout

"Cannot resolve method" unable to access object methods inside a loop (passing object reference to function)


I'm new to Android and JAVA,... my Activity have many widgets that are inside separated LinearLayouts. I want to toggle them on and off accordingly. I have some radio buttons which will hide or show certain LinearLayouts as required. It makes more sense to me having the widgets grouped as sections for this case.

I'm having a problem accessing the methods "getVisibility" and "setVisibility" from inside a "for" loop. I'm using an array of Object type. I thought in maybe just pass the layouts id's as strings but something tells me it will not work.

As a side question : I have 13 total Linear Layouts in a single activity is it bad practice? I could not find a better way to horizontal align elements, maybe I took the short route? :p Thanks in advance.

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new_job);

  ...

    LinearLayout monthlyContainer = (LinearLayout) findViewById(R.id.monthlyContainer);
    LinearLayout weeklyContainer = (LinearLayout) findViewById(R.id.weeklyContainer);
    LinearLayout fortnightlyContainer = (LinearLayout) findViewById(R.id.fortnightlyContainer);
    LinearLayout fourweeklyContainer = (LinearLayout) findViewById(R.id.fourweeklyContainer);
    LinearLayout twiceMonthlyContainer = (LinearLayout) findViewById(R.id.twiceMonthlyContainer);
    RadioGroup earningsContainer = (RadioGroup) findViewById(R.id.earningsContainer);

    ///create a list of views to hide
    Object[] viewsToToggle={monthlyContainer, weeklyContainer, fortnightlyContainer, fourweeklyContainer, twiceMonthlyContainer, earningsContainer};



    //pass object array to hideView method
    this.hideView(viewsToToggle, monthlyContainer);

  ....

}



private void hideView(Object[] viewsToToggle, Object excludeMe){

    ///// create array to contain elements to toggle visibility


    Object[] viewstoHide = new Object[4];


    for(int i = 0; i < viewsToToggle.length; i++){

        if(viewsToToggle[i] != excludeMe){

            viewstoHide[i] =  viewsToToggle[i];
        }


    }


    for(int j = 0; j < viewstoHide.length; j++ ){

        if(viewstoHide[j].getVisibility() == View.VISIBLE){ //// syntax error on this line
            viewstoHide[j].setVisibility(View.GONE); //// syntax error on this line
        }


    }




    System.out.println("VIEWS TO HIDE : " + Arrays.toString(viewstoHide));



}

Solution

  • Your viewstoHide array does not contain ui elements, so you cannot use those methods. In order to use those methods, change the type of your array or add ui elements like LinearLayout or Radiogroup.

    It's not a very good practice to add different types of ojects in your array because when you loop through the array of objects and you if want a to use a specific method on all elements, you can have only one different element and your loop will break. So in order to solve your problem, please use specific type for each array or add only ui elements. Good preactice is to verify if your elements are instanceof desired classes. Hope it helps.