0
        TextView countTV = mView.findViewById(R.id.main_left_count);
        final TextView totalTV = mView.findViewById(R.id.main_left_total);
        orderGoodsAdapter = new OrderGoodsAdapter(mContext,mView){
            @Override
            public void onRefreshBottom(int len,int total) {
                super.onRefreshBottom(len,total);
                countTV.setText(String.valueOf(len));
                totalTV.setText(String.valueOf(total));
            }
        };


this code will show error "android java access from inner class, needs to declared final"

if you set the variable final, or set variable as a class field, the error will gone.

what's different for class field and final keyword?

lee
  • 151
  • 1
  • 6

1 Answers1

0

You should code it like this:

public class TestActivity extends Activity {

    private OrderGoodsAdapter orderGoodsAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        final TextView countTV = findViewById(R.id.main_left_count);
        final TextView totalTV = findViewById(R.id.main_left_total);
        orderGoodsAdapter = new OrderGoodsAdapter(TestActivity.this, mView){
            @Override
            public void onRefreshBottom(int len,int total) {
                super.onRefreshBottom(len,total);
                countTV.setText(String.valueOf(len));
                totalTV.setText(String.valueOf(total));
            }
        };
    }
}

or you should do this

public class TestActivity extends Activity {

    private OrderGoodsAdapter orderGoodsAdapter;
    private TextView countTV;
    private TextView totalTV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        countTV = findViewById(R.id.main_left_count);
        totalTV = findViewById(R.id.main_left_total);
        orderGoodsAdapter = new OrderGoodsAdapter(TestActivity.this, mView){
            @Override
            public void onRefreshBottom(int len,int total) {
                super.onRefreshBottom(len,total);
                countTV.setText(String.valueOf(len));
                totalTV.setText(String.valueOf(total));
            }
        };
    }
}

You can check this for more explanation: SO link

RKRK
  • 1,284
  • 5
  • 14
  • 18