Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ONCREATE SLIDE 26
- FirebaseApp.initializeApp(this);
- //Create database reference
- databaseArtists = FirebaseDatabase.getInstance().getReference("artists");
- //get values from XML
- editTextName = (EditText) findViewById(R.id.editTextName);
- buttonAdd = (Button) findViewById(R.id.buttonAddArtist);
- //listViewArtists = (ListView) findViewById(R.id.ListViewArtist);
- //artistList = new ArrayList<>();
- //attach clicklistener to the button
- buttonAdd.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- addArtist();
- } });
- Add artist slide 27
- private void addArtist() {
- //get artistname and convert to string from editextname
- String name = editTextName.getText().toString().trim();
- //check if the name is not empty
- if (!TextUtils.isEmpty(name)) {
- //if exist push data to firebase database
- //store inside id in database
- //every time data stored the id will be unique
- String id = databaseArtists.push().getKey();
- //store
- Artist artist = new Artist(id, name);
- //store artist inside unique id
- databaseArtists.child(id).setValue(artist);
- Toast.makeText(this, "Artist added", Toast.LENGTH_LONG).show();
- } else {
- //if the name is empty
- //if the value is not given displaying a toast
- Toast.makeText(this, "Please enter a name", Toast.LENGTH_LONG).show();
- }
- }
- Artist list slide 38
- public class ArtistList extends ArrayAdapter<Artist> {
- private Activity context;
- List<Artist> artists;
- //Create constructor
- public ArtistList(Activity context, List<Artist> artists) {
- super(context, R.layout.list_layout, artists);
- this.context = context;
- this.artists = artists;
- }
- @Override
- public View getView(int position, View convertView, ViewGroup parent) {
- LayoutInflater inflater = context.getLayoutInflater();
- View listViewItem = inflater.inflate(R.layout.list_layout, null, true);
- TextView textViewName = (TextView) listViewItem.findViewById(R.id.textViewName);
- Artist artist = artists.get(position);
- textViewName.setText(artist.getArtistName());
- return listViewItem;
- }
- }
- onstart method
- @Override
- protected void onStart() {
- super.onStart();
- //attaching value event listener
- databaseArtists.addValueEventListener(new ValueEventListener() {
- @Override
- public void onDataChange(DataSnapshot dataSnapshot) {
- //clearing the previous artist list
- artistList.clear();
- //iterating through all the nodes
- for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
- //getting artist
- Artist artist = postSnapshot.getValue(Artist.class);
- //adding artist to the list
- artistList.add(artist);
- }
- //creating adapter
- ArtistList artistAdapter = new ArtistList(MainActivity.this, artistList);
- //attaching adapter to the listview
- listViewArtists.setAdapter(artistAdapter);
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement