Devrath
Devrath

Reputation: 42854

Referring non-final variable inside an inner class for detecting onClick in android

What i am doing:: I have generated views dynamically as below, i am trying to detect onclick for a view (linearInner)

Error I am getting :: In onClick for linearInner i am getting error as

Cannot refer to a non-final variable linearInner inside an inner class defined in a different method

How can i resolve this !


private void buildMenuTable() {
        ImageView imageView = null;
        TableRow table_row = null;
        LinearLayout linMain=null;
        LinearLayout linearOuter=null;
        LinearLayout linearInner=null;
        LinearLayout linearTxtOpaque=null;
        LinkedList<LinkedHashMap<String, String>> menuLst=new LinkedList<LinkedHashMap<String,String>>();
        LinkedHashMap<String, String> menuMap=new LinkedHashMap<String,String>();

        boolean selection=false;

        //Function call to get menu names from database
        menuLst=createDrawerMenuList();

        for(int menuCnt=0;menuCnt<menuLst.size();menuCnt++){

            //Create the tableRow and add params to it
            table_row = new TableRow(getApplicationContext());
            table_row.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.MATCH_PARENT));

            menuMap=menuLst.get(menuCnt);

            int colCnt=1;

            for (Entry<String, String> entry : menuMap.entrySet()) {  

                //Get the object from the Hashmap
                String imgIcon = "";
                String txtTitle = "";   

                imgIcon=entry.getKey();        
                txtTitle=entry.getValue();


                linMain=new LinearLayout(getApplicationContext());
                linMain.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.MATCH_PARENT));
                linMain.setOrientation(LinearLayout.VERTICAL);
                linMain.setBackgroundColor(getApplicationContext().getResources().getColor(R.color.black));

                if(colCnt==2){
                    linMain.setPadding(5, 0, 5, 0);
                }else if(menuMap.size()==1){
                    linMain.setPadding(5, 0, 5, 5);
                }else{
                    linMain.setPadding(5, 0, 0, 5);
                }

                linearOuter=new LinearLayout(getApplicationContext());
                linearOuter.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.WRAP_CONTENT));
                linearOuter.setOrientation(LinearLayout.VERTICAL);
                linearOuter.setBackgroundColor(getApplicationContext().getResources().getColor(R.color.cGold));

                linearTxtOpaque=new LinearLayout(getApplicationContext());
                linearTxtOpaque.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.WRAP_CONTENT));
                linearTxtOpaque.setOrientation(LinearLayout.VERTICAL);
                linearTxtOpaque.setBackgroundColor(getApplicationContext().getResources().getColor(R.color.black));
                linearTxtOpaque.setAlpha((float) 0.4);

                linearInner=new LinearLayout(getApplicationContext());
                linearInner.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.WRAP_CONTENT));
                linearInner.setOrientation(LinearLayout.VERTICAL);
                linearInner.setBackgroundColor(getApplicationContext().getResources().getColor(R.color.whiteColor));
                linearInner.setPadding(2, 2, 2, 2);

                linearInner.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        //Toast.makeText(getApplicationContext(), ""+textView.getText()+"", Toast.LENGTH_LONG).show();

                        linearInner.setBackgroundColor(getApplicationContext().getResources().getColor(R.color.whiteColor));

                        linearInner.setBackgroundColor(getApplicationContext().getResources().getColor(R.color.cBlue));

                    }
                });

                //Create the ImageView and add params to it
                imageView = new ImageView(getApplicationContext());
                imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
                imageView.setImageResource(R.drawable.ic_friends);
                imageView.setScaleType(ImageView.ScaleType.CENTER);

                //Create the TextView and add params to it
                final TextView textView = new TextView(getApplicationContext());
                textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
                textView.setText(txtTitle);
                textView.setPadding(1,1,1,1);
                textView.setTextSize(12);
                //textView.setBackgroundColor(getApplicationContext().getResources().getColor(R.color.silverColor));
                textView.setTextColor(getApplicationContext().getResources().getColor(R.color.cGoldText));



                if(menuMap.size()==1){
                    TableRow.LayoutParams aLp = (TableRow.LayoutParams) linMain.getLayoutParams();
                    aLp.span = 2;
                    linMain.setLayoutParams(aLp);   
                }

                table_row.addView(linMain);
                linMain.addView(linearInner);
                linearInner.addView(linearOuter);
                linearOuter.addView(imageView);
                linearTxtOpaque.addView(textView);
                linearOuter.addView(linearTxtOpaque);
                colCnt++;
            }
            tableLayout.addView(table_row);
        }
    }

Upvotes: 0

Views: 134

Answers (1)

Oren
Oren

Reputation: 4252

You could make linearInner final as the error suggests. Or alternatively you could use the view parameter your click listener receives, which in this case is exactly the view you are trying to manipulate. So it would be:

linearInner.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        //Toast.makeText(getApplicationContext(), ""+textView.getText()+"", Toast.LENGTH_LONG).show();

        v.setBackgroundColor(getApplicationContext().getResources().getColor(R.color.whiteColor));

        v.setBackgroundColor(getApplicationContext().getResources().getColor(R.color.cBlue));

    }
});

Upvotes: 2

Related Questions