SHARE
TWEET

Untitled

a guest Jun 20th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package connect2you.com;
  2.  
  3. import android.content.Intent;
  4. import android.support.annotation.NonNull;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.support.v7.widget.Toolbar;
  8. import android.text.TextUtils;
  9. import android.view.Menu;
  10. import android.view.MenuItem;
  11. import android.view.View;
  12. import android.widget.Button;
  13. import android.widget.EditText;
  14. import android.widget.ListView;
  15. import android.widget.Spinner;
  16. import android.widget.Toast;
  17.  
  18. import java.util.ArrayList;
  19. import java.util.List;
  20.  
  21. import com.google.firebase.database.DataSnapshot;
  22. import com.google.firebase.database.DatabaseError;
  23. import com.google.firebase.database.DatabaseReference;
  24. import com.google.firebase.database.FirebaseDatabase;
  25. import com.google.firebase.database.ValueEventListener;
  26.  
  27. public class WindowsPost extends AppCompatActivity {
  28.  
  29.     EditText editTextName;
  30.     Button buttonAddList;
  31.     Spinner spinnerGenres;
  32.  
  33.     DatabaseReference databaseLists;
  34.  
  35.     ListView listViewType;
  36.  
  37.     List<List> listValues;
  38.  
  39.     @Override
  40.     protected void onCreate(Bundle savedInstanceState) {
  41.         super.onCreate(savedInstanceState);
  42.         setContentView(R.layout.activity_windows_post);
  43.  
  44.         Toolbar toolbar = findViewById(R.id.toolbar);
  45.         setSupportActionBar(toolbar);
  46.  
  47.         databaseLists = FirebaseDatabase.getInstance().getReference("Windows Thread");
  48.  
  49.         editTextName = (EditText) findViewById(R.id.editTextName);
  50.         buttonAddList = (Button) findViewById(R.id.buttonAddList);
  51.         spinnerGenres = (Spinner) findViewById(R.id.spinnerGenres);
  52.  
  53.         listViewType = (ListView) findViewById(R.id.listViewType);
  54.  
  55.         listValues = new ArrayList<>();
  56.         buttonAddList.setOnClickListener(new View.OnClickListener() {
  57.             @Override
  58.             public void onClick(View v) {
  59.                 addList();
  60.             }
  61.         });
  62.     }
  63.  
  64.     @Override
  65.     protected void onStart() {
  66.         super.onStart();
  67.  
  68.         databaseLists.addValueEventListener(new ValueEventListener() {
  69.             @Override
  70.             public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  71.  
  72.                 listValues.clear();
  73.  
  74.                 for (DataSnapshot listSnapshot : dataSnapshot.getChildren()){
  75.                     List list = listSnapshot.getValue(List.class);
  76.  
  77.                     listValues.add(list);
  78.                 }
  79.  
  80.                 ListValues adapter = new ListValues(WindowsPost.this, listValues);
  81.                 listViewType.setAdapter(adapter);
  82.             }
  83.  
  84.             @Override
  85.             public void onCancelled(@NonNull DatabaseError databaseError) {
  86.  
  87.             }
  88.         });
  89.  
  90.     }
  91.  
  92.     @Override
  93.     public boolean onCreateOptionsMenu (Menu menu){
  94.  
  95.         getMenuInflater().inflate(R.menu.menu, menu);
  96.  
  97.         return super.onCreateOptionsMenu(menu);
  98.     }
  99.  
  100.     @Override
  101.     public boolean onOptionsItemSelected(MenuItem item) {
  102.  
  103.         if (item.getItemId() == R.id.menuLogOut){
  104.  
  105.             startActivity(new Intent(WindowsPost.this, MainActivity.class));
  106.         }
  107.  
  108.         return super.onOptionsItemSelected(item);
  109.     }
  110.  
  111.  
  112.  
  113.     private void addList(){
  114.         String name = editTextName.getText().toString().trim();
  115.         String genre = spinnerGenres.getSelectedItem().toString();
  116.  
  117.         if (!TextUtils.isEmpty(name)){
  118.  
  119.             String id = databaseLists.push().getKey();
  120.  
  121.             List list = new List (id, name, genre);
  122.  
  123.             databaseLists.child(id).setValue(list);
  124.  
  125.             editTextName.setText("");
  126.  
  127.             Toast.makeText(this, "Post added", Toast.LENGTH_LONG).show();
  128.  
  129.         }else{
  130.             Toast.makeText(this,"You need to enter a title", Toast.LENGTH_LONG).show();
  131.         }
  132.  
  133.     }
  134. }
  135.      
  136. package connect2you.com;
  137.  
  138. public class List {
  139.  
  140.    String listId;
  141.    String listName;
  142.    String listGenre;
  143.  
  144.     public List(){
  145.  
  146.     }
  147.  
  148.     public List(String listId, String listName, String listGenre) {
  149.         this.listId = listId;
  150.         this.listName = listName;
  151.         this.listGenre = listGenre;
  152.     }
  153.  
  154.  
  155.     public String getListId() {
  156.         return listId;
  157.     }
  158.  
  159.     public String getListName() {
  160.         return listName;
  161.     }
  162.  
  163.     public String getListGenre() {
  164.         return listGenre;
  165.     }
  166. }
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