title: Retrieve information from Firebase element clicked in ListView
body: <p>I am making an app where Firebase is used to store and retrieve data. When I retrieve data for the DB I have it displayed in a ListView in a fragment. I can then click on a given element in the list and it opens a details fragment.
  Right now, I can only display data from a predetermend node from the DB, but I want to display data from the element I have clicked on. To do this I need to have the nodes unique key extracted and the parsed to the other fragments, so data from the chosen node may be displayed. But I do not know how to do this? Can you help me?</p>
  
  <p>Here is a picture of my DB structure:</p>
  
  <p><a href="https://i.stack.imgur.com/A7izX.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/A7izX.jpg" alt="enter image description here"></a></p>
  
  <p>"Song" is the first child in the DB and it is the -L-*****... keys I want to extract.</p>
  
  <p>Here is the code for the SongData element which is the one send to and retrieved from the data base:</p>
  
  <pre><code>    public class SongData implements Serializable {
  
  private String songTitel;
  private String songArtist;
  private String songLanguage;
  private String songCode;
  private String intentAction;
  private int resultCode;
  
      public SongData(){ }
  
  public SongData(String songTitel, String songArtist){
  
      this.songTitel = songTitel;
      this.songArtist = songArtist;
  }
  
  public SongData(String songTitel, String songArtist, String songLanguages, String songCode){
  
      this.songTitel = songTitel;
      this.songArtist = songArtist;
      this.songLanguage = songLanguages;
      this.songCode = songCode;
  }
  
  public String getSongTitel() {
      return songTitel;
  }
  public void setSongTitel(String songTitel) {
      this.songTitel = songTitel;
  }
  
  public String getSongArtist() {
      return songArtist;
  }
  public void setSongArtist(String songArtist) {
      this.songArtist = songArtist;
  }
  
  public String getSongLanguage() {
      return songLanguage;
  }
  public void setSongLanguage(String songLanguage) {
      this.songLanguage = songLanguage;
  }
  
  public String getSongCode() {
      return songCode;
  }
  public void setSongCode(String songCode) {
      this.songCode = songCode;
  }
  
  public String getIntentAction() {
      return intentAction;
  }
  
  public int getResultCode() {
      return resultCode;
  }
  
  @Override
  public String toString() {
      return "SONG TITLE :" + this.songTitel + "\n SONG ARTIST :" + 
      this.songArtist;
  }
      }
  </code></pre>
  
  <p>Here the code from the activity that displays data in a ListView:</p>
  
  <pre><code>    public class SearchFragment extends Fragment {
  // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
  private static final String ARG_SONG_TITLE = "song title";
  private static final String ARG_SECTION_ID = "section_id";
  
  private String mSongTitle; //mParam1
  private Button btnSearch;
  private TextView txtViewSearch;
  private EditText editTextSearch;
  private ListView listViewSearch;
  private SearchAdaptor searchAdaptor;
  
  //For storing list of songs from DB
  private ArrayList&lt;String&gt; songArrayList = new ArrayList&lt;&gt;();
  
  private OnFragmentSearchInteractionListener mListener;
  
  public SearchFragment() {
      // Required empty public constructor
  }
  
  /**
   * Use this factory method to create a new instance of
   * this fragment using the provided parameters.
   *
   * @param songTitle Parameter 1.
   * @return A new instance of fragment SearchFragment.
   */
  // TODO: Rename and change types and number of parameters
  public static SearchFragment newInstance(String songTitle) {
      SearchFragment fragment = new SearchFragment();
      Bundle args = new Bundle();
      args.putString(ARG_SONG_TITLE, songTitle);
      fragment.setArguments(args);
      return fragment;
  }
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      if (getArguments() != null) {
          mSongTitle = getArguments().getString(ARG_SONG_TITLE);
      }
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      // Inflate the layout for this fragment
      View view = inflater.inflate(R.layout.fragment_search, container, false);
  
      //Find elements from view
      btnSearch = (Button) view.findViewById(R.id.btnSearch);
      txtViewSearch = (TextView) view.findViewById(R.id.txtViewSearch);
      editTextSearch = (EditText) view.findViewById(R.id.editTxtSearch);
      listViewSearch = (ListView) view.findViewById(R.id.listViewSearch);
  
      //Array adapter for Array List with songs
     final ArrayAdapter&lt;String&gt; arrayAdapter = new ArrayAdapter&lt;String&gt;(getActivity(), android.R.layout.simple_list_item_1, songArrayList);
     listViewSearch.setAdapter(arrayAdapter);
  
      FirebaseDatabase database = FirebaseDatabase.getInstance();
      DatabaseReference songRef = database.getReference("Song");
  
      songRef.addChildEventListener(new ChildEventListener() {
          @Override
          public void onChildAdded(DataSnapshot dataSnapshot, String s) {
  
              SongData song = (SongData) dataSnapshot.getValue(SongData.class);
              String songString = String.valueOf(song);
              songArrayList.add(songString);
              arrayAdapter.notifyDataSetChanged();
  
          }
  
          @Override
          public void onChildChanged(DataSnapshot dataSnapshot, String s) {
                  //Required
          }
  
          @Override
          public void onChildRemoved(DataSnapshot dataSnapshot) {
  
              songArrayList.remove(dataSnapshot.getValue(SongData.class));
              arrayAdapter.notifyDataSetChanged();
  
          }
  
          @Override
          public void onChildMoved(DataSnapshot dataSnapshot, String s) {
                  //Required
          }
  
          @Override
          public void onCancelled(DatabaseError databaseError) {
                  //Required
          }
      });
  
      listViewSearch.setOnItemClickListener(new AdapterView.OnItemClickListener() {
          @Override
          public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long Id) {
              //position is the index of the item, counts from 0
              //Reference: https://stackoverflow.com/questions/21028786/how-do-i-open-a-new-fragment-from-another-fragment
              SongDetailsFragment songDetailsFragment = new SongDetailsFragment();
              getActivity().getSupportFragmentManager().beginTransaction()
                       .replace(R.id.fragment_placeholder, songDetailsFragment,"findThisFragment")
                       .addToBackStack(null)
                       .commit();
          }
      });
  
      return view;
  }
  
  @Override
  public void onResume() {
      super.onResume();
  
      // Refresh the state of the +1 button each time the activity receives focus.
      /////btnSearch.initialize(PLUS_ONE_URL, PLUS_ONE_REQUEST_CODE);
  }
  
  // TODO: Rename method, update argument and hook method into UI event
  public void onButtonPressed(Uri uri) {
      if (mListener != null) {
          mListener.onFragmentInteraction(uri);
      }
  }
  
  @Override
  public void onAttach(Context context) {
      super.onAttach(context);
      if (context instanceof OnFragmentSearchInteractionListener) {
          mListener = (OnFragmentSearchInteractionListener) context;
      } else {
          throw new RuntimeException(context.toString()
                  + " must implement OnFragmentSearchInteractionListener");
      }
  }
  
  @Override
  public void onDetach() {
      super.onDetach();
      mListener = null;
  }
  
  /**
   * This interface must be implemented by activities that contain this
   * fragment to allow an interaction in this fragment to be communicated
   * to the activity and potentially other fragments contained in that
   * activity.
   * &lt;p&gt;
   * See the Android Training lesson &lt;a href=
   * "http://developer.android.com/training/basics/fragments/communicating.html"
   * &gt;Communicating with Other Fragments&lt;/a&gt; for more information.
   */
  public interface OnFragmentSearchInteractionListener {
      // TODO: Update argument type and name
      void onFragmentInteraction(Uri uri);
  }
  
      }
  </code></pre>
  
  <p>Code for the fragment that displays details:</p>
  
  <pre><code>    public class SongDetailsFragment extends Fragment {
  // TODO: Rename parameter arguments, choose names that match
  // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
  private static final String ARG_PARAM1 = "param1";
  private static final String ARG_PARAM2 = "param2";
  // The request code must be 0 or greater.
  private static final int PLUS_ONE_REQUEST_CODE = 0;
  // The URL to +1.  Must be a valid URL.
  private final String PLUS_ONE_URL = "http://developer.android.com";
  // TODO: Rename and change types of parameters
  private String mParam1;
  private String mParam2;
  private PlusOneButton mPlusOneButton;
  
  private OnSongDetailsFragmentInteractionListener mListener;
  
  private TextView txtViewShowSongTitel;
  private TextView txtViewShowSongArtist;
  private TextView txtViewShowSongLanguage;
  private TextView txtViewShowSongCode;
  
  public SongDetailsFragment() {
      // Required empty public constructor
  }
  
  /**
   * Use this factory method to create a new instance of
   * this fragment using the provided parameters.
   *
   * @param param1 Parameter 1
   * @return A new instance of fragment SongDetailsFragment.
   */
  // TODO: Rename and change types and number of parameters
  public static SongDetailsFragment newInstance(String param1) {
      SongDetailsFragment fragment = new SongDetailsFragment();
      Bundle args = new Bundle();
      args.putString(ARG_PARAM1, param1);
      fragment.setArguments(args);
      return fragment;
  }
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      if (getArguments() != null) {
          mParam1 = getArguments().getString(ARG_PARAM1);
          mParam2 = getArguments().getString(ARG_PARAM2);
      }
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
      // Inflate the layout for this fragment
      View view = inflater.inflate(R.layout.fragment_song_details, container, false);
  
      final TextView txtViewShowSongTitel = (TextView) view.findViewById(R.id.TxtViewShowSongTitel);
      final TextView txtViewShowSongArtist = (TextView) view.findViewById(R.id.TxtViewShowSongArtist);
      final TextView txtViewShowSongLanguage = (TextView) view.findViewById(R.id.TxtViewShowSongLanguage);
      final TextView txtViewShowSongCode = (TextView) view.findViewById(R.id.TxtViewShowSongCode);
  
      // Reference: https://firebase.google.com/docs/database/android/read-and-write
      //Retrieve data for the Firebase Database
      FirebaseDatabase database = FirebaseDatabase.getInstance();
      DatabaseReference songRef = database.getReference("Song").child("-L-qBD0S742-EQkVvers");
  
      songRef.addValueEventListener(new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot dataSnapshot) {
  
              SongData s = dataSnapshot.getValue(SongData.class);
  
              txtViewShowSongTitel.setText(s.getSongTitel());
              txtViewShowSongArtist.setText(s.getSongArtist());
              txtViewShowSongLanguage.setText(s.getSongLanguage());
              txtViewShowSongCode.setText(s.getSongCode());
          }
  
          @Override
          public void onCancelled(DatabaseError databaseError) {
  
              // Getting songData failed, log a message
              Log.w(TAG, "loadSong:onCancelled", databaseError.toException());
          }
      });
  
      Button btnOK = (Button) view.findViewById(R.id.btnOK);
      btnOK.setOnClickListener(new View.OnClickListener() {
          public void onClick(View v) {
  
              //Close SongDetailsFragment and return to previous fragment
             getActivity().onBackPressed();
          }
  
      });
  
      return view;
  }
  
  @Override
  public void onResume() {
      super.onResume();
  
      // Refresh the state of the +1 button each time the activity receives focus.
      //mPlusOneButton.initialize(PLUS_ONE_URL, PLUS_ONE_REQUEST_CODE);
  }
  
  // TODO: Rename method, update argument and hook method into UI event
  public void onButtonPressed(Uri uri) {
      if (mListener != null) {
          mListener.onFragmentInteraction(uri);
      }
  }
  
  /*@Override
  public void onAttach(Context context) {
      super.onAttach(context);
      if (context instanceof OnSongDetailsFragmentInteractionListener) {
          mListener = (OnSongDetailsFragmentInteractionListener) context;
      } else {
          throw new RuntimeException(context.toString()
                  + " must implement OnSongDetailsFragmentInteractionListener");
      }
  }*/
  
  @Override
  public void onDetach() {
      super.onDetach();
      mListener = null;
  }
  
  /**
   * This interface must be implemented by activities that contain this
   * fragment to allow an interaction in this fragment to be communicated
   * to the activity and potentially other fragments contained in that
   * activity.
   * &lt;p&gt;
   * See the Android Training lesson &lt;a href=
   * "http://developer.android.com/training/basics/fragments/communicating.html"
   * &gt;Communicating with Other Fragments&lt;/a&gt; for more information.
   */
  public interface OnSongDetailsFragmentInteractionListener {
      // TODO: Update argument type and name
      void onFragmentInteraction(Uri uri);
  }
  
      }
  </code></pre>
  
  <p>I hope someone in here can help me!</p>