-1

Maybe this question is asked before but I am not getting how to resolve this issue,so when I use Youtube API in my application under Firebase RecyclerView I got this error that "Variable 'youtuber' is accessed from within inner class, needs to be declared final" and it wont go unless I use final keyword, so due to this I am unable to get data from my database because the value cant be changed to String as it gets fixed by using "Final", can anyone help me with this so that I can use the youtuber String without declaring it final.

I have tried using the Global variable but that also does not work here, Please help.

I am using Android Studio.

Youtube activity code:

public class YoutubeVideos extends AppCompatActivity {

    private RecyclerView YouRecycler;
    private DatabaseReference YDatabaseReference;
    public static String DEVELOPER_KEY = "AIzaSyAEK3iPoxfItaHJsiuJ_693tuuQjkdGfQk";


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.youtube_videos);
        YDatabaseReference = FirebaseDatabase.getInstance().getReference().child("you");
        YouRecycler = (RecyclerView)findViewById(R.id.Youtube_recycler);
        YouRecycler.setHasFixedSize(true);
        YouRecycler.setLayoutManager(new LinearLayoutManager(this));


    }

    @Override
    public void onStart() {
        super.onStart();

      final FirebaseRecyclerAdapter<post2youtube, youtubeViewHolder> youFirebaseAdapter = new FirebaseRecyclerAdapter<post2youtube, youtubeViewHolder>(
                post2youtube.class,
                R.layout.youtube_card_view,
                youtubeViewHolder.class,
                YDatabaseReference
        ) {
            @Override
            protected void populateViewHolder(youtubeViewHolder yviewHolder, post2youtube ymodel, int position) {
                yviewHolder.setYoutube(ymodel.getYoutube());

            }
        };

        YouRecycler.setAdapter(youFirebaseAdapter);
    }

    public static class youtubeViewHolder extends RecyclerView.ViewHolder {
        View youView;

        public youtubeViewHolder(View itemView) {
            super(itemView);
            youView = itemView;
        }
        public void setYoutube( String youtuber){
        YouTubeThumbnailView youPlay = (YouTubeThumbnailView)youView.findViewById(R.id.youtubler);
                youPlay.initialize(DEVELOPER_KEY, new YouTubeThumbnailView.OnInitializedListener() {
                    @Override
                    public void onInitializationSuccess(YouTubeThumbnailView youTubeThumbnailView, final YouTubeThumbnailLoader youTubeThumbnailLoader) {
                        youTubeThumbnailLoader.setVideo(youtuber);
                        youTubeThumbnailLoader.setOnThumbnailLoadedListener(new YouTubeThumbnailLoader.OnThumbnailLoadedListener() {
                            @Override
                            public void onThumbnailLoaded(YouTubeThumbnailView youTubeThumbnailView, String s) {
                                youTubeThumbnailLoader.release();
                            }

                            @Override
                            public void onThumbnailError(YouTubeThumbnailView youTubeThumbnailView, YouTubeThumbnailLoader.ErrorReason errorReason) {

                            }
                        });
                    }

                    @Override
                    public void onInitializationFailure(YouTubeThumbnailView youTubeThumbnailView, YouTubeInitializationResult youTubeInitializationResult) {

                    }
                });

            }


        }
rohit b
  • 75
  • 13
  • 3
    Possible duplicate of [Why are only final variables accessible in anonymous class?](https://stackoverflow.com/questions/4732544/why-are-only-final-variables-accessible-in-anonymous-class) – Pavneet_Singh Oct 30 '17 at 07:11
  • @Pavneet_Singh how is it duplicate!! , i am trying to find a solution veere – rohit b Oct 30 '17 at 07:56
  • you can only use static variable in static context so make your global variable static , initialize it in oncreate or i don't know why you have any issue with global variable , you shouldn't have and the duplicate link contains most of the optimal solution to your issue – Pavneet_Singh Oct 30 '17 at 08:07
  • @Pavneet_Singh making global variable static does not help :( as i am getting the string from getter and setter class maybe thats why – rohit b Oct 30 '17 at 08:33
  • `I am unable to get data from my database because the value cant be changed to String as it gets fixed by using "Final",` but how? , when ever `setYoutube` is called the value of `youtuber` can't be changed if it is `final` and i can't see why you need to change inside `setYoutube` function, just debug your code , see the values which are coming from database and in `setYoutube` function – Pavneet_Singh Oct 30 '17 at 10:09

1 Answers1

0

Add the DEVELOPER_KEY in string.xml and get it anywhere you want using In strings.xml

<string name="developer_key">AIzaSyAEK3iPoxfItaHJsiuJ_693tuuQjkdGfQk</string>

In your .Java files,

String mystring = getResources().getString(R.string.DEVELOPER_KEY);