Firebase Changing Layout of Child Data Information in Android

Trying to replace a data from specific child (Firebase Database)

When you do this

btnSetLimit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

findId();

String newLimit = etLimit.getText().toString().trim();
db.child(kidKey).child("kidLimit").setValue(newLimit);
}
});

findId(); is executed but you don't know when it will finish, so after that method is executed and waiting for data, this line

db.child(kidKey).child("kidLimit").setValue(newLimit);

will have kidKey with a null value because it has not been pulled yet from the database, so instead, you should move your code to the onDataChange() or make a new callback when all the asynchronous process finishes

btnSetLimit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

findId();
}
});

public void findId(){

String kidName = kidSpinner.getSelectedItem().toString();
String newLimit = etLimit.getText().toString().trim();

db = FirebaseDatabase.getInstance().getReference().child("kids");
db.orderByChild("kidName").equalTo(kidName).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot dataSnapshot1:dataSnapshot.getChildren()){
kidKey = dataSnapshot1.getKey();
}

db.child(kidKey).child("kidLimit").setValue(newLimit);

}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});

How to update multiple firebase child values based on each child autogeneratedID

The main problem in your code is, you tried to call the arraylist in the adapter from the activity. You cant do that. So what you have to do is, create an interface which will update the arraylist in the activity when a change is made in the recycler view.
Heres how to achieve it:

First, create an interface like this:

public interface RecyclerItemClick {

public void onItemClick(int position, String newValue);
}

This will be called when a user makes changes in edit text.

add implements RecyclerItemClick, in your activity and overide the onItemClick() function.

Declare recycler view and adapter outside the database function , pass RecyclerItemClick object into the adapter. Then call customAdapter.notifyDataSetChanged(); after getting data from database.

 customAdapter = new UpdateStudentRecyclerAdapter(AddStudent.this,editModelArrayList, this);
recyclerView.setAdapter(customAdapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.VERTICAL, false));
StudentdatabaseReference.child(GroupId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
editModelArrayList = new ArrayList<AddStudentModel>();
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
AddStudentModel value = dataSnapshot1.getValue(AddStudentModel.class);
AddStudentModel fire = new AddStudentModel();
String name = value.getStudentName();
String id = value.getStudentID();
fire.setStudentName(name);
fire.setStudentID(id);
editModelArrayList.add(fire);
}
customAdapter.notifyDataSetChanged();

}

@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w("Hello", "Failed to read value.", error.toException());
}
});

Then in the adapter,

 public class UpdateStudentRecyclerAdapter extends 
RecyclerView.Adapter<UpdateStudentRecyclerAdapter.MyViewHolder> {

private LayoutInflater inflater;
RecyclerItemClick itemClick;
public static ArrayList<AddStudentModel> editModelArrayList;

public UpdateStudentRecyclerAdapter(Context ctx, ArrayList<AddStudentModel>
editModelArrayList,RecyclerItemClick itemClick){

inflater = LayoutInflater.from(ctx);
this.editModelArrayList = editModelArrayList;
this.itemClick = itemClick;
}

@Override
public UpdateStudentRecyclerAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

View view = inflater.inflate(R.layout.add_student_rv_item, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}

@Override
public void onBindViewHolder(final UpdateStudentRecyclerAdapter.MyViewHolder holder, final int position) {
AddStudentModel mylist = editModelArrayList.get(position);
holder.textView.setText(mylist.getStudentID());
holder.editText.setText(mylist.getStudentName());

}

@Override
public int getItemCount() {
return editModelArrayList.size();
}

class MyViewHolder extends RecyclerView.ViewHolder{

protected EditText editText;
protected TextView textView;

public MyViewHolder(View itemView) {
super(itemView);
editText = (EditText) itemView.findViewById(R.id.editid);
textView = (TextView) itemView.findViewById(R.id.id_view);
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

}
@Override
public void afterTextChanged(Editable editable) {
itemClick.onItemClick(getAdapterPosition(), editText.getText().toString());
}
});

}

}

Now whenever a change is made in the editText, the interface function in activity will be called. Now in the overide onItemClick() function , update the arraylist like this:

@override
public void onItemClick(int position, String newValue){
editModelArrayList.get(position).setEditTextValue(newValue); // i assume, this is where u store updated value.
}

Now, on update.setOnClickListener() :

update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
for (int i = 0; i < editModelArrayList.size(); i++){
AddStudentModel mylist = editModelArrayList.get(i);

String ky =mylist.getStudentID();
StudentdatabaseReference.child(GroupId).child(ky).child("name").setValue(mylist.getEditTextValue());
} }});

I think this will work. Try it.

How to stay on top child in Firebase Database Android Studio

Child nodes in the Firebase console are shown in alphabetical order. There is no way to change this.

To control the order of child nodes in your application code, you can use the orderBy... methods shown in the documentation on sorting data.

how to make changes in a multiple child nodes of my database firebase with android

To be able to update multiple nodes, you need to know the exact path of each node.

It seems you already have a reference to a specific user. In that case, you will need to read the personas, loop over them, and then update each in turn. Something like this:

Map<String,Object> update= new HashMap<>();
update.put("latitud",lati);
update.put("longitud",longit);
update.put("dirCoordenadas",dir);
databaseReference.child("Personas").addListenerForSingleValueEvent(new ValueEventListener() {
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot child: snapshot.getChildren()) {
child.getRef().updateChildren(update);
}
}
...
)

How to change the value of child of an unknown parent in Firebase - Android

To solve this, you need to use a query. So please use the following lines of code:

DatabaseReference messageRef = mRootRef.child("messages").child(mCurrentUserId).child(mChatUser);
Query query = messageRef.orderByChild("seen").equalTo(false);
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()) {
ds.child("seen").getRef().setValue(true);
}
}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d(TAG, databaseError.getMessage()); //Don't ignore errors!
}
};
query.addListenerForSingleValueEvent(valueEventListener);

The result will the change of your seen property from false to true.

If you also consider at some point to try using Cloud Firestore, here you can find a tutorial on how to create a complete and functional Firestore Chat App using Kotlin.

How do I display child of child's data from firebase in my RecyclerView?

First of all, you need to create 2 loops since your json looks like that and store them inside an arrayList. You are suppose to get all the data there inside the arrayList.

FirebaseDatabase.getInstance().getReference().child("Category_wise").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
ArrayList<Scard> sCardList = new ArrayList<Scard>();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
if (snapshot.exists()){
for (DataSnapshot shot : snapshot.getChildren()){
if (shot.exists()){
final Scard scard = shot.getValue(Scard.class);
if (scard != null){
sCardList.add(scard);
}
}
}
}
}
final ScardAdapter scardAdapter = new ScardAdapter(sCardList);
recyclerView.setAdapter(scardAdapter);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});

Next you need to create your own adapter. You can search on Google also for more information. But you will get the point here.

public class ScardAdapter extends RecyclerView.Adapter<ScardViewHolder> {
private ArrayList<Scard> sCardList;
public ScardAdapter(final ArrayList<Scard> sCardList) {
this.sCardList = sCardList;
}
@NonNull
@Override
public ScardViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.scard_item, parent, false);
return new ScardViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ScardViewHolder holder, final int position) {
final Scard model = sCardList.get(position);
holder.getTvCompanyName.setText(model.getCompanyName);
}
@Override
public int getItemCount() {
return sCardList.size();
}
}


Related Topics



Leave a reply



Submit