How to Update data on Firebase Realtime Database
How to Update data on Firebase Realtime Database
To update Data we need to use on item click listener on our ListView which we created in our last post.
When we click on the ListView item, it will return us a unique id that is created on our database at the time of the creation of a node. then on behalf of that unique specific node id, we will update the value in the database.
Also, we create an Alert Dialog box to update our value.
MainActivity.java
package studio.harpreet.firebasedatabase;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
DatabaseReference databasestudent;
EditText roll_et,name_et,std_et;
Button add_btn;
ListView studentListView;
List<StudentModel > studentlist;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
roll_et = findViewById(R.id.student_roll);
name_et = findViewById(R.id.student_name);
std_et = findViewById(R.id.student_std);
add_btn = findViewById(R.id.add_btn);
studentListView = findViewById(R.id.student_listview);
studentlist = new ArrayList<>();
databasestudent = FirebaseDatabase.getInstance().getReference("Student");
add_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addstudent(roll_et.getText().toString().trim(),
name_et.getText().toString().trim(),
std_et.getText().toString().trim());
}
});
studentListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
final StudentModel studentModel = studentlist.get(position);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
LinearLayout layout = new LinearLayout(MainActivity.this);
layout.setOrientation(LinearLayout.VERTICAL);
final EditText name_et = new EditText(MainActivity.this);
layout.addView(name_et);
name_et.setHint("Name");
builder.setTitle("Update Name")
.setMessage("Write name to Update")
.setView(layout)
.setPositiveButton("Update", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Student").child(studentModel.getId());
StudentModel studentModel1 = new StudentModel(studentModel.getId(),studentModel.getRollno(),
name_et.getText().toString().trim(),studentModel.getStd());
reference.setValue(studentModel1);
}
}).show();
}
});
}
private void addstudent(String Rollno, String Name, String Std)
{
String id = databasestudent.push().getKey();
StudentModel studentModel = new StudentModel(id,Rollno,Name,Std);
databasestudent.child(id).setValue(studentModel);
}
@Override
protected void onStart() {
super.onStart();
databasestudent.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
studentlist.clear(); // because everytime when data updates in your firebase database it creates the list with updated items
// so to avoid duplicate fields we clear the list everytime
if(dataSnapshot.exists())
{
for(DataSnapshot studentsnapshot : dataSnapshot.getChildren())
{
StudentModel studentModel = studentsnapshot.getValue(StudentModel.class);
studentlist.add(studentModel);
}
Student_ListAdaptor adaptor = new Student_ListAdaptor(MainActivity.this,studentlist);
studentListView.setAdapter(adaptor);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
It will successfully update data in our Firebase Realtime Database and show it on a ListView
Subscribe to Harpreet studio on Youtube
Like Harpreet Studio on Facebook
Follow me on Instagram
No comments