简体   繁体   中英

Variable needs to be declared as final but it needs to be declared as non-final too

I have a case that my variable needs to be accessed as a final but it needs to be as non-final too.

Here's my code:

public class ShowViewModel extends ViewModel {
    public void setShows(final String type, boolean isFavorite, FavoriteHelper favoriteHelper) {
        AsyncHttpClient client = new AsyncHttpClient();
        ArrayList<Show> listItems = new ArrayList<>();
        listItems.clear();

        if (isFavorite) {
            Cursor cursor = favoriteHelper.queryAll(type);

            // this assignment needs listItems as a non-final variable
            listItems = MappingHelper.mapCursorToArrayList(cursor);
        } else {
            String url = String.format("http://api.dev");

            client.get(url, new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                    try {
                        String result = new String(responseBody);
                        JSONObject responseObject = new JSONObject(result);
                        JSONArray results = responseObject.getJSONArray("results");

                        for (int i = 0; i < results.length(); i++) {
                            JSONObject shows = results.getJSONObject(i);
                            Show show = new Show();
                            show.setImage(shows.getString("poster_path"));
                            ....
                            // this assignment needs listItems as a final variable
                            listItems.add(show);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }
}

Then, I tried to move the listItems as property of the class

public calss ShowViewModel extends ViewModel {
    private ArrayList<Show> listItems = new ArrayList<>();
    public void setShows(final String type, boolean isFavorite, FavoriteHelper favoriteHelper) {
        ...
    }
}

It doesn't show any error and worked perfectly. But it affect another part of my application.

Any solution? Thank you.

The normal solution is to use two variables. For example:

int i = 0;
while (i < 10) {
   i++;
   executor.submit(new Runnable() {
           public void run() {
                System.out.println(i);  // Compilation Error
           });
}

becomes:

int i = 0;
while (i < 10) {
   i++;
   final int ii = i;
   executor.submit(new Runnable() {
           public void run() {
                 System.out.println(ii); // OK
           });
}

You should be able use this approach in your code.

Here's a simple solution:

List<Show> cursorList = MappingHelper.mapCursorToArrayList(cursor);
listItems.addAll(cursorList);

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