Harish Raaj
Harish Raaj

Reputation: 45

method not called in fragment

I am relatively new to java and would appreciate some help. I am trying to use a method(handleSelection method) in a fragment but the method does not seem to work and instead it is highlighted that the method is not used.

public class TrendingFragment extends Fragment {



    Song selectedSong;

    public void handleSelection(View view)
    {
        String resourceId = AppUtil.getResourceId(getActivity(),view);

        selectedSong = songCollection.searchById(resourceId);

        AppUtil.popMessage(getActivity(), "Streaming song: " + selectedSong.getTitle());

        sendDataToActivity(selectedSong);
    }

    public void sendDataToActivity (Song song)
    {
        Intent intent = new Intent(getActivity(), PlaySongActivity.class);

        intent.putExtra("id", song.getId());
        intent.putExtra("title", song.getTitle());
        intent.putExtra("artist", song.getartist());
        intent.putExtra("fileLink" ,song.getFileLink());
        intent.putExtra("coverArt", song.getCoverArt());

        startActivity(intent);
    }




    private SongCollection songCollection = new SongCollection();
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)

    {
        View v = inflater.inflate(R.layout.fragment_trending, container, false);


        return v;
    }

}

Upvotes: 0

Views: 125

Answers (1)

Dominik Wuttke
Dominik Wuttke

Reputation: 555

Based on the Comments you Need to implement the functions for the button. I assume the Button is inside the view of the Fragment. This should be your oncreateview function.

View v = inflater.inflate(R.layout.fragment_trending, container, false);
button = findViewById(R.id...):
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                handleSelection(view);

            }
        });

return v;

you Need to implement a button variable inside your Fragment and enter the correct id for your button

Upvotes: 1

Related Questions