SHARE
TWEET

Untitled

a guest Apr 18th, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ONCREATE SLIDE 26
  2. FirebaseApp.initializeApp(this);
  3.  
  4.         //Create database reference
  5.         databaseArtists = FirebaseDatabase.getInstance().getReference("artists");
  6.         //get values from XML
  7.         editTextName = (EditText) findViewById(R.id.editTextName);
  8.         buttonAdd = (Button) findViewById(R.id.buttonAddArtist);
  9.  
  10.         //listViewArtists = (ListView) findViewById(R.id.ListViewArtist);
  11.         //artistList = new ArrayList<>();
  12.  
  13.         //attach clicklistener to the button
  14.         buttonAdd.setOnClickListener(new View.OnClickListener() {
  15.             @Override
  16.             public void onClick(View view) {
  17.                 addArtist();
  18.             } });
  19.            
  20.            
  21. Add artist slide 27        
  22.  private void addArtist() {
  23.         //get artistname and convert to string from editextname
  24.         String name = editTextName.getText().toString().trim();
  25.  
  26.         //check if the name is not empty
  27.         if (!TextUtils.isEmpty(name)) {
  28.             //if exist push data to firebase database
  29.             //store inside id in database
  30.             //every time data stored the id will be unique
  31.             String id = databaseArtists.push().getKey();
  32.             //store
  33.             Artist artist = new Artist(id, name);
  34.             //store artist inside unique id
  35.             databaseArtists.child(id).setValue(artist);
  36.             Toast.makeText(this, "Artist added", Toast.LENGTH_LONG).show();
  37.  
  38.         } else {
  39.             //if the name is empty
  40.             //if the value is not given displaying a toast
  41.             Toast.makeText(this, "Please enter a name", Toast.LENGTH_LONG).show();
  42.         }
  43.     }          
  44.    
  45.    
  46. Artist list slide 38
  47. public class ArtistList extends ArrayAdapter<Artist> {
  48.     private Activity context;
  49.     List<Artist> artists;
  50.  
  51.     //Create constructor
  52.     public ArtistList(Activity context, List<Artist> artists) {
  53.         super(context, R.layout.list_layout, artists);
  54.         this.context = context;
  55.         this.artists = artists;
  56.     }
  57.  
  58.     @Override
  59.     public View getView(int position, View convertView, ViewGroup parent) {
  60.         LayoutInflater inflater = context.getLayoutInflater();
  61.         View listViewItem = inflater.inflate(R.layout.list_layout, null, true);
  62.  
  63.         TextView textViewName = (TextView) listViewItem.findViewById(R.id.textViewName);
  64.  
  65.         Artist artist = artists.get(position);
  66.         textViewName.setText(artist.getArtistName());
  67.  
  68.         return listViewItem;
  69.     }
  70. }
  71.  
  72.  
  73. onstart method
  74. @Override
  75.     protected void onStart() {
  76.         super.onStart();
  77.         //attaching value event listener
  78.         databaseArtists.addValueEventListener(new ValueEventListener() {
  79.             @Override
  80.             public void onDataChange(DataSnapshot dataSnapshot) {
  81.  
  82.                 //clearing the previous artist list
  83.                 artistList.clear();
  84.  
  85.                 //iterating through all the nodes
  86.                 for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
  87.                     //getting artist
  88.                     Artist artist = postSnapshot.getValue(Artist.class);
  89.                     //adding artist to the list
  90.                     artistList.add(artist);
  91.                 }
  92.  
  93.                 //creating adapter
  94.                 ArtistList artistAdapter = new ArtistList(MainActivity.this, artistList);
  95.                 //attaching adapter to the listview
  96.                 listViewArtists.setAdapter(artistAdapter);
  97.             }
  98.  
  99.             @Override
  100.             public void onCancelled(DatabaseError databaseError) {
  101.  
  102.             }
  103.         });
  104.     }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top