Last active
February 23, 2018 13:38
-
-
Save majeytunio/9cc24a7edb06b2c2d3bbbb649ac9b602 to your computer and use it in GitHub Desktop.
A Great Method Generated By Majid Tunio To Parse FirebaseFirestore Data Into Android Spinner Adapter With Beautiful Way
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context="pk.jobme.jobmeapp.PostJobActivity"> | |
<Spinner | |
android:id="@+id/job_category_spinner" | |
android:layout_marginEnd="8dp" | |
android:layout_marginStart="8dp" | |
android:layout_marginTop="8dp" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
android:prompt="Choose any category" | |
android:textSize="16sp" /> | |
</android.support.constraint.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/************************************** | |
* Categories Object Class * | |
***************************************/ | |
/** | |
* Created by Majey on 2/18/2018. | |
*/ | |
public class Categories { | |
public String cat_id, cat_name, cat_description, cat_image; | |
public Categories(){ | |
} | |
public String getCat_id() { | |
return cat_id; | |
} | |
public void setCat_id(String cat_id) { | |
this.cat_id = cat_id; | |
} | |
public String getCat_name() { | |
return cat_name; | |
} | |
public void setCat_name(String cat_name) { | |
this.cat_name = cat_name; | |
} | |
public String getCat_description() { | |
return cat_description; | |
} | |
public void setCat_description(String cat_description) { | |
this.cat_description = cat_description; | |
} | |
public String getCat_image() { | |
return cat_image; | |
} | |
public void setCat_image(String cat_image) { | |
this.cat_image = cat_image; | |
} | |
public Categories(String cat_id, String cat_name, String cat_description, String cat_image) { | |
this.cat_id = cat_id; | |
this.cat_name = cat_name; | |
this.cat_description = cat_description; | |
this.cat_image = cat_image; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/***************************************** | |
* Majeys Firebase Spinner Activity Class * | |
******************************************/ | |
/** | |
* Created by Majey on 2/18/2018. | |
*/ | |
public class MajeysFirebaseSpinner extends AppCompatActivity { | |
// Firebase FireStore Object Variable | |
private FirebaseFirestore firebaseFirestore; | |
// Activity Instance Creation | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_post_job); | |
// A Great Method Generated By Majid Tunio To Parse FirebaseFirestore Data Into Spinner Adapter With Beautiful Way | |
// Instances Of Spinner | |
jobCategorySpinner = (Spinner) findViewById(R.id.job_category_spinner); | |
// Get Instance Of Firebase FireStore | |
firebaseFirestore = FirebaseFirestore.getInstance(); | |
// Set a final ArrayList for Your Categories Object | |
final ArrayList categoriesList = new ArrayList<Categories>(); | |
// Set a Final ArrayAdapter typeOf ArrayList as spinnerAdapter and add new ArrayAdapter with current Context, Layout and categoriesList | |
final ArrayAdapter<ArrayList> spinnerAdapter = new ArrayAdapter<ArrayList>(PostJobActivity.this, android.R.layout.simple_list_item_1, categoriesList); | |
// Get Data From Firebase Firestore | |
firebaseFirestore.collection("JobCategories").addSnapshotListener(new EventListener<QuerySnapshot>() { | |
@Override | |
public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) { | |
// If any error this will popup a toast including error. | |
if(e != null){ | |
Toast.makeText(PostJobActivity.this, "Firebase Error: " + e.getMessage(), Toast.LENGTH_SHORT).show(); | |
} | |
// Get all documents from selection of JobCategories | |
for (DocumentChange doc : documentSnapshots.getDocumentChanges()){ | |
// Check If Data Has Been Added Into Document Successfully | |
if(doc.getType() == DocumentChange.Type.ADDED) { | |
// Store Document Data Object Into Your Categories Object Class | |
Categories categories = doc.getDocument().toObject(Categories.class); | |
// Add Gotten Object Data Into Your ArrayList And Get Values You Want To Pupulate Into Spinner Items | |
categoriesList.add(categories.getCat_name()); | |
// Notify All Above Changes Into Spinner Adapter | |
spinnerAdapter.notifyDataSetChanged(); | |
} | |
} | |
} | |
}); | |
// Set Firestore Parsed Adapter Into Spinner | |
jobCategorySpinner.setAdapter(spinnerAdapter); | |
// A Great Method Generated By Majid Tunio To Parse FirebaseFirestore Data Into Spinner Adapter With Beautiful Way | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment