在内部类中引用非final变量来检测android中的onClick

时间:2014-08-17 11:58:44

标签: android android-linearlayout

我在做什么 ::我已动态生成视图,如下所示,我正在尝试检测视图的onclick(linearInner

我收到错误 :: onClick linearInner我收到错误

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

我该如何解决这个问题!


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);
        }
    }

1 个答案:

答案 0 :(得分:2)

您可以根据错误提示将linearInner设为最终。或者,您可以使用单击侦听器接收的视图参数,在这种情况下,您正在尝试操作该视图。所以它会是:

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));

    }
});