Update current list itemHow to get specific pushedID in Firebase?Get current time and date on AndroidLoadmanager onLoadFinished not calledsetText on button from another activity androidMoshi's Custom Adapter with RxAndroid & Retrofit & KotlinAdding Social Media Share Logic From Firebase in AndroidUpdated global variables does not reflect inside ValueEventListener's onCancelled methodApp crash when use mobie data (3g/4g)Search Firestore query don't show data in RecycleViewRetrofit 2 - Getting response 200, but list is emptyHow to add child(Product) under a child(Store) in Firebase Database using RecyclerView PLEASE

Why does AES have exactly 10 rounds for a 128-bit key, 12 for 192 bits and 14 for a 256-bit key size?

Why is short-wave infrared portion of electromagnetic spectrum so sensitive to fire?

Plot of a tornado-shaped surface

How do apertures which seem too large to physically fit work?

When were female captains banned from Starfleet?

How can mimic phobia be cured?

Does IPv6 have similar concept of network mask?

Open a doc from terminal, but not by its name

Why is it that I can sometimes guess the next note?

Did arcade monitors have same pixel aspect ratio as TV sets?

What is going on with 'gets(stdin)' on the site coderbyte?

User Story breakdown - Technical Task + User Feature

PTIJ: Haman's bad computer

Strong empirical falsification of quantum mechanics based on vacuum energy density?

What to do when eye contact makes your subordinate uncomfortable?

Does an advisor owe his/her student anything? Will an advisor keep a PhD student only out of pity?

Does malloc reserve more space while allocating memory?

Invalid date error by date command

Quoting Keynes in a lecture

What is Cash Advance APR?

Does Doodling or Improvising on the Piano Have Any Benefits?

What if you are holding an Iron Flask with a demon inside and walk into Antimagic Field?

Unexpected behavior of the procedure `Area` on the object 'Polygon'

Is this toilet slogan correct usage of the English language?



Update current list item


How to get specific pushedID in Firebase?Get current time and date on AndroidLoadmanager onLoadFinished not calledsetText on button from another activity androidMoshi's Custom Adapter with RxAndroid & Retrofit & KotlinAdding Social Media Share Logic From Firebase in AndroidUpdated global variables does not reflect inside ValueEventListener's onCancelled methodApp crash when use mobie data (3g/4g)Search Firestore query don't show data in RecycleViewRetrofit 2 - Getting response 200, but list is emptyHow to add child(Product) under a child(Store) in Firebase Database using RecyclerView PLEASE













1















I open the list item, get the data in fetchData(), then I expect that by calling the addTarget() method I will update the current item(name and description). Instead, I create a new one.



Q: How can I update the current one?



enter image description here



class TargetEditFragment : Fragment() 

private var nameEditText: TextInputEditText? = null
private var descriptionEditText: TextInputEditText? = null
private var button: Button? = null
private var databaseReference: DatabaseReference? = null

override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
arguments?.getString(KEY_TARGET_GUID, "")


override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
return inflater.inflate(R.layout.fragment_target_add, container, false)


override fun onViewCreated(view: View, savedInstanceState: Bundle?)
super.onViewCreated(view, savedInstanceState)
databaseReference = FirebaseDatabase.getInstance().getReference("targets")
setupViews()
fetchData(guid = arguments?.getString(KEY_TARGET_GUID, "") ?: "")


private fun setupViews()
nameEditText = view?.findViewById(R.id.nameEditText)
descriptionEditText = view?.findViewById(R.id.descriptionEditText)

button = view?.findViewById(R.id.addNote)
button?.setOnClickListener addTarget()


private fun addTarget()
val name = nameEditText?.text.toString().trim()
val description = descriptionEditText?.text.toString().trim()

if (!TextUtils.isEmpty(name))
val id: String = databaseReference?.push()?.key.toString()
val target = Target(guid = id, name = name, description = description)
databaseReference?.child(id)?.setValue(target)
else Log.d("some", "Enter a name")


private fun fetchData(guid: String)
// Attach a listener to read the data at the target id
databaseReference?.child(guid)?.addValueEventListener(object : ValueEventListener
override fun onDataChange(dataSnapshot: DataSnapshot)
val data = dataSnapshot.value as HashMap<String, String>
val name = data["name"] ?: ""
val description = data["description"] ?: ""

if (name.isEmpty()) Log.d("some", "nameIsEmpty")
else
updateViewsContent(name = name, description = description)



override fun onCancelled(p0: DatabaseError)
Log.d("some", "onCancelled")

)


private fun updateViewsContent(name: String?, description: String?)
nameEditText?.text = Editable.Factory.getInstance().newEditable(name)
descriptionEditText?.text = Editable.Factory.getInstance().newEditable(description)


companion object

fun newInstance(guid: String): TargetEditFragment =
TargetEditFragment().apply
arguments = Bundle().apply putString(KEY_TARGET_GUID, guid)












share|improve this question




























    1















    I open the list item, get the data in fetchData(), then I expect that by calling the addTarget() method I will update the current item(name and description). Instead, I create a new one.



    Q: How can I update the current one?



    enter image description here



    class TargetEditFragment : Fragment() 

    private var nameEditText: TextInputEditText? = null
    private var descriptionEditText: TextInputEditText? = null
    private var button: Button? = null
    private var databaseReference: DatabaseReference? = null

    override fun onCreate(savedInstanceState: Bundle?)
    super.onCreate(savedInstanceState)
    arguments?.getString(KEY_TARGET_GUID, "")


    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
    return inflater.inflate(R.layout.fragment_target_add, container, false)


    override fun onViewCreated(view: View, savedInstanceState: Bundle?)
    super.onViewCreated(view, savedInstanceState)
    databaseReference = FirebaseDatabase.getInstance().getReference("targets")
    setupViews()
    fetchData(guid = arguments?.getString(KEY_TARGET_GUID, "") ?: "")


    private fun setupViews()
    nameEditText = view?.findViewById(R.id.nameEditText)
    descriptionEditText = view?.findViewById(R.id.descriptionEditText)

    button = view?.findViewById(R.id.addNote)
    button?.setOnClickListener addTarget()


    private fun addTarget()
    val name = nameEditText?.text.toString().trim()
    val description = descriptionEditText?.text.toString().trim()

    if (!TextUtils.isEmpty(name))
    val id: String = databaseReference?.push()?.key.toString()
    val target = Target(guid = id, name = name, description = description)
    databaseReference?.child(id)?.setValue(target)
    else Log.d("some", "Enter a name")


    private fun fetchData(guid: String)
    // Attach a listener to read the data at the target id
    databaseReference?.child(guid)?.addValueEventListener(object : ValueEventListener
    override fun onDataChange(dataSnapshot: DataSnapshot)
    val data = dataSnapshot.value as HashMap<String, String>
    val name = data["name"] ?: ""
    val description = data["description"] ?: ""

    if (name.isEmpty()) Log.d("some", "nameIsEmpty")
    else
    updateViewsContent(name = name, description = description)



    override fun onCancelled(p0: DatabaseError)
    Log.d("some", "onCancelled")

    )


    private fun updateViewsContent(name: String?, description: String?)
    nameEditText?.text = Editable.Factory.getInstance().newEditable(name)
    descriptionEditText?.text = Editable.Factory.getInstance().newEditable(description)


    companion object

    fun newInstance(guid: String): TargetEditFragment =
    TargetEditFragment().apply
    arguments = Bundle().apply putString(KEY_TARGET_GUID, guid)












    share|improve this question


























      1












      1








      1








      I open the list item, get the data in fetchData(), then I expect that by calling the addTarget() method I will update the current item(name and description). Instead, I create a new one.



      Q: How can I update the current one?



      enter image description here



      class TargetEditFragment : Fragment() 

      private var nameEditText: TextInputEditText? = null
      private var descriptionEditText: TextInputEditText? = null
      private var button: Button? = null
      private var databaseReference: DatabaseReference? = null

      override fun onCreate(savedInstanceState: Bundle?)
      super.onCreate(savedInstanceState)
      arguments?.getString(KEY_TARGET_GUID, "")


      override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
      return inflater.inflate(R.layout.fragment_target_add, container, false)


      override fun onViewCreated(view: View, savedInstanceState: Bundle?)
      super.onViewCreated(view, savedInstanceState)
      databaseReference = FirebaseDatabase.getInstance().getReference("targets")
      setupViews()
      fetchData(guid = arguments?.getString(KEY_TARGET_GUID, "") ?: "")


      private fun setupViews()
      nameEditText = view?.findViewById(R.id.nameEditText)
      descriptionEditText = view?.findViewById(R.id.descriptionEditText)

      button = view?.findViewById(R.id.addNote)
      button?.setOnClickListener addTarget()


      private fun addTarget()
      val name = nameEditText?.text.toString().trim()
      val description = descriptionEditText?.text.toString().trim()

      if (!TextUtils.isEmpty(name))
      val id: String = databaseReference?.push()?.key.toString()
      val target = Target(guid = id, name = name, description = description)
      databaseReference?.child(id)?.setValue(target)
      else Log.d("some", "Enter a name")


      private fun fetchData(guid: String)
      // Attach a listener to read the data at the target id
      databaseReference?.child(guid)?.addValueEventListener(object : ValueEventListener
      override fun onDataChange(dataSnapshot: DataSnapshot)
      val data = dataSnapshot.value as HashMap<String, String>
      val name = data["name"] ?: ""
      val description = data["description"] ?: ""

      if (name.isEmpty()) Log.d("some", "nameIsEmpty")
      else
      updateViewsContent(name = name, description = description)



      override fun onCancelled(p0: DatabaseError)
      Log.d("some", "onCancelled")

      )


      private fun updateViewsContent(name: String?, description: String?)
      nameEditText?.text = Editable.Factory.getInstance().newEditable(name)
      descriptionEditText?.text = Editable.Factory.getInstance().newEditable(description)


      companion object

      fun newInstance(guid: String): TargetEditFragment =
      TargetEditFragment().apply
      arguments = Bundle().apply putString(KEY_TARGET_GUID, guid)












      share|improve this question
















      I open the list item, get the data in fetchData(), then I expect that by calling the addTarget() method I will update the current item(name and description). Instead, I create a new one.



      Q: How can I update the current one?



      enter image description here



      class TargetEditFragment : Fragment() 

      private var nameEditText: TextInputEditText? = null
      private var descriptionEditText: TextInputEditText? = null
      private var button: Button? = null
      private var databaseReference: DatabaseReference? = null

      override fun onCreate(savedInstanceState: Bundle?)
      super.onCreate(savedInstanceState)
      arguments?.getString(KEY_TARGET_GUID, "")


      override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View?
      return inflater.inflate(R.layout.fragment_target_add, container, false)


      override fun onViewCreated(view: View, savedInstanceState: Bundle?)
      super.onViewCreated(view, savedInstanceState)
      databaseReference = FirebaseDatabase.getInstance().getReference("targets")
      setupViews()
      fetchData(guid = arguments?.getString(KEY_TARGET_GUID, "") ?: "")


      private fun setupViews()
      nameEditText = view?.findViewById(R.id.nameEditText)
      descriptionEditText = view?.findViewById(R.id.descriptionEditText)

      button = view?.findViewById(R.id.addNote)
      button?.setOnClickListener addTarget()


      private fun addTarget()
      val name = nameEditText?.text.toString().trim()
      val description = descriptionEditText?.text.toString().trim()

      if (!TextUtils.isEmpty(name))
      val id: String = databaseReference?.push()?.key.toString()
      val target = Target(guid = id, name = name, description = description)
      databaseReference?.child(id)?.setValue(target)
      else Log.d("some", "Enter a name")


      private fun fetchData(guid: String)
      // Attach a listener to read the data at the target id
      databaseReference?.child(guid)?.addValueEventListener(object : ValueEventListener
      override fun onDataChange(dataSnapshot: DataSnapshot)
      val data = dataSnapshot.value as HashMap<String, String>
      val name = data["name"] ?: ""
      val description = data["description"] ?: ""

      if (name.isEmpty()) Log.d("some", "nameIsEmpty")
      else
      updateViewsContent(name = name, description = description)



      override fun onCancelled(p0: DatabaseError)
      Log.d("some", "onCancelled")

      )


      private fun updateViewsContent(name: String?, description: String?)
      nameEditText?.text = Editable.Factory.getInstance().newEditable(name)
      descriptionEditText?.text = Editable.Factory.getInstance().newEditable(description)


      companion object

      fun newInstance(guid: String): TargetEditFragment =
      TargetEditFragment().apply
      arguments = Bundle().apply putString(KEY_TARGET_GUID, guid)









      android firebase firebase-realtime-database kotlin






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited yesterday







      Morozov

















      asked yesterday









      MorozovMorozov

      1,1631828




      1,1631828






















          1 Answer
          1






          active

          oldest

          votes


















          1














          There is no way to update an element using the push() method because everytime you call this method a new unique key is generated. In order to perform an update you need to know the key of the element you want to update and use it in your reference. For more informations, please see my answer from the following post:



          • How to get specific pushedID in Firebase?





          share|improve this answer























          • Not clear( I go into a specific note, id I know. If I knowing the id why i can t to update the data for this id?

            – Morozov
            yesterday






          • 1





            In order to create an update you most likely should use updateChildren() method an pass a Map as an argument. And your code should look like this: databaseReference.child("-LaQq-QDnLJHWjR3ru4R").updateChildren(map) (be sure to have the exact key as in the database). Does it work this way?

            – Alex Mamo
            yesterday











          • I updated the question. The structure is visible in the image. It turns out I will need to do the update in the fetchData() or after?

            – Morozov
            yesterday











          • You are still using push(). There is no way you can update an element using it. Have you tried to use the hardcoded key in the reference? Does it work?

            – Alex Mamo
            yesterday












          • I doesn t understand where in code i need to use it?

            – Morozov
            yesterday










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55280678%2fupdate-current-list-item%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          There is no way to update an element using the push() method because everytime you call this method a new unique key is generated. In order to perform an update you need to know the key of the element you want to update and use it in your reference. For more informations, please see my answer from the following post:



          • How to get specific pushedID in Firebase?





          share|improve this answer























          • Not clear( I go into a specific note, id I know. If I knowing the id why i can t to update the data for this id?

            – Morozov
            yesterday






          • 1





            In order to create an update you most likely should use updateChildren() method an pass a Map as an argument. And your code should look like this: databaseReference.child("-LaQq-QDnLJHWjR3ru4R").updateChildren(map) (be sure to have the exact key as in the database). Does it work this way?

            – Alex Mamo
            yesterday











          • I updated the question. The structure is visible in the image. It turns out I will need to do the update in the fetchData() or after?

            – Morozov
            yesterday











          • You are still using push(). There is no way you can update an element using it. Have you tried to use the hardcoded key in the reference? Does it work?

            – Alex Mamo
            yesterday












          • I doesn t understand where in code i need to use it?

            – Morozov
            yesterday















          1














          There is no way to update an element using the push() method because everytime you call this method a new unique key is generated. In order to perform an update you need to know the key of the element you want to update and use it in your reference. For more informations, please see my answer from the following post:



          • How to get specific pushedID in Firebase?





          share|improve this answer























          • Not clear( I go into a specific note, id I know. If I knowing the id why i can t to update the data for this id?

            – Morozov
            yesterday






          • 1





            In order to create an update you most likely should use updateChildren() method an pass a Map as an argument. And your code should look like this: databaseReference.child("-LaQq-QDnLJHWjR3ru4R").updateChildren(map) (be sure to have the exact key as in the database). Does it work this way?

            – Alex Mamo
            yesterday











          • I updated the question. The structure is visible in the image. It turns out I will need to do the update in the fetchData() or after?

            – Morozov
            yesterday











          • You are still using push(). There is no way you can update an element using it. Have you tried to use the hardcoded key in the reference? Does it work?

            – Alex Mamo
            yesterday












          • I doesn t understand where in code i need to use it?

            – Morozov
            yesterday













          1












          1








          1







          There is no way to update an element using the push() method because everytime you call this method a new unique key is generated. In order to perform an update you need to know the key of the element you want to update and use it in your reference. For more informations, please see my answer from the following post:



          • How to get specific pushedID in Firebase?





          share|improve this answer













          There is no way to update an element using the push() method because everytime you call this method a new unique key is generated. In order to perform an update you need to know the key of the element you want to update and use it in your reference. For more informations, please see my answer from the following post:



          • How to get specific pushedID in Firebase?






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered yesterday









          Alex MamoAlex Mamo

          46.1k82965




          46.1k82965












          • Not clear( I go into a specific note, id I know. If I knowing the id why i can t to update the data for this id?

            – Morozov
            yesterday






          • 1





            In order to create an update you most likely should use updateChildren() method an pass a Map as an argument. And your code should look like this: databaseReference.child("-LaQq-QDnLJHWjR3ru4R").updateChildren(map) (be sure to have the exact key as in the database). Does it work this way?

            – Alex Mamo
            yesterday











          • I updated the question. The structure is visible in the image. It turns out I will need to do the update in the fetchData() or after?

            – Morozov
            yesterday











          • You are still using push(). There is no way you can update an element using it. Have you tried to use the hardcoded key in the reference? Does it work?

            – Alex Mamo
            yesterday












          • I doesn t understand where in code i need to use it?

            – Morozov
            yesterday

















          • Not clear( I go into a specific note, id I know. If I knowing the id why i can t to update the data for this id?

            – Morozov
            yesterday






          • 1





            In order to create an update you most likely should use updateChildren() method an pass a Map as an argument. And your code should look like this: databaseReference.child("-LaQq-QDnLJHWjR3ru4R").updateChildren(map) (be sure to have the exact key as in the database). Does it work this way?

            – Alex Mamo
            yesterday











          • I updated the question. The structure is visible in the image. It turns out I will need to do the update in the fetchData() or after?

            – Morozov
            yesterday











          • You are still using push(). There is no way you can update an element using it. Have you tried to use the hardcoded key in the reference? Does it work?

            – Alex Mamo
            yesterday












          • I doesn t understand where in code i need to use it?

            – Morozov
            yesterday
















          Not clear( I go into a specific note, id I know. If I knowing the id why i can t to update the data for this id?

          – Morozov
          yesterday





          Not clear( I go into a specific note, id I know. If I knowing the id why i can t to update the data for this id?

          – Morozov
          yesterday




          1




          1





          In order to create an update you most likely should use updateChildren() method an pass a Map as an argument. And your code should look like this: databaseReference.child("-LaQq-QDnLJHWjR3ru4R").updateChildren(map) (be sure to have the exact key as in the database). Does it work this way?

          – Alex Mamo
          yesterday





          In order to create an update you most likely should use updateChildren() method an pass a Map as an argument. And your code should look like this: databaseReference.child("-LaQq-QDnLJHWjR3ru4R").updateChildren(map) (be sure to have the exact key as in the database). Does it work this way?

          – Alex Mamo
          yesterday













          I updated the question. The structure is visible in the image. It turns out I will need to do the update in the fetchData() or after?

          – Morozov
          yesterday





          I updated the question. The structure is visible in the image. It turns out I will need to do the update in the fetchData() or after?

          – Morozov
          yesterday













          You are still using push(). There is no way you can update an element using it. Have you tried to use the hardcoded key in the reference? Does it work?

          – Alex Mamo
          yesterday






          You are still using push(). There is no way you can update an element using it. Have you tried to use the hardcoded key in the reference? Does it work?

          – Alex Mamo
          yesterday














          I doesn t understand where in code i need to use it?

          – Morozov
          yesterday





          I doesn t understand where in code i need to use it?

          – Morozov
          yesterday



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55280678%2fupdate-current-list-item%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript