访问匿名内部类中的非final值

时间:2017-08-21 05:01:29

标签: java android inner-classes anonymous-inner-class

我有这5个Edittext字段,我想使用按钮在数据库中写入这些值。对于按钮Clicklistener,我使用了匿名内部类,但只允许最终值,这意味着我将在数据库中写入时获得空字段。我怎样才能将这些字段作为非最终字段访问?有什么办法吗?

 public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    EditText yardarea = (EditText)view.findViewById(R.id.yardarea);
    String areaofyard = yardarea.getText().toString();
    EditText brick = (EditText)view.findViewById(R.id.brick);
    final String brickused = brick.getText().toString();
    EditText rcc = (EditText)view.findViewById(R.id.rcc);
    final String rccused = rcc.getText().toString();
    EditText overhead = (EditText)view.findViewById(R.id.overhead);
    final String ovhh = overhead.getText().toString();
    EditText underground = (EditText)view.findViewById(R.id.underground);
    final String ung = underground.getText().toString();

    Button button = (Button) view.findViewById(R.id.register);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DatabaseAccess databaseAccess = DatabaseAccess.getInstance(getActivity().getApplicationContext());
            databaseAccess.open();
            databaseAccess.insertIntoCriteriaTable(areaofyard, brickused, rccused, ovhh, ung );
            databaseAccess.close();
        }
    });
}

4 个答案:

答案 0 :(得分:2)

public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    final EditText yardarea = (EditText)view.findViewById(R.id.yardarea);
    final EditText brick = (EditText)view.findViewById(R.id.brick);
    final EditText rcc = (EditText)view.findViewById(R.id.rcc);
    final EditText overhead = (EditText)view.findViewById(R.id.overhead);
    final EditText underground = (EditText)view.findViewById(R.id.underground);

    Button button = (Button) view.findViewById(R.id.register);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String areaofyard = yardarea.getText().toString();
            String brickused = brick.getText().toString();
            String rccused = rcc.getText().toString();
            String ovhh = overhead.getText().toString();
            String ung = underground.getText().toString();

            DatabaseAccess databaseAccess = DatabaseAccess.getInstance(getActivity().getApplicationContext());
            databaseAccess.open();
            databaseAccess.insertIntoCriteriaTable(areaofyard, brickused, rccused, ovhh, ung );
            databaseAccess.close();

        }
    });
}

您可以将EditText对象声明为final,也可以将它们声明为全局变量,并在按钮的onclick上使用上面发布的相同代码。

答案 1 :(得分:2)

尝试将EditText声明为全局

EditText yardarea , brick , rcc,  overhead , underground;

public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    yardarea = (EditText)view.findViewById(R.id.yardarea);
     brick = (EditText)view.findViewById(R.id.brick);
    rcc = (EditText)view.findViewById(R.id.rcc);
    overhead = (EditText)view.findViewById(R.id.overhead);
    underground = (EditText)view.findViewById(R.id.underground);

    Button button = (Button) view.findViewById(R.id.register);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String areaofyard = yardarea.getText().toString();
            String brickused = brick.getText().toString();
            String rccused = rcc.getText().toString();
            String ovhh = overhead.getText().toString();
            String ung = underground.getText().toString();

            DatabaseAccess databaseAccess = DatabaseAccess.getInstance(getActivity().getApplicationContext());
            databaseAccess.open();
            databaseAccess.insertIntoCriteriaTable(areaofyard, brickused, rccused, ovhh, ung );
            databaseAccess.close();

        }
    });
}

答案 2 :(得分:0)

如何将字符串放在onClickListener中呢?

  

public void onViewCreated(View view,@ Nullable Bundle   savedInstanceState){           super.onViewCreated(view,savedInstanceState);

    EditText yardarea = (EditText)view.findViewById(R.id.yardarea);





    EditText brick = (EditText)view.findViewById(R.id.brick);



    EditText rcc = (EditText)view.findViewById(R.id.rcc);



    EditText overhead = (EditText)view.findViewById(R.id.overhead);




    EditText underground = (EditText)view.findViewById(R.id.underground);




    Button button = (Button) view.findViewById(R.id.register);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String areaofyard = yardarea.getText().toString();
            String brickused = brick.getText().toString();
            String rccused = rcc.getText().toString();
            String ovhh = overhead.getText().toString();
            String ung = underground.getText().toString();
            DatabaseAccess databaseAccess = DatabaseAccess.getInstance(getActivity().getApplicationContext());
            databaseAccess.open();
            databaseAccess.insertIntoCriteriaTable(areaofyard, brickused, rccused, ovhh, ung );
            databaseAccess.close();

        }
    });



}

答案 3 :(得分:0)

 You should get the value from edittext inside onClick 


 final EditText yardarea = (EditText)view.findViewById(R.id.yardarea);
 final EditText brick = (EditText)view.findViewById(R.id.brick);
 final EditText rcc = (EditText)view.findViewById(R.id.rcc);
 final EditText overhead = (EditText)view.findViewById(R.id.overhead);
 final EditText underground = (EditText)view.findViewById(R.id.underground);


 button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
              String brickused = brick.getText().toString();
              String rccused = rcc.getText().toString();
              String ovhh = overhead.getText().toString();
              String areaofyard = yardarea.getText().toString();
              String ung = underground.getText().toString();                  
              // Do your database operation with these value
            }
        });