Firebase for android – Insert value as a list data

Home  >>  Tutorials  >>  Android  >>  Firebase for android – Insert value as a list data

Firebase for android – Insert value as a list data

17
Feb,2018

0

Part : one –

Go to firebase console and click database . If you want to insert database without authentication then go to database->rules tab and

{
 "rules": {
 ".read": "auth == null",
 ".write": "auth == null"
 }
}

Part 2 :

Create Model . I used Notes Model

/**
 * Created by ADN Digital on 12/24/2017.
 */

public class Notes {

public String id;
 public String title;
 public String description;
 public String date;
 public String day;
 public String currentDate;


 public Notes() {
 }


 public String getId() {
 return id;
 }

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

public String getTitle() {
 return title;
 }

public void setTitle(String title) {
 this.title = title;
 }

public String getDescription() {
 return description;
 }

public void setDescription(String description) {
 this.description = description;
 }

public String getDate() {
 return date;
 }

public void setDate(String date) {
 this.date = date;
 }

public String getDay() {
 return day;
 }

public void setDay(String day) {
 this.day = day;
 }

public String getCurrentDate() {
 return currentDate;
 }

public void setCurrentDate(String currentDate) {
 this.currentDate = currentDate;
 }

@Override
 public String toString() {
 return "Notes{" +
 "id='" + id + '\'' +
 ", title='" + title + '\'' +
 ", description='" + description + '\'' +
 ", date='" + date + '\'' +
 ", day='" + day + '\'' +
 ", currentDate='" + currentDate + '\'' +
 '}';
 }
}




Part 3 :

CreateNote activity . I have take 2 edit text named title and description and a button createNote ;
and paste this code.

public class CreateNote extends AppCompatActivity {

private EditText title, description;

 private Button btnDone;

private String titleText, descriptionText, dateText, timeText;

Context mContext;
 private DatabaseReference mDatabase;

@Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_create_note);
 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
 setSupportActionBar(toolbar);


 getSupportActionBar().setDisplayHomeAsUpEnabled(true);
 getSupportActionBar().setDisplayShowHomeEnabled(true);
 getSupportActionBar().setTitle("New Note");


 mContext = CreateNote.this;
 title = findViewById(R.id.title_et);
 description = findViewById(R.id.description_et);
btnDone = findViewById(R.id.done);


 final FirebaseAuth auth = FirebaseAuth.getInstance();


 mDatabase = FirebaseDatabase.getInstance().getReference();
 final DatabaseReference noteNode = mDatabase.child("notes").child(auth.getCurrentUser().getPhoneNumber().toString());
 final DatabaseReference noteNode1 = mDatabase.child("notes");

btnDone.setOnClickListener(new View.OnClickListener() {
 @Override
 public void onClick(View view) {

 titleText = title.getText().toString().trim());




 descriptionText = description.getText().toString().trim());


if (titleText.length()<=0 || descriptionText.length()<=0 ) {
 Toast.makeText(mContext, "Title or description is empty , Please enter title and description .", Toast.LENGTH_SHORT).show();
 } else {
 // titleText = title.getText().toString();
 // descriptionText = description.getText().toString();


 Notes notes = new Notes();
 notes.id = noteNode1.getKey();
 notes.title = titleText;
 notes.description = descriptionText;
 notes.date = String.valueOf(datePicker.getDayOfMonth() + 1) + "/" + String.valueOf(datePicker.getMonth() + 1) + "/" + String.valueOf(datePicker.getYear());

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
 notes.day = String.valueOf(timePicker.getHour()) + ":" + String.valueOf(timePicker.getMinute());
 } else {
 notes.day = String.valueOf(timePicker.getCurrentHour()) + ":" + String.valueOf(timePicker.getCurrentMinute());

}

String timeStamp = new SimpleDateFormat("yyyy.MM.dd HH:mm").format(Calendar.getInstance().getTime());
 notes.currentDate = timeStamp;
 noteNode.push().setValue(notes);


 Intent intent = new Intent(CreateNote.this, MainActivity.class);
 startActivity(intent);
 }
 }
 });


 }

Leave a Reply

Your email address will not be published. Required fields are marked *