In ListAdapter I cant update data Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of In ListAdapter I cant update data without wasting too much if your time.

The question is published on by Tutorial Guruji team.

When I clicked confirmation button, all of datas are updated.what I want is to update that clicked line not all of them. enter image description hereenter image description here

 DatabaseReference read=database.getReference("Posts").child(user.getDisplayName());
 public void myClickHandler(View v) {
    read.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot edtData : dataSnapshot.getChildren()) {

                String i=edtData.child("completed").getValue().toString();
                if (i!="true"){
                    edtData.getRef().child("completed").setValue("true");
                }
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    });
}

Answer

If you change your data structure like this , it can be more useful for you.

@IgnoreExtraProperties
public class User {

public String id;
public String hastaneAdi;
public String hastaninAdi;
public String kanGrubu;
public String sehir;
public Boolean completed;


public User() {
    // Default constructor required for calls to DataSnapshot.getValue(User.class)
}

public User(String id, String hastaneAdi, String hastaninAdi, String kanGrubu, String sehir, Boolean completed) {
    this.id = id;
    this.hastaneAdi = hastaneAdi;
    this.hastaninAdi = hastaninAdi;
    this.kanGrubu = kanGrubu;
    this.sehir = sehir;
    this.completed = completed;
}

public String getId() {
    return id;
}

public void setId(String id) {
    this.id = id;
}

public String getHastaneAdi() {
    return hastaneAdi;
}

public void setHastaneAdi(String hastaneAdi) {
    this.hastaneAdi = hastaneAdi;
}

public String getHastaninAdi() {
    return hastaninAdi;
}

public void setHastaninAdi(String hastaninAdi) {
    this.hastaninAdi = hastaninAdi;
}

public String getKanGrubu() {
    return kanGrubu;
}

public void setKanGrubu(String kanGrubu) {
    this.kanGrubu = kanGrubu;
}

public String getSehir() {
    return sehir;
}

public void setSehir(String sehir) {
    this.sehir = sehir;
}

public Boolean getCompleted() {
    return completed;
}

public void setCompleted(Boolean completed) {
    this.completed = completed;
}
}

You can add new value like this:

 DatabaseReference write =database.getReference("Posts").child(user.getDisplayName());
 String id = write.push().getKey();
 User user = new User(id, "123", "123",....);
 write.child(id).setValue(user);

You can update value like this:

 DatabaseReference update =database.getReference("Posts").child(user.getDisplayName());
 update.child(yourlist.get(position).getId()).child("completed").setValue(true);

You don’t need valueListener any more

We are here to answer your question about In ListAdapter I cant update data - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji