我需要将newiconid[0]的值从方法arg2更改为onItemSelected()。我需要在onClick()中使用该值。以下代码不起作用,只是将newiconid[0]的值保留为0。如何做到这一点?

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

最佳答案

您可以尝试将newiconid设为班级成员吗?然后,在两个范围内都应该可以访问它。是的,正如@ Agent404所说,请不要将其设置为final

10-08 01:22