简体   繁体   中英

How do i change the value of an object from an inner method?

I need to change the value of newiconid[0] to arg2 from method onItemSelected() . And I need to use that value in onClick() . The following code does not work, it just keeps the value of newiconid[0] as 0 . How to accomplish this?

@Override
public void onClick(DialogInterface dialog,
        int which) {
    final int newiconid[] = new int[1];
    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(
                AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
            // TODO Auto-generated method stub
            System.out.println("" + arg1 + arg2
                    + arg3);
            newiconid[0] = arg2;
        }

        @Override
        public void onNothingSelected(
                AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });
    // TODO Auto-generated method stub
    System.out.println("updateicon "+newiconid[0]+" "+itemid);
    datasource.updateVSIcon(newiconid[0], itemid);
}

You've made it a FINAL , Final's are constants and DO NOT CHANGE once they've be initialized. Try taking final out first and Let us know what happens.

Can you try making newiconid a class member instead? It should then be accessible in both scopes. And yes, as @Agent404 said, do not make it final

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM